summing program not working

Greetings,

I'm attempting to figure out why I believe my summing program is running fifty bajillion times over as the sum I get in my outfile.dat is a huge number that is always changing

the infile.dat is just
1
2
3
4

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
int main(){
//reads the numbers in the infile and sums them up 
// writes the sum to outfile.dat

   ifstream in_stream;
   ofstream out_stream;
   
   in_stream.open("inifle.dat");
   out_stream.open("outfile.dat");
   
   int first, second, third, fourth;
   int sum = first + second + third + fourth;
   cout << "the sum is " << sum << endl;
   
   in_stream >> first >> second >> third >> fourth;
   out_stream << "The sum of this is\n"
              << sum
              << endl;
   in_stream.close();
   out_stream.close();

    
    system("pause");
    return 0;
}


I think its running again and again but I cannot figure out why

Thanks!
closed account (N36fSL3A)
-----------------------------------------------------

THE PROBLEM

Look at line 11 & 12:
int first, second, third, fourth;
You're not initializing these variables. That makes them of undefined value.

int sum = first + second + third + fourth;
Now, you're using those variables (adding them), which are of undefined value, in your program.

-----------------------------------------------------

THE SOLUTION

Just set the variables to a value.

So something like:
int first = 0, second = 0, third = 0, fourth = 0;

should fix it. You can replace '0' with any number you'd like.

--------
However, by looking at your code, I'm assuming you'd like to read the uninitialized variables from a file. Simply do this:
in_stream >> first >> second >> third >> fourth;

And that should allow your program to read the values from a file.

-----------------------------------------------------


I've spent way more time on how it looks than I should have.
Last edited on
Thanks for taking a look at it. The in_stream must not be working because after I initialize everything to zero I then use the tried to take the instream and

1
2
3
4
5
6
7
8
9
10
11
   int first =0, second =0, third =0, fourth =0;
   int sum = first + second + third + fourth;
   cout << "First Number" << first << "Second Number" << second << endl;
   cout << "the sum is " << sum << endl;
   
   in_stream >> first >> second >> third >> fourth;
   out_stream << "The sum of this is\n"
              << (first + second + third + fourth)
              << endl;
   in_stream.close();
   out_stream.close();


my sum is now zero. At least predicatable 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
#include <iostream>
#include <fstream>

int main()
{
    using namespace std ;

    ifstream in_stream("inifle.dat");
    ofstream out_stream("outfile.dat");

    int first, second, third, fourth;

    // **** calculate and print out the sum after you have read in the numbers
    // int sum = first + second + third + fourth;
    // cout << "the sum is " << sum << '\n';

    in_stream >> first >> second >> third >> fourth; // read in the numbers

    // **** now, calculate and print out the sum
    int sum = first + second + third + fourth;
    cout << "the sum is " << sum << '\n';

    out_stream << "The sum of this is\n"
              << sum
              << '\n';
}
closed account (N36fSL3A)
Yes, like I've said above in my post, you aren't reading the files into the program. C++ doesn't magically know what you want to do.

JLBorges put my example into a runnable form. (Well he forgot the return value, but whatever)

So this should be what I meant:

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>

int main()
{
    using namespace std ;

    ifstream in_stream("inifle.dat");
    ofstream out_stream("outfile.dat");

    int first = 0, second = 0, third = 0, fourth = 0;

    // **** calculate and print out the sum after you have read in the numbers
    // int sum = first + second + third + fourth;
    // cout << "the sum is " << sum << '\n';

    in_stream >> first >> second >> third >> fourth; // read in the numbers

    // **** now, calculate and print out the sum
    int sum = first + second + third + fourth;
    cout << "the sum is " << sum << '\n';

    out_stream << "The sum of this is\n"
              << sum
              << '\n';

    return 0;
}
Last edited on
figure out what was wrong infile.dat was misspelled. Silly me thank you for your help!
Topic archived. No new replies allowed.