I'm fairly new to C++ so please bear with me. I've tried cin.get() cin.clear() etc but nothing works so far. I pressed ctrl+Z to end the while loop. Don't know if it has anything to do with it.
Thank you guys!
#include <iostream>
#include <string>
#include <iomanip>
#include <ios>
usingnamespace std;
int main()
{
cout<<"Please enter student's name"<<endl;
string name;
cin>>name;
cout<<"Please enter your midterm and final exam grades"<<endl;
double midterm,final;
cin>>midterm>>final;
cout<<"Please enter all your HW grandes below"<<endl;
int count=0;
double sum=0;
double x; //a variable to read as one HW grade
while (cin>>x)
{
sum=sum+x;
count++;
}
streamsize prec=cout.precision();
cout<<"Your final semester grade is "<<setprecision(3)<<0.2*midterm+0.4*final+0.4*sum/count<<setprecision(prec)<<endl;
//cin.clear();
//cin.sync();
cout<<"enter any number to exit"<<endl;
int i;
cin>>i;
return 0;
}
Sorry I didn't make my question very clear. i didn't get an error. I don't know how to prevent the console from closing down after execution. The console closes down right after I entered ctrl+Z+enter (to satisfy the 'false' condition for the while loop i think? ) so I didn't even get a chance to see if the last output is correct.
I'm tempted to report this entire thread. "Console Closing Down" is literally the title of the second sticky thread on this board. It answers this exact question with discussion of methods that work and why some methods are better than others.
@ OP: When you ping a remote computer or a website, does ping.exe try to keep the console open for you so that you can read the result? How about calcs.exe? How about any of the console tools in SysInternals? Why do you think that none of these console applications bother to do what you are trying to do here? I'm being serious here.