| | |
while loop in c++ help needed
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2004
Posts: 1
Reputation:
Solved Threads: 0
Please help me get the minimum in the following problem. i get everything else but the min. take a look at it.
//**************************************************
// Description : This program asks a user for five real numbers, then outputs
// the number of data entries entered, the total of the numbers
// entered, the mean average of the numbers entered, the minimum
// and maximum value entered. it will also format the output to
// one decimal place.
//**************************************************
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
double num, sum = 0, avg, max, min;
int count = 0; // loop counter variable
while (count < 5)
{
cout <<"Enter number " << count ++ << ":";
cin >> num;
sum = sum + num;
if (count == 0)
{
max = num;
min = num;
}
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
}
cout <<"The number of values entered is " << count << endl;
cout <<"The sum of values entered is " << sum << endl;
avg = sum / count;
cout <<"The average of values entered is " << avg << endl;
cout <<"The maximum value entered is " << max << endl;
cout <<"The minimum of value entered is " << min << endl;
system("PAUSE");
return 0;
}
//**************************************************
// Description : This program asks a user for five real numbers, then outputs
// the number of data entries entered, the total of the numbers
// entered, the mean average of the numbers entered, the minimum
// and maximum value entered. it will also format the output to
// one decimal place.
//**************************************************
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
double num, sum = 0, avg, max, min;
int count = 0; // loop counter variable
while (count < 5)
{
cout <<"Enter number " << count ++ << ":";
cin >> num;
sum = sum + num;
if (count == 0)
{
max = num;
min = num;
}
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
}
cout <<"The number of values entered is " << count << endl;
cout <<"The sum of values entered is " << sum << endl;
avg = sum / count;
cout <<"The average of values entered is " << avg << endl;
cout <<"The maximum value entered is " << max << endl;
cout <<"The minimum of value entered is " << min << endl;
system("PAUSE");
return 0;
}
Last edited by cscgal; Mar 7th, 2004 at 12:58 am.
In your while loop, you're telling it to loop five times and keeping track with variable count. Once you first enter the while loop, you're immediately incrementing it with the stament count++. Even if you're using this with a cout as output, it still increments it. What does this mean? It means that once it enter, it equals 1, and your statement of
will never run. All you have to do is take the "++" from the "count" variable. You then increment this variable before another loop begins. Like this:
Also, don't forget to initialize your variables always. In this case, you'd want to set them to zero, as I did.
C++ Syntax (Toggle Plain Text)
if (count == 0) { max = num; min = num; }
will never run. All you have to do is take the "++" from the "count" variable. You then increment this variable before another loop begins. Like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdlib.h> using namespace std; int main(void) { double num, sum = 0, avg=0, max=0, min=0; int count = 0; // loop counter variable while (count < 5) { cout <<"Enter number " << count << ":"; cin >> num; sum = sum + num; if (count == 0) { max = num; min = num; } if (num > max) { max = num; } if (num < min) { min = num; } count++; } cout <<"The number of values entered is " << count << endl; cout <<"The sum of values entered is " << sum << endl; avg = sum / count; cout <<"The average of values entered is " << avg << endl; cout <<"The maximum value entered is " << max << endl; cout <<"The minimum of value entered is " << min << endl; return 0; }
Also, don't forget to initialize your variables always. In this case, you'd want to set them to zero, as I did.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
![]() |
Similar Threads
- x86 NASM loop (Assembly)
- for loop help needed (JavaScript / DHTML / AJAX)
- quick question (C++)
- Java math program (Java)
- 2-D array (C++)
- Read unlimited no. of people. (C)
Other Threads in the C++ Forum
- Previous Thread: Cannot get switch to execute
- Next Thread: newbies guide to c++
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop 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 news number numbertoword output parameter 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 visual visualstudio win32 windows winsock wordfrequency wxwidgets






