-
why is this not working
Hello
I have this code
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
using namespace std ;
int main()
{
// ask for and read the student's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
++count;
sum += x;
}
while (count == 0 )
{
cout <<"You haven't entered any values" << endl;
cout << endl ;
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
++count;
sum += x;
}
}
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
As the user don't put in any numbers they get a warning and get a chance to do it again.
But here the warning is displayed without giving the user a moment to do the input again.
Why is this happening and how can I solve this ?
Roelof
-
Most likely because you have crap left in the input buffer. Clear it out with cin.ignore.
-
Hoi Elysia.
Something like this :
Code:
while (count == 0 )
{
cin.ingnore (1000, '/n') ;
cout <<"You haven't entered any values" << endl;
cout << endl ;
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
++count;
sum += x;
}
Roelof
-
You should probably only ignore after something failed ;-). Furthermore, '/n' isn't legal, I think you meant '\n'.
-
Did you try compiling that?
-
Yes.
I tried it and I mean '\n'
It compiles but still a infentive loop.
EVOEx : when count == 0 then it fails because nothing has be inputted.
Roelof
-
Then post your real code, instead of something you typed up full of errors.
-
oke,
Here the code :
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
using namespace std ;
int main()
{
// ask for and read the student's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
++count;
sum += x;
}
while (count == 0 )
{
cin.ignore (1000, '\n');
cout <<"You haven't entered any values" << endl;
cout << endl ;
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
++count;
sum += x;
}
}
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
Roelof
-
How about:
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
using namespace std ;
int main()
{
// ask for and read the student's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
cin.ignore (1000);
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
cin.ignore (1000);
++count;
sum += x;
}
while (count == 0 )
{
cin.ignore (1000);
cout <<"You haven't entered any values" << endl;
cout << endl ;
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
cin.ignore (1000);
++count;
sum += x;
}
}
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
Does this work?
-
Hoi Elysia,
It don't work.
Still only the message that there is no input and that the whole time.
Roelof
Edit 1 :
Maybe use a vector to check for input and after no input use a return 1.
The same as the example in the book.
-
Bah. If no one helps you in the meantime, I'll take a look when I get home.
-
Oke,
Don't hurry.
I think it will be 2020 when I have the book ready.
Roelof
-
O_o
Depending on what you want to do, the logic of the code is broken.
In this example, I've shown a possible fix and noted part of the problem.
Soma
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
using namespace std ;
int main()
{
// ask for and read the student's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
do {
while (cin >> x)
{
++count;
sum += x;
}
if(!cin.eof())
{
cin.clear();
cin.ignore(1000, '\n');
cout << "not a double value\n";
}
} while(!cin.eof());
// this will not work. the last block runs until EOF
// EOF is a state that can't logically be ignored
// or cleared as the next read would just EOF again
/*while (count == 0 )
{
cin.ignore (1000);
cout <<"You haven't entered any values" << endl;
cout << endl ;
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (cin >> x)
{
cin.ignore (1000);
++count;
sum += x;
}
}*/
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
-
Oke,
I understand but not complety.
But why not cin .eof() when displaying the error message
Must that be cin.eof.
And if a user enters one value then it's good and the user may use EOF otherwise the loop will run forever.
Roelof
Edit 1 : I tried it and it's not working. When you now use a EOF then the programm does not respond anymore
-
I got it working with this:
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
using namespace std ;
int main()
{
// ask for and read the student's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
while (count == 0 )
{
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by -1: ";
for (;;)
{
cin >> x;
if (x == -1)
break;
++count;
sum += x;
}
if (count == 0)
{
cout <<"You haven't entered any values" << endl;
cout << endl ;
}
}
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}