C++ Read in Name and Numbers From File
Howdy. I'thousand completely new to programming and I have a question about a program 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 think I may have the beginning correct. This is my measly attempt:
| |
| |
should be
| |
assuming that each of the values are separated past a whitespace.
Yep, didn't catch that typo. My master defoliation lies with how would I get the counts and sums.
Should I be using a for loop or a while loop? Can someone at least point me in that management? I'm currently have
for (int value = 0; value > 0; count++)
Is that wrong?
Last edited on
I thought I was on the right rail for this, merely the output displayed an endless amount of sixes.
| |
Last edited on
@Guy1988
For line 19, try if (value > 0)
I'm thinking that te beginning number read in with the infile, was a 6. So, since value never decreases and become less than 0, you lot enter an endless loop, e'er displaying the outset number, which was a 6;
Yep, that'due south what I concluded upward doing. Would I nest the other if statements inside? To add the counts for each if statement, practise I add count++ inside each one?
If it helps, the numbers in my file are -nine, half dozen, 0, and 4.
@Guy1988
You seem to be referring to your original postal service code, as there is no count, or other if statement, in it. Seems the above program was simply a examination to get things figured out..
| |
If information technology helps, the numbers in my file are -9, 6, 0, and 4.
Aye. Get-go number that was greater than 0 is the half dozen. Line 19 disregarded the -9, since it was non greater than 0
My first post was me completely lost with what to practise. I then idea I had to nest while loops.
| |
This is what I'm stuck on. How practice I get the multiple values read and display each count and sum for >, <, and then brandish the count for == 0.
This is what I accept now:
| |
How do I display separate counts and sums for each of the branched if statements?
You lot need to have 3 sum variables and 3 count variables and test as each new number is read in:
1. setup variables count_of_negative_numbers, sum_of_ngative numbers, count_of_positive_numbers etc etc
2. read a new number chosen new_number
3. if new_number < 0
and then increase count-of_negative_numbers and add together new_number to sum_of_negative numbers
4. if new_number == 0 ... aforementioned procedure
5. do the aforementioned for new_number > 0 ...
...
half-dozen. loop back to get a new new_number
Terminal edited on
This is so frustrating for me, I'm not understanding.
What practice I read a new number for? Everything that needs to be read is in the file. What I have to do is brandish the sums and counts for those numbers.
I've got something that looks like this at present.
| |
Now, when I execute, cypher 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 sure the file opened first, before I did anything else, and it processed.
I'thou getting no output displayed when executing. What's going on?
| |
Concluding edited on
| |
Should be :
| |
In that location is an infinite loop. The program never finishes. It gets stuck in the while loop at line 21.
I was getting help from someone and now I'm getting crazy large numbers in my output.
So is my whole structure incorrect?
Source: https://www.cplusplus.com/forum/general/198475/
0 Response to "C++ Read in Name and Numbers From File"
Post a Comment