5 Replies - 3683 Views - Last Post: 10 February 2012 - 08:38 AM Rate Topic: -----

#1 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

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


#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;



Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with a while loop

#2 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

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;
  
}






Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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

Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

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!
Was This Post Helpful? 0
  • +
  • -

#5 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

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.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1