Quantcast
Channel: Active questions tagged windows-subsystem-for-linux - Ask Ubuntu
Viewing all articles
Browse latest Browse all 2796

Pressing EOF (Ctrl+D) exits from the while loop

$
0
0
#include<iostream>#include<vector>#include<ios>#include<iomanip>#include<stdexcept>#include<algorithm>#include<string>using namespace std;struct student{        string name;        double major;        double minor;        vector<double> hw;};istream& input(istream& in, struct student& s){        cout<<"Enter your name, major and minor marks respectively: ";        in>>s.name>>s.major>>s.minor;        //cout<<"hey"<<s.name<<s.major<<s.minor<<endl;        if(in)        {                cout<<endl<<"Now enter your homework marks respectively: ";                s.hw.clear();                double x;                while(in>>x)                        s.hw.push_back(x);                in.clear();        }        return in;}double grade (struct student& s){        vector<double>::size_type t = s.hw.size();        if(t!=0){                sort(s.hw.begin(),s.hw.end());                double med = (t%2==0)?(s.hw[t/2]+s.hw[1+t/2])/2:s.hw[t/2];                return .4*s.major+ .2*s.minor + .4*med;        }else{throw domain_error("The student does not enter any homework.");}}int main(){        vector<struct student> students;        struct student sl;        while(input(cin, sl))        {                students.push_back(sl);        }        cout<<endl;        for(int i=0;i<students.size();i++)        {                struct student s=students[i];                try{                double t=grade(s);                streamsize l = cout.precision();                cout<<"Name = "<<s.name<<"  final grade = "<<setprecision(3)<<t<<setprecision(l)<<endl;        }catch(domain_error){                cout<<s.name<<" you must enter your homework's marks."<<endl<<"Please try again.";                return 1;}        }        return 0;}

I run the code after entering the data in the homework vector. When I enter the eof (Ctrl+D)it comes to the input function but does not take any input so it exits the while loop in main().

output

Enter your name, major and minor marks respectively: jack 39 49Now enter your homework marks respectively: 20 10 19 Enter your name, major and minor marks respectively: Name = jack  final grade = 33

What should I do such that It will take another student data? Is there any problem with the Ctrl+D?

Note: I am using Ubuntu in WSL.


Viewing all articles
Browse latest Browse all 2796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>