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
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
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.
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();
#include <iostream>
#include <fstream>
int main()
{
usingnamespace 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';
}
#include <iostream>
#include <fstream>
int main()
{
usingnamespace 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;
}