Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

C++ - Problem with reading in data to file

Hi!
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.

Problems

-----------------------------------------------------------------------------------------------

1.
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.

2.
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.
T-Jar.cpp
pot.txt
0
rijku
Asked:
rijku
  • 5
  • 2
  • 2
1 Solution
 
HooKooDooKuCommented:
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.
0
 
HooKooDooKuCommented:
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.
0
 
sarabandeCommented:
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;
}
else
    return false;

Open in new window

Sara
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!

 
HooKooDooKuCommented:
@sarabande
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.

@rijku
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.
0
 
sarabandeCommented:
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.

Sara

0
 
HooKooDooKuCommented:
The first while loop does indeed end.  Actually, it ends too soon.

When readATrans() reads the last line of the pot.txt file, the function returns 'false' because eof() of the stream has been reached.  But the tl.addMore_TL() function is inside the while loop.  The result is that the last transaction from pot.txt never gets added to the transaction list.

And again, the descriptions of the options (0-6) read like after each option, the user will be allowed to choose another option.  But selecting an option is NOT inside of a loop (other than the loop that makes sure an option 0 to 6 has been made).

But all of these issues are issues that should be easily caught by simply stepping through the code with the debuggger.

0
 
rijkuAuthor Commented:
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.
0
 
HooKooDooKuCommented:
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?
0
 
rijkuAuthor Commented:
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.
0

Featured Post

Technology Partners: 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!

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now