Loop troubles

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 31
Reputation: CRD is an unknown quantity at this point 
Solved Threads: 0
CRD CRD is offline Offline
Light Poster

Loop troubles

 
0
  #1
May 31st, 2007
this loop will not exit when n is pressed I sure would appreciate a clue as to how to exit a loop. here is the code.

code
// Ex3_12.cpp
// using a while loop to compute an average
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main( void )
{
double value = 0.0; // Value entered stored here
double sum = 0.0; // Total of value accumulated here
int i = 0; // Count of number of values
char indicator = 'y'; // Continue or not
while(indicator = 'y') // Loop as long as y is entered
{
cout << endl
<< "Enter a value: ";
cin >> value; // Read a value
++i; // Increment count
sum += value; // Add current input to total

cout << endl
<< "Do you want to enter another value enter n to end)? ";
cin >> indicator; // Read indicator
}
cout << endl
<< "The average of the " << i
<<" value you entered is " << sum/i << "."
<< endl;
return 0;
}

Thanks Dick
Last edited by CRD; May 31st, 2007 at 9:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Loop troubles

 
0
  #2
May 31st, 2007
while(indicator = 'y')
You want == for comparison.
Last edited by dwks; May 31st, 2007 at 9:42 pm.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Loop troubles

 
0
  #3
Jun 3rd, 2007
Plus you need to convert the character to lowercase so as not to force the user to enter 'y' in lower case only.

while(tolower(continue) == 'y')
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC