Program Help.
Nov 18, 2011 at 3:27am UTC
Can someone please tell me why this doesn't work?
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include<iostream>
#include<ios>
#include<algorithm>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
int main(){
cout << "Enter the first student's name: " ;
string name1;
cin >> name1;
cout << "Enter the second studesnt's name: " ;
string name2;
cin >> name2;
cout << "Enter the 10 homework grade for the first student: " ;
vector<double > stu1;
vector<double > stu2;
double x;
double y;
double sum1 = 0;
double sum2 = 0;
while (cin >> x && y != 10){
stu1.push_back(x);
y++;
sum1 += x;
}
cout << "Enter the homeworks grades for the second student: " ;
y = 0;
while (cin >> x && y != 10){
stu2.push_back(x);
y++;
sum2 += x;
}
streamsize prec = cout.precision();
setprecision(3);
cout << name1 << "'s average is: " << sum1 / 10 << endl;
cout << name2 << "'s average is: " << sum2 / 10 << endl;
setprecision(prec);
return 0;
}
It's suppose to stop letting you input numbers after the 10th, but it doesn't.
Nov 18, 2011 at 3:33am UTC
(1) You didn't initialize y
before asking for the grades for the first student.
(2) y is a double
so the y != 10
condition will probably always be false thanks to the inaccuracy of floating point. I would recommend you make y an int
instead.
Nov 18, 2011 at 3:54am UTC
Thanks, that went completely over my head.
Nov 18, 2011 at 8:10pm UTC
Replace
double y;
with
int y = 0;
Topic archived. No new replies allowed.