#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int number, sum;
ifstream indata;
indata.open("data.txt");
if(!indata)
{
cout<<"Can't open the file"<<endl;
return 1;
}
cin>>indata;
sum = 0;
while (indata)
{
indata >> number;
sum = sum + number;
}
cout<<"The sum is:" << sum <<endl;
indata.close();
system("pause");
return 0;
Having problems with a while loop
Page 1 of 15 Replies - 1934 Views - Last Post: 10 February 2012 - 08:38 AM
#1
Having problems with a while loop
Posted 10 February 2012 - 12:42 AM
I am working on a problem that deals with an infile. Everything I run the file it keeps add an extra number to the sum. For example, I have 1 and 2 in the text file and it gives me 4
Replies To: Having problems with a while loop
#2
Re: Having problems with a while loop
Posted 10 February 2012 - 12:47 AM
I am having problems with trying to figure out how to make the output go to the data file.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int number, sum, count;
ifstream indata;
indata.open("data1.txt");
if(!indata)
{
cout<<"Can't open the file"<<endl;
return 1;
}
sum = 0;
while (indata)
{
count = 1;
while (count <= 5 && indata)
{
cin >> number;
sum = sum + number;
cin>>indata;
}
cout << sum / count <<endl;
}
#3
Re: Having problems with a while loop
Posted 10 February 2012 - 12:53 AM
indata is an input stream, and cin is an input stream, so line 19 makes no sense and should be deleted.
The problem is that line 22 lets you enter the loop when there is nothing left in the file but a newline character, and as a result the last value that remains in number from the previous iteration is added into sum a second time.
Modify your while loop as follows:
or better still:
This way, it won't enter the loop when there is no numerical data remaining to be read from the file.
The problem is that line 22 lets you enter the loop when there is nothing left in the file but a newline character, and as a result the last value that remains in number from the previous iteration is added into sum a second time.
Modify your while loop as follows:
while (indata >> number)
{
sum = sum + number;
}
or better still:
while (indata >> number)
{
sum += number;
}
This way, it won't enter the loop when there is no numerical data remaining to be read from the file.
This post has been edited by r.stiltskin: 10 February 2012 - 12:55 AM
#4
Re: Having problems with a while loop
Posted 10 February 2012 - 04:50 AM
ifstream indata;
indata.open("data1.txt");
Do you mean to be READING from data1.txt, or WRITING to it? Because you've opened the file for input only (ifstream is an INPUT stream), and you're attempting to read into it via cin.
Grrrr...
Merged duplicate topics. You've been here long enough that you should know not to create duplicate topics!
#5
Re: Having problems with a while loop
Posted 10 February 2012 - 07:44 AM
Its two different cases and I am confused now on what to do since you merge both problems. Lets try to tackle on thing at a time.
#6
Re: Having problems with a while loop
Posted 10 February 2012 - 08:38 AM
Quote
Lets try to tackle on thing at a time.
Okay, post your current code to one of your problems, along with any error messages you received when compiling. Then ask specific questions based on the code you posted.
Jim
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|