Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Garbage read from file...

Posted on 2004-04-04
11
Medium Priority
?
253 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

885 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