Solved

File Input Problem

Posted on 1998-04-15
4
160 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
ID: 1184307
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
ID: 1184308
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
ID: 1184309
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
ID: 1184310
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

679 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