We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


C++ - Problem with reading in data to file

rijku asked
Medium Priority
Last Modified: 2012-06-27
I'm making a program that works as a money jar or pot. It works like, e.g. if three people are out on a dinner then one of the three may pay for all of them, and then someone else pays for the next time's activity, so if the dinner costs 120, then person B and C are in a 40 debt each to person A.

I have attached the code as a cpp file and a infile (pot.txt) . In pot.txt is it showed how the "transactions" are being set up.



My main() consists of a menu with choices from 0-6. I have a problem in my first choice 1, which should read a transaction from the keyboard with the help of the method "Transaction::readATrans()". At the moment, it seems not to work at all. Would gladly accept some hints of how to make it work correct.
I have made the readATrans() method a "bool" so that i later can be called by "TransactionList::loadin()" which is a method that reads from/to file or the screen.

I also have a problem with the maths, in the choice 6, incorrect numbers will be summoned, and i dont know where i've screwed up.


I'll be glad for any help or suggestions provided.
Please ask if I've been unclear, or if additional info is needed.
Watch Question


You need to step through your code in a debugger.

From what I can tell, Option #1 seems to do nothing because you call "readTrans" passing in a reference to the standard input... the code is basically "hanging" on the command
  is >> date >> type >> name >> amount >> num_griends
waiting for you to manually key these 5 pieces of data.

As for issue #2, I think your problem is that when you calculate how much someone has paid, you are dividing the amount paid by the number of people present.  But the amount someone paid is the amount they paid regardless of how many people were present.
Top Expert 2016

the t.readATrans call doesn't read from keyboard but from file pot.txt. the problem could be that you didn't check on fail status of the stream what could happen quite easily.

i think it would be better if you read the file pot.txt line by line and do the conversions using a temporary istringstream. that way the file read wouldn't fail and you could handle the case when a conversion error occurs.

std::string strtrans;
if (std::getline(is, strtrans))
     std::istringstream iss(strtrans);
     if (!(iss >> date))
         // report error and return false
     if ((!(iss >> type))
         // report error and return false

     // and so on with  name >> amount >> num_friends and friends[i]
    return true;
    return false;

Open in new window


While t.readATrans does indeed read from pot.txt BEFORE the switch statement, case 1 of the switch statement calls readATrans again with cin as the referenced stream.

The call to readATrans appears to work if you simply key in a line of text as it appears in the pot.txt file.  So that doesn't seem to be an issue.  But what does look "odd" is that once a choice has been made and the switch statement finishes execution, the application terminates.  So when 1 is selected, the pot.txt file is read, an additional transaction is added from the keyboard, and then the program terminates.

It looks to me that the intent of the application should have been to read the contents of the pot.txt file, then go into a loop that displays the choices, executes the switch statement based on the choice, and loops back to display the choices again unless 0 was the choise.

Of course the user does have to know the format of the input line to properly key a transaction.  Such an input is obviously error prone, but that doesn't appear to be the problem with the application.
Top Expert 2016

yes, i saw both calls but i assumed it doesn't end the first while loop cause it never comes to the is.eof() == true?

rijku, you might think of putting all code of main function into a outer while loop which you do break when choice is exit. then (1) better would be to read the records from pot.txt.


Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


Thanks for the input!
By reading your comments I imagine that I should update the main() with a loop that keeps looping until the condition 0 (exit) is met.

And then somehow fix the tl.addMore_TL() (and all that is related) so that the transactions will be added correctly to the transaction list.

would that make the application okay you say?

And sorry, I'm new to programming since winter, and I haven't used the debugger the way I guess you do, but I'll look it up so that I maybe can ask less questions in the future.

Don't forget the issue about how much someone has paid... (you are dividing the amount someone pays by the number of people present... doesn't make any sense).

Otherwise, as a new programmer, one of the tools you MUST learn is the ability to step through your program so that you can catch these types of errors.  

What are you using the compile the program?


Yeah, I haven't forgot! I'll take a closer look and work it out.

I can imagine!

I'm using Code blocks, for the reason that it's simple when doing small applications, like no projects etc.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.