| | |
Loop troubles
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 31
Reputation:
Solved Threads: 0
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
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.
while(indicator = 'y') 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
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
![]() |
Similar Threads
- String to char* conversion (C++)
- Loop...without the loop (Java)
- GDI (C)
Other Threads in the C++ Forum
- Previous Thread: How to make these three files pass compiled?
- Next Thread: Convertings strings to numbers
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






