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:

                        1
2
3
4
v
half-dozen
seven
8
ix
x
11
12
13
fourteen
fifteen
16
17
18
xix
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 #'south > 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 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.

                        1
2
3
4
5
6
seven
8
9
10
11
12
thirteen
14
15
16
17
eighteen
19
20
21
22
23
24
25
26
27
                                                  #include <iostream>                          #include <fstream>                          using                          namespace                          std;                          int                          master() {     ifstream inFile;                          int                          value;      inFile.open up("My_Path.txt");                          if                          (!inFile)     {         cout <<                          "\nError opening file.\n";                          return                          thirteen;     }                          while                          (inFile >> value)     {                          while                          (value > 0)         {             cout << value;         }     }      inFile.shut();                          return                          0; }                      

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

                        1
2
3
4
5
6
7
8
9
ten
11
12
13
14
xv
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;// aforementioned as above                          inFile.open up("My_Path.txt");                          while(inFile >> value)                          // While file can exist read in ( You lot can increase numbers in text                          {         count++;                          // Increase count                                                    cout <<                          "Sum of #'s > "                          << count;                          // Print out variable                                                    sum += value;                          // Increase sum total                          }  inFile.shut();                          // Close file.                          cout <<                          "Sum total of all "                          << count <<                          " numbers, was "<< sum << endl;                          // Print out the results                          return                          0; }                      

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.

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

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:

                        1
ii
3
4
5
6
7
8
9
ten
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?

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.

                        i
2
3
4
5
half dozen
seven
viii
9
10
11
12
13
14
fifteen
sixteen
17
18
19
xx
21
22
23
24
25
26
27
28
29
thirty
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 up("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;                          render                          0; }                      

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?

                        ane
2
3
four
5
6
seven
8
ix
10
eleven
12
13
14
15
xvi
17
18
19
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
34
35
36
37
38
39
xl
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                          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 #'due south > 0: "                          << sump;     cout <<                          "Count of the #'s > 0: "                          << countp;     cout <<                          "Sum of the #'s < 0: "                          << sumn;     cout <<                          "Count of the #'southward < 0: "                          << countn;     cout <<                          "Count of the #'s = 0: "                          << countz;                          return                          0; }                      

Concluding edited on

                        one
2
three
4
five
                                                  else                          {             countz++;             sumz += value;         }     }                      

Should be :

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

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?

badgerditer1940.blogspot.com

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

0 Response to "C++ Read in Name and Numbers From File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel