Unformat input, Format output

Can someone help me out, if possible. (thanks)
I've copy/pasted my program below. the program is written how my instructor wants me to write this. i am in an intro to c++ class. so all were learning is fairly basic at this point. so hmm...

When I input 3 numerical values, it averages 1 output value (YAY), which is the purpose of the program to begin with.

MY ISSUE (sad face): i would like the user to input values (with or without decimal points such as 3 or 3.14) and have an ACCURATE output averaged to having 2 decimal digits.
- so far, my output has 2 decimal digits (YAY).
- however, when the average is calculated, the input gets rounded to the nearest whole number.

- i hope u guys can get a better understanding with the example below.


EXAMPLE: (these are hypothetical values, user can input whatever)
what happens -
input: 1.23
input: 4.56
input: 7.89
output: 4.00 <-- my problem (4 is the avg of 1+4+7)

what i want to happen (and cant figure out) -
input: 1.23
input: 4.56
input: 7.89
output: 4.56 <-- which is the right average if calculated properly


ANYWAY, any suggestion would help. THANKS A BUNCHES
..kRistine..
__________________________________________


#include <iostream>
#include <iomanip>
using namespace std;

double calcAverage(int a, int b, int c)
{
double avg = 0.0;
avg = (a + b + c) / 3.0;
return avg;
} // calcAverage

int main()
{

int x;
cout << "Enter any numerical value: ";
cin >> x;
cin.ignore(1000, 10);

int y;
cout << "Enter any numerical value: ";
cin >> y;
cin.ignore(1000, 10);

int z;
cout << "Enter any numerical value: ";
cin >> z;
cin.ignore(1000, 10);

double t = calcAverage(x, y, z);
cout << " " << endl;
cout << fixed << setprecision(2) << "The average is: " << t << endl;


cin.ignore();
cin.get();
return 0;
} // main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{

int x; //<------------------------Problem here - wrong type
cout << "Enter any numerical value: ";
cin >> x;
cin.ignore(1000, 10);

int y;//<<--------------------problem here - wrong type
cout << "Enter any numerical value: ";
cin >> y;
cin.ignore(1000, 10);

int z; //<-----------------------------problem here - wrong type
cout << "Enter any numerical value: ";
cin >> z;
cin.ignore(1000, 10);

double t = calcAverage(x, y, z);
cout << " " << endl;
cout << fixed << setprecision(2) << "The average is: " << t << endl;


cin.ignore();
cin.get();
return 0;
} // main 

i'm sorry. is possible if you expand what the problem is exactly?

did i use wrong variables?
i experimented and changed "int" to "double" and it still gave me the same problem.

thank you for your time and suggestions. i really REALLY appreciate this.
like i said, im super beginner with this. like beyond super beginner.

thanks again,
...kRistine...
Actually I was too keen and forgot to mention, that even if you change x, y and z to double, it still wont work,
because the average function takes integers as parameters, so all the good work of getting
the double values from the user is lost when they get truncated to integer to pass to the calcAverage function.
Last edited on
dude, thanks a million.

i changed ALL of my "int's" to "double's" and it worked.
i absolutely appreciate the help; you have no idea how much that helped me out.
i would have never considered that. at all.

ahh the headaches of programming. =P

thank you.
...kristine...
Topic archived. No new replies allowed.