Reading numbers from a file

Pages: 12
Hi. I'm 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int value1, value2, value3, value4;
    int sum;
    int count = 0;

    inFile.open("My_Path");

    inFile >> value1, value2, value3, value4;

    inFile.close();

    sum = value1 + value2 + value3 + value4;

    while ( sum > 0 )
    {
        cout << "Sum of #'s > 0    : ";
        sum += value;
        count++;
    }

    return 0;
}
 
inFile >> value1, value2, value3, value4;

should be
 
inFile >> value1 >> value2 >> value3 >> value4;

assuming that each of the values are separated by a whitespace.
Yep, didn't catch that typo. My main confusion 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 direction? I'm currently have

for (int value = 0; value > 0; count++)

Is that wrong?
Last edited on
I thought I was on the right track for this, but the output displayed an endless amount of sixes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int value;

    inFile.open("My_Path.txt");
    if (!inFile)
    {
        cout << "\nError opening file.\n";
        return 13;
    }

    while (inFile >> value)
    {
        while (value > 0)
        {
            cout << value;
        }
    }

    inFile.close();
    return 0;
}
Last edited on
@Guy1988

For line 19, try if (value > 0)

I'm thinking that te first number read in with the infile, was a 6. So, since value never decreases and become less than 0, you enter an endless loop, always displaying the first number, which was a 6;
Yep, that's what I ended up doing. Would I nest the other if statements inside? To add the counts for each if statement, do I add count++ inside each one?

If it helps, the numbers in my file are -9, 6, 0, and 4.
@Guy1988

You seem to be 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 get things figured out..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int value;
    int sum = 0; // Initialize to 0;
    int count = 0;// same as above

    inFile.open("My_Path.txt");

    while(inFile >> value) // While file can be read in ( You can increase numbers in text
    {
        count++;  // Increase count      
        cout << "Sum of #'s > " << count; // Print out variable 
        sum += value; // Increase sum total
    }
 inFile.close(); // Close file.
cout << "Sum total of all " << count << " numbers, was "<< sum << endl; 
// Print out the results

    return 0;
}


If it helps, the numbers in my file are -9, 6, 0, and 4.


Yep. First number that was greater than 0 is the 6. Line 19 disregarded the -9, since it was not greater than 0
My first post was me completely lost with what to do. I then thought I had to nest while loops.

1
2
3
4
5
6
while(inFile >> value) // While file can be read in ( You can increase numbers in text
    {
        count++;  // Increase count      
        cout << "Sum of #'s > " << count; // Print out variable 
        sum += value; // Increase sum total
    }


This is what I'm stuck on. How do 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 now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
inFile >> value;
    while (inFile)
    {
        if (value > 0)
        {
            count++;
        }
        else if (value < 0)
        {
            count++;
        }
        else
        {
            count++;
        }
    }


How do I display separate counts and sums for each of the branched if statements?
closed account (48T7M4Gy)
You 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 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
5. do the same for new_number > 0 ...
...

6. loop back to get a new new_number
Last 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 do is display the sums and counts for those numbers.

I've got something that looks like this now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int countn = 0, countp = 0, countz = 0;
    int value;
    int sumn, sump, sumz, sum;

    inFile.open("My_Path.txt");

    inFile >> value;
    while (inFile)
    {
        if (value > 0)
        {
            sum += sump;
            countp++;
        }
        else if (value < 0)
        {
            countn++;
        }
        else
        {
            countz++;
        }
    }

    inFile.close();

    cout << "Sum of #'s > 0: " << countp;
    return 0;
}
Now, when I execute, nothing is displayed.
What is the contents of the file "My_Path.txt"?
The numbers I listed: -9, 6, 0, and 4 in Notepad.

I made sure the file opened first, before I did anything else, and it processed.
I'm getting no output displayed when executing. What's going on?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int countn, countp, countz;
    int value;
    int sump, sumn, sumz;

    inFile.open("My_Path.txt");

    if (!inFile)
    {
        cout << "error";
        return 13;
    }
    
    inFile >> value;
    while (inFile)
    {
        if (value > 0)
        {
            countp++;
            sump += value;
        }
        else if (value < 0)
        {
            countn++;
            sumn += value;
        }
        else
        {
            countz++;
            sumz += value;
        }
    }

    inFile.close();

    cout << "Sum of #'s > 0: " << sump;
    cout << "Count of the #'s > 0: " << countp;
    cout << "Sum of the #'s < 0: " << sumn;
    cout << "Count of the #'s < 0: " << countn;
    cout << "Count of the #'s = 0: " << countz;
    return 0;
}
Last edited on
1
2
3
4
5
else        {
            countz++;
            sumz += value;
        }
    }

Should be :
1
2
3
4
5
else        {
            countz++;
            sumz += value;
        } inFile >> value;
    }
What's going on?

There 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 wrong?
Pages: 12