Skip to content Skip to sidebar Skip to footer

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:

                        1
2
3
iv
5
six
vii
viii
nine
10
11
12
13
14
xv
xvi
17
18
19
20
21
22
23
24
25
26
27
28
                                                  #include <iostream>                          #include <fstream>                          using                          namespace                          std;                          int                          primary() {     ifstream inFile;                          int                          value1, value2, value3, value4;                          int                          sum;                          int                          count = 0;      inFile.open up("My_Path");      inFile >> value1, value2, value3, value4;      inFile.shut();      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 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.

                        one
ii
three
iv
5
6
7
8
9
ten
11
12
xiii
xiv
fifteen
16
17
xviii
19
20
21
22
23
24
25
26
27
                                                  #include <iostream>                          #include <fstream>                          using                          namespace                          std;                          int                          main() {     ifstream inFile;                          int                          value;      inFile.open up("My_Path.txt");                          if                          (!inFile)     {         cout <<                          "\nError opening file.\northward";                          return                          13;     }                          while                          (inFile >> value)     {                          while                          (value > 0)         {             cout << value;         }     }      inFile.close();                          render                          0; }                      

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

                        1
two
3
4
5
6
7
8
nine
10
11
12
13
14
xv
16
17
18
xix
20
21
22
23
24
25
                                                  #include <iostream>                          #include <fstream>                          using                          namespace                          std;                          int                          primary() {     ifstream inFile;                          int                          value;                          int                          sum = 0;                          // Initialize to 0;                          int                          count = 0;// aforementioned as higher up                          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 #'due south > "                          << count;                          // Print out variable                                                    sum += value;                          // Increase sum total                          }  inFile.shut();                          // Close file.                          cout <<                          "Sum full of all "                          << count <<                          " numbers, was "<< sum << endl;                          // Print out the results                          return                          0; }                      

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.

                        1
2
3
iv
5
half dozen
                                                  while(inFile >> value)                          // While file can be read in ( You can increase numbers in text                          {         count++;                          // Increase count                                                    cout <<                          "Sum of #'south > "                          << count;                          // Impress out variable                                                    sum += value;                          // Increment sum total                          }                      

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:

                        1
ii
3
four
5
vi
7
8
9
10
11
12
13
fourteen
xv
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?

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.

                        ane
two
3
four
v
6
vii
8
9
ten
11
12
13
fourteen
fifteen
16
17
18
nineteen
twenty
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.shut();      cout <<                          "Sum of #'s > 0: "                          << countp;                          return                          0; }                      

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?

                        i
ii
3
4
five
half-dozen
7
8
ix
10
11
12
13
14
15
sixteen
17
18
19
xx
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                          primary() {     ifstream inFile;                          int                          countn, countp, countz;                          int                          value;                          int                          sump, sumn, sumz;      inFile.open("My_Path.txt");                          if                          (!inFile)     {         cout <<                          "error";                          render                          xiii;     }          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 #'south < 0: "                          << sumn;     cout <<                          "Count of the #'south < 0: "                          << countn;     cout <<                          "Count of the #'s = 0: "                          << countz;                          return                          0; }                      

Last edited on

                        i
2
3
iv
5
                                                  else                          {             countz++;             sumz += value;         }     }                      

Should be :

                        1
2
3
4
five
                                                  else                          {             countz++;             sumz += value;         }                          inFile >> value;                          }                      

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?

smartsopland.blogspot.com

Source: https://www.cplusplus.com/forum/general/198475/

Post a Comment for "Reading an Int From a File C++"