Im having a very hard time understanding this (cin >> x). Im just getting into c++ and im reading accelerated c++. In the book its a program on making a student grade chart this is the code.
while (cin >> x)
This is the code im having a extremely hard time understanding. So is x going into the input of cin?? if so what is cin this is the full code..
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//ask for and read the student's name
cout << "Please enter your 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 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 to 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;
}
It runs perfectly because I copied it from the book I just don't understand what the hell does that mean. I just don't get it im assuming that cin is the last output ,but what >> does. Is it like cout << or what it is really confusing me right now please respond.
The line while(cin >> x) will try to extract a value into the variable until an error occurs. This error is triggered by either eof() or trying to insert something that the variable can't accommodate. For example trying to enter a character for a numeric entry.
The C++ streams can return a Boolean type of value that indicates success or failure.