Solved

File Input Problem

Posted on 1998-04-15
4
156 Views
Last Modified: 2010-04-10
I'm having a problem compiling on line 180.  the content for the init file can be any filename under 50 characters long and should be read into the program.  Line 180 cause the program to crash for some unknown reason.  The filenames when the program is used will have to be at least 15 characters long.  It is prefered that the filenames be read in one character at a time, but any solutions would be helpful.  The source code follows:

#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <conio.h>
#include <fstream.h>
#include <bool.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>
#include <string.h>
#include <bios.h>
#include <mouse.cpp>
#include <cursor.h>


//Definitions///////////////////////////////////////////////////////////////

#define       RIGHT       0x01
#define       LEFT        0x02
#define       CTRL        0x04
#define       ALT         0x08


//Functions/////////////////////////////////////////////////////////////////

void                         Title_Screen                                                (void);

void                         Load_Init                                                            (int &);

int                         Pick_Test                                                            (int &);

void                         Load_Test                                                            (void);

void                         Read_Test_Question                              (void);

void                         Read_Question                                                (void);

void                         Read_Choices                                                (void);

void                         Skip_Question                                                (int);

void                         Main_Screen                                                      (void);

void                         Good_Bye                                                            (void);

void                         Save_Question_Answer                        (void);

void                         Run_Test                                                            (void);


//Global Constants//////////////////////////////////////////////////////////

const int            Max_Question_Line_Length            =            300;

const int            Number_Choices                                          =            4;

const int            Max_Choice_Line_Lengths                  =            120;

const int            Max_Number_O_Test_Files                  =            10;

const int            Test_Filename_Length                        =            50;

const int            Mouse_Speed_Threshold                        =            32;

const char      Program_Version                              []      =            "0.1";

const char      Program_Author                              []      =            "Scott Mcleod";

const char  Init_Filename                                    []      =            "A:\\TESTER.INI";


//Databases/////////////////////////////////////////////////////////////////

struct                   Tes_Type
{

      char                   Question                                                 [Max_Question_Line_Length];

      char                   Choice                                                   [Number_Choices]
                                                                                                            [Max_Choice_Line_Lengths];
};

char Test_Filenames [Max_Number_O_Test_Files][Test_Filename_Length];


//Filestreams///////////////////////////////////////////////////////////////

ifstream            InWard;
ofstream             OutWard;


//The MAIN program block////////////////////////////////////////////////////
void main(void)
{
      int Mouse_Installed                  =      0,
                  Number_O_Tests                   =      0,
                  Picked                                          =      0;

      randomize();

      atexit(Good_Bye);

//      Init the mouse
      if(mouse.Exists());
      {
            Mouse_Installed = 1;
            mouse.SetSpeedThreshold(Mouse_Speed_Threshold); // Defualt 255
            mouse.Disable();
            mouse.Hide();
      }

      Title_Screen();

      Load_Init(Number_O_Tests);

      Picked = Pick_Test(Number_O_Tests);
}


//Displays the title screen/////////////////////////////////////////////////
void Title_Screen(void)
{
//      Sets the normal video
      normvideo();

      textcolor(15);
      textbackground(0);

//      Clear the screen before useing
      clrscr();

//      Display the title screen
      cout << "Tester Version " << Program_Version << " By " << Program_Author
                   << endl;

//      Wait before clear screen
      sleep(1);
      clrscr();
}


//Loads the INI file for this program///////////////////////////////////////
void Load_Init(int &Number_O_Tests)
{
      int a=0,
                  b=0,
                  File_Fail=0;

      char ch = ' ';

      char temp[30];


//      Load the init file
      InWard.open("a:\\tester.inn");

//      Get the file error status
      File_Fail = !InWard.fail();

//      if there was a problem, then it was the location
//      to cause this error
      if(!File_Fail)
      {
            cout << "Please move the " << Init_Filename << " file to the current" <<
                                    " directory\n";
            cout << "Error finding " << Init_Filename << " in current directory\n";
            abort();
      }

      for(a=1;a<=Max_Number_O_Test_Files;a++)
      {
            strnset(Test_Filenames[a],' ',(Test_Filename_Length+1));
      }

      for(a=1;a<=Max_Number_O_Test_Files;a++)
      {
            for(b=1;b<=Test_Filename_Length;b++)
            {
                  ch = InWard.get();

                  if((ch != '\n') && (ch != ' ') && (ch != NULL))
                        Test_Filenames[a][b] = ch;
                  if((ch == '\n') || (ch == ' ') || (ch == NULL))
                        b = Test_Filename_Length + 1;
            }
      }

      for(a=1;a<=Max_Number_O_Test_Files;a++)
      {
            if(strlen(Test_Filenames[a]) != 0)
                  Number_O_Tests++;
      }

      InWard.close();

//      Get the file error status
      File_Fail = !InWard.fail();

//      if there was a problem, then it was the location
//      to cause this error
      if(!File_Fail)
      {
            cout << "There was a error closing the file\n";
            cout << "This program will now terminate";
            abort();
      }
}


//Allows the user to pick a test to take////////////////////////////////////
int Pick_Test(int &Number_O_Tests)
{
      int      Picked = 0,
                  a = 0,
                  b = 0;
      char ch;

//      Display loaded databases
      clrscr();

//      Display the filenames and their corresponding number
      cout << "Filenames:\nÍÍÍÍÍÍÍÍÍÍ\n\n";

//      Display the language file for the user ti pick from
      for(a=1;a<=10;a++)
      {
            if(a==10)
                  cout << "Test File #" << a << ": ";
            else
                  cout << "Test File # " << a << ": ";

            for(b=1;b<=Test_Filename_Length;b++)
            {
                  ch = Test_Filenames[a][b];
                  if((ch != '\n') && (ch != ' '))
                        cout << ch;
            }
      }

      cout << '\n';

      cout << "\nÍÍÍÍÍÍÍÍÍÍ\n";

//      Display the number of daatabases
      cout << "\nNumber of Tests:  " << Number_O_Tests << "\n";

//      Abort the program if no databases are loaded
      if (Number_O_Tests < 1)
      {
            cout << "A file error has occured the " << Init_Filename << " is either empty or\n"
                         << "has been corrupted\n";
            abort();
      }

//      If only one database, no choice is required
      if(Number_O_Tests == 1)
      {
            Picked = 1;
      }

//      if not then a choice is required
      else
      {
//            Loop until a valid number is picked
            do
            {
                  cout << "Pick a number: ";
                  cin >> Picked;
            }while ((Picked <= 1) && (Picked >= 10));
      }

//      Return the value of the language file picked
      return Picked;
}


//Display the end of program screen/////////////////////////////////////////
void Good_Bye(void)
{
//      Clear the screen before printing to it
      clrscr();

//      Say goodbye to the user
      cout << "GoodBye :)" << endl << "Thank you for using tester as your test taking tool" << endl;

//      Wait to quit program
      sleep(2);
}

:End of source code
0
Comment
Question by:Dragon041598
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 200 total points
Comment Utility
These are wrong:

    for(a=1;a<=Max_Number_O_Test_Files;a++)
    for(b=1;b<=Test_Filename_Length;b++)

Etc...

Arrays indexes in C/C++ start from 0, not form 1.
E.g., int x[16] contains elements x[0] to x[15].

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Try this:

    for(a=0;a<Max_Number_O_Test_Files;a++)
    {
        strnset(Test_Filenames[a],' ',(Test_Filename_Length));
    }
   
    for(a=0;a<Max_Number_O_Test_Files;a++)
    {
        for(b=0;b<Test_Filename_Length;b++)
        {
            ch = InWard.get();
           
            if((ch != '\n') && (ch != ' ') && (ch != NULL))
                Test_Filenames[a][b] = ch;
            else
                break;
        }
    }
   
    for(a=0;a<Max_Number_O_Test_Files;a++)
    {
        if(strlen(Test_Filenames[a]) != 0)
            Number_O_Tests++;
    }
   
Or even:

    for(a=0;a<Max_Number_O_Test_Files;a++)
    {
            InWard >> Test_Filenames[a];
           
            if(*Test_Filenames[a])
                Number_O_Tests++;
            else
                break;
    }
   
No need to zero the array, if you define thus:

    char Test_Filenames [Max_Number_O_Test_Files][Test_Filename_Length] = { 0 };

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Remember that the best C++ programmers have only nine fingers, yet they can type as well as the rest of us.  How is this possible?  They count their fingers starting from 0!
0
 

Author Comment

by:Dragon041598
Comment Utility
Thanks for the help.  It was the array, and the program works now.  I still like to read characters in one at a time; so that I can filter while reading the file saving code later in the program to filter it back out, but thanks again.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 66
ASP.net build a IF/Then Walkthrough Guide 1 165
computer science syllabus 3 52
c++ syntax question 9 34
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now