Link to home
Start Free TrialLog in
Avatar of RySk8er30
RySk8er30

asked on

Garbage read from file...

Hi,

I have a function in a class called (Account) which loads information from a file.  Here is the code...

void Account::verifyAcct() {

      bool exit = false;
      int pinFile = 0;

      do {

      lower();
      center("Please enter your four digit account number: ");
      cout << endl << setw(37) << " ";
      acctNum = getNumOnly();
      cout << endl << endl;
      center("Please enter your four digit pin number: ");
      cout << endl << setw(37) << " ";
      pin = getPin();

      _itoa(acctNum, acctFile, 10);

      strcat(acctFile, ".txt");

      ifstream infile;

      //Tries to open the file.  If it does not exist, it does not create one.
      infile.open(acctFile, ios::out|ios::nocreate);
      
      //If the account does not exist, prompt user to create one.
      
      if (!infile) {

            ClearScreen;
            lower();
            center("Invalid account number...");
            cout << endl << endl;
            pause();
            ClearScreen;

      } else {

            infile >> acctNum;
            infile >> balance;
            infile >> pinFile;
            infile >> firstName;
            infile >> middleInital;
            infile >> lastName;

            if (pinFile != pin) {

                  ClearScreen;
                  lower();
                  center("Invalid pin number...");
                  cout << endl << endl;
                  pause();
                  ClearScreen;

            } else {
                  
                  exit = true;

            }

            //Closes the file
            infile.close();

      }

      } while (!exit);

}

Now when I try to call those values with another function in the same Account class...

void Account::displayAcctInfo(){ //Displays account information to the user
      
      /*

      Function Name: displayAcctInfo
      Purpose: To display current account information
      Parameters:
            Input: none
            Input & Output: none
            Output: acctNum, balance, firstName, lastName
      Return Value: void
      Data Members Accessed: none
      Data Members Modified: none
      Non-local Variables Used: none
      Functions Called: none

      */

      
      //Outputs information to user
      system("cls");
      lower();
      center("Account Information:");
      cout << endl;
      cout << setw(31) << "Name:" << firstName << " " << middleInital << " " << lastName << endl;
      cout << setw(32) << "Account Number: " << acctNum << endl;
      cout << setw(32) << "Balance Amount: " << balance << endl << endl;
      pause();
      ClearScreen;

} //End displayAcctInfo

I just get garbage.  What am I doing wrong?  The variables are all private to the function.

Ryan
Avatar of lakshman_ce
lakshman_ce

>ifstream infile;
You have to use ofstream for using ios::out

-Lakshman
Avatar of RySk8er30

ASKER

Hi,

If I make firstName (a variable that is being read) public in the class and try to read if from another .cpp file, it works, but if I read it from the Account class I get problems.

Ryan
   ifstream infile;

     //Tries to open the file.  If it does not exist, it does not create one.
     infile.open(acctFile, ios::in|ios::nocreate);        // <<<<<---- correct like this
Hi,

I know the file exits and the data is valid.

Ryan
Do You correct for ios::in ?
Yes.  When I put the function calling the displayAcctInfo, in the class account and it worked.

Ryan
Are firstName, acctNum, etc, Account class members? Or just global variables?
How exactly do you call displayAcctInfo() when you get garbage? Do you call verifyAcct() before?
Looks like the variables are not initialized. Are you sure that you have assigned the values to the variables you are refering to?
Can you post us the part of code where you do things like that?

   Account any;
   any.verifyAcct();
   any.displayAcctInfo();

I assume that firstName, acctNum, ...  are member variables of class Account. However, you should make sure that these variables are not defined as global variables somewhere else (maybe in a header file...). You may test this, by renaming the class members, e. g. by adding an 'm_' prefix.

Regards, Alex
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial