Reading an Int From a File C++
Howdy. I'm completely new to programming and I have a question near a programme I have to write.
I need to read some numbers from a file and display the sum of the numbers > 0, count of the numbers > 0, sum of the number < 0, count of the numbers < 0, and a count of the numbers == 0.
I'm completely stuck on how I should write the while loops. Hell, how I should write anything. I recall I may have the beginning correct. This is my measly attempt:
| |
| |
should be
| |
assuming that each of the values are separated by a whitespace.
Yep, didn't catch that typo. My principal confusion lies with how would I become the counts and sums.
Should I be using a for loop or a while loop? Can someone at least signal me in that direction? I'yard currently accept
for (int value = 0; value > 0; count++)
Is that wrong?
Last edited on
I idea I was on the right rail for this, but the output displayed an endless amount of sixes.
| |
Last edited on
@Guy1988
For line 19, endeavor if (value > 0)
I'm thinking that te start number read in with the infile, was a vi. So, since value never decreases and become less than 0, you lot enter an endless loop, always displaying the offset number, which was a 6;
Yep, that'south what I ended upward doing. Would I nest the other if statements inside? To add the counts for each if argument, do I add count++ inside each one?
If it helps, the numbers in my file are -nine, 6, 0, and iv.
@Guy1988
You seem to exist referring to your original post code, as there is no count, or other if statement, in it. Seems the above program was just a test to go things figured out..
| |
If it helps, the numbers in my file are -nine, half-dozen, 0, and four.
Yep. Get-go number that was greater than 0 is the 6. Line 19 disregarded the -nine, since it was not greater than 0
My first post was me completely lost with what to exercise. I and then thought I had to nest while loops.
| |
This is what I'1000 stuck on. How exercise I get the multiple values read and display each count and sum for >, <, and then display the count for == 0.
This is what I have at present:
| |
How do I display separate counts and sums for each of the branched if statements?
You need to have 3 sum variables and 3 count variables and test equally each new number is read in:
one. setup variables count_of_negative_numbers, sum_of_ngative numbers, count_of_positive_numbers etc etc
2. read a new number called new_number
3. if new_number < 0
then increment count-of_negative_numbers and add new_number to sum_of_negative numbers
4. if new_number == 0 ... same process
five. do the same for new_number > 0 ...
...
6. loop back to become a new new_number
Concluding edited on
This is so frustrating for me, I'm not understanding.
What do I read a new number for? Everything that needs to be read is in the file. What I have to exercise is display the sums and counts for those numbers.
I've got something that looks like this now.
| |
At present, when I execute, zero is displayed.
What is the contents of the file "My_Path.txt"?
The numbers I listed: -nine, 6, 0, and 4 in Notepad.
I made certain the file opened first, before I did anything else, and information technology processed.
I'm getting no output displayed when executing. What'south going on?
| |
Last edited on
| |
Should be :
| |
At that place is an space loop. The plan never finishes. It gets stuck in the while loop at line 21.
I was getting help from someone and at present I'm getting crazy big numbers in my output.
And then is my whole construction incorrect?
Source: https://www.cplusplus.com/forum/general/198475/
Post a Comment for "Reading an Int From a File C++"