Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

File Input Problem

Posted on 1998-04-15
4
159 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

809 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