I am a beginner programmer taking my first C++ class. I've had great luck so far just following examples/book information/instructor information/the occasional Google search. Tonight all my methods failed. Here's my assignment:
Write a program to input and display a sales file. Each input line contains an integer ID number and a sales amount. When the end of the file occurs, print the high sales amount and the number of times that amount occurred. Print also the second highest sales amount and the number of times that one occurred.
I have managed to get the program to display correctly, as well as find the largest and second largest numbers. However, no matter what I change (have spent upwards of 6 hours on this already, and am exhausted) the counter for finding the second largest number fails. If someone could point me in the right direction, I'd really appreciate it.
Could you show the contents of sales1.txt so we can test it?
I'm guessing it's just due to the order in which the items come. Imagine this list:
1 2 3 4 5 6 7 8 9 10
-> count1 will be 10, because a new maximum is found at every step. count2 will be 0.
The thing is, every step is checked separately and sequentially. The 2nd highest number could be the maximum at the moment it is evaluated. Once a higher value is found, the number is discarded.
Maybe add an extra check in the if at line 51 to check whether the old hiSales1 is higher than hiSales2, and if so replace it before discarding the value.
There were originally more spaces between the columns, so I reduced it down to 1 character between the two thinking that would solve the problem for my inSales.ignore(1); on line 71.
I could not get the additional check to make any difference. It returned dollar amounts as $0.00 instead, and the count was 0. I played around with the location, but it didn't help.
This is a problem I just found- when I run the program with sales2.txt or sales3.txt, it doesn't even return the right amounts. I really don't understand what is flawed in my code that would cause this kind of massive variation between the txt files. Here are the other two for relevancy: