finding average?

Apr 28, 2009 at 10:24am
// stand alone project
// finding an average from three numbers

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{

// displaying purpose

cout << "Average Calculator\n" ;


// collecting user input

int numberOne;
int numberTwo;
int numberThree;
int average;
int i;

cout << "Enter Number" ;
cin >> numberOne;

cout << "Enter_Number:" ;
cin >> numberTwo;

cout << "Enter_Number:" ;
cin >> numberThree;

// the math


average = (numberOne + numberTwo + numberThree) / 3);

cin >> average;
std::cin >> i;
return 0;

}
Apr 28, 2009 at 11:11am
Yes?
Apr 28, 2009 at 7:43pm
when i run the program it wont display the average? is that the correct way to use
cin << average; ?
Apr 28, 2009 at 7:46pm
you are making a mistake here.

average = (numberOne + numberTwo + numberThree) / 3);

and
 
 cout<<average;



Last edited on Apr 28, 2009 at 8:01pm
Apr 28, 2009 at 7:47pm
you need cout<<average; for output a value of varriable

read:
http://www.cplusplus.com/reference/iostream/cin/
http://www.cplusplus.com/reference/iostream/cout/
Last edited on Apr 28, 2009 at 7:48pm
Apr 28, 2009 at 8:09pm
Also notice that you average would be an int so it will have its value truncated, to have a more precise value you should use a double variable and a double division
Apr 28, 2009 at 8:40pm
thanks for all the help, yeah i named float average and couldn't figure out why i wasnt getting acurate numbers back, now i know.

-masiht, i found your mistake here

cout << "average"; no quotations...but im having trouble figuring out why (N1 + N2 + N3) / 3;
was it the last )) ???
Apr 28, 2009 at 9:04pm
you have extra ')' in expression average = (numberOne + numberTwo + numberThree) / 3);
Apr 28, 2009 at 9:30pm
1
2
3
4
-masiht, i found your mistake here

cout << "average"; no quotations...but im having trouble figuring out why (N1 + N2 + N3) / 3;
was it the last )) ???


average is type integer and it doesn't need any quotations.

you could do it like this
 
cout<<"average = "<<average;
Topic archived. No new replies allowed.