Adding an if-else statement, where?

I have most of a program written but need a little assistance for the last part of it.

Here is what it needs to accomplish
1. Prompt the user for a value. This value can be any rational number. Do
this until the value supplied is less than zero. (Accomplished)
2. Output the arithmetic mean of these values, not including the value less
than zero that ends the statement.
3. Output the geometric mean of these values, not including the value less
than zero that ends the statement.

I have all of these requirements coded, but I also need to have my program output "undefined" in the event that the first value input by the user is less than 0.


here is my current code block

# include <iostream>
# include <cmath>
# include <iomanip>

using namespace std;

int main()
{
const float okaynum = 0.0;
float usernum;
float sum;

float counter = 1;
cout << "Value 1: ";
float product=1.0;

while ( (cin >> usernum) && (usernum >= okaynum) )
{
counter += 1;
sum = sum + usernum;
cout << "Value " << counter << ": ";
product = product*usernum;
}

cout << setprecision(3) << "Arithmetic Mean: " << sum / (counter-1)
<< endl;

cout << "Geometric Mean: " << pow(product,(1 / (counter-1)));
cout << endl;

cout << "Arithmetic Mean: undefined" << endl;
cout << "Geometric Mean: undefined" << endl;

return 0;
}

I know I'm going to be using an if-else statement, but I am not sure where to put it. If I place it so that my while-loop is nested inside of the if statement it will require user input before my loop begins and then cause two user inputs to be required. If I place it in my while-loop the if statement will be repeated, which is not what I want. By the end of the program, I should have one output for Arithmetic Mean and one output for Geometric Mean.

Also, sorry about the uncolored text, I'm not sure how to get my text colored here?
Last edited on
the if-else should go after the while loop.
1
2
3
4
5
6
7
8
while(...){
   ...
}
if(counter-1 > 0){//by the way, why not start counter from 0?
   //print means
}else cout << "undefined";

return 0;


I'm not sure how to get my text colored here?
[code] [/code] tags. It's the first symbol in the format menu ( <> ).
EDIT: hamsterman mean you should select your codes and then press (<>) button at right to get you code colored.
Wow, thanks a bunch hamsterman. I really can't believe it was as simple an error as that, I was making my problem harder than it had to be. Also, thanks for the tip on code printing for the forums. Much appreciated.
Topic archived. No new replies allowed.