Solved

Garbage read from file...

Posted on 2004-04-04
11
249 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ substatte a varabe for a string in a LPCTSTR statment 8 94
Header of docx file 17 129
show out valin of json for debugging in visaul c++ 1 47
Outlook 13 76
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

679 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