Solved

Garbage read from file...

Posted on 2004-04-04
11
245 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:RySk8er30
11 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10754129
>ifstream infile;
You have to use ofstream for using ios::out

-Lakshman
0
 

Author Comment

by:RySk8er30
ID: 10754154
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
0
 
LVL 17

Expert Comment

by:mokule
ID: 10754259
   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
0
 

Author Comment

by:RySk8er30
ID: 10754277
Hi,

I know the file exits and the data is valid.

Ryan
0
 
LVL 17

Expert Comment

by:mokule
ID: 10754305
Do You correct for ios::in ?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:RySk8er30
ID: 10754316
Yes.  When I put the function calling the displayAcctInfo, in the class account and it worked.

Ryan
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10755572
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?
0
 

Expert Comment

by:mprasanna
ID: 10755609
Looks like the variables are not initialized. Are you sure that you have assigned the values to the variables you are refering to?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10755800
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
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10907855
Closed, 125 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
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.

707 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

11 Experts available now in Live!

Get 1:1 Help Now