Solved

Garbage read from file...

Posted on 2004-04-04
11
247 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 68
Issue accessing member variable of atomic class 16 116
What does std::atomic give me? 7 125
Microsoft C++ code failing in executable that worked 9 101
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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