(grade=cin.get())!=EOF与grade=cin.get()!=EOF有什么区别_钱汇娱乐城

(grade=cin.get())!=EOF与grade=cin.get()!=EOF有什么区别

原题目:(grade=cin.get())!=EOF与grade=cin.get()!=EOF有什么区别
分类:编程开辟 > 最初更新工夫:【2017-08-02 09:21:17】
题目增补:

//gradebook.h

#include<string>

using namespace std;

class gradebook

{

public:

gradebook(string name);

void setcoursename(string name);

string getcoursename();

void displaymessage();

void inputgrades();

void displaygradereport();

private:

string coursename;

int acount;

int bcount;

int ccount;

int dcount;

int fcount;

};

//gradebook.cpp

#include<iostream>

#include"gradebook.h"

using namespace std;

gradebook::gradebook(string name)

{

setcoursename(name);

acount=0;

bcount=0;

ccount=0;

dcount=0;

fcount=0;

}

void gradebook::setcoursename(string name)

{

string nameofcourse;

if(name.length()<=25)

coursename=name;

if(name.length()>25)

{

getline(cin,nameofcourse);

if(nameofcourse.length()<=25)

coursename=nameofcourse;

else

coursename=nameofcourse.substr(0,25);

}

}

string gradebook::getcoursename(){return coursename;}

void gradebook::displaymessage(){cout<<"welcome to gradebook for"<<getcoursename()<<"!"<<endl;}

void gradebook::inputgrades ()

{

int grade;

cout<<"Enter the letter grade."<<endl

<<"Enter the EOF character to end input."<<endl;

while(grade=cin.get ()!=EOF)

{

switch(grade)

{

case 'A':

case 'a':

acount++;

break;

case 'B':

case 'b':

bcount++;

break;

case 'C':

case 'c':

ccount++;

break;

case 'D':

case 'd':

dcount++;

break;

case 'F':

case'f':

fcount++;

break;

case '\t':

case ' ':

case '\n':

break;

default:

cout<<"Icorrect letter grade entered."<<endl;

cout<<"Enter a new grade:"<<endl;

break;

}

}

}

void gradebook::displaygradereport ()

{

cout<<"\nNumber of students who receive each letter grade:\n"

<<"A:\n"<<acount

<<"\nB:\n"<<bcount

<<"\nC:\n"<<ccount

<<"\nD\n"<<dcount

<<"\nF\n"<<fcount<<endl;

}

//test.cpp

#include <iostream>

#include "gradebook.h"

int main()

{

gradebook mygradebook("c++");

mygradebook.displaymessage();

mygradebook.inputgrades();

mygradebook.displaygradereport();

system("pause");

return 0;

}

在这个语境中,grade=cin.get()!=EOF会呈现输出任何字符回车之后运转后果都是

Icorrect letter grade entered.

Enter a new grade:

而(grade=cin.get())!=EOF则不会

最佳答案

(grade=cin.get())!=EOF 是从控制台失掉一个字符,并判别它能否为EOF

grade=cin.get()!=EOF 是先判控制台能否为EOF,然后将这个后果赋结grade

后果完全差别

而前者才是准确的写法

这个与C++运算符优先级有关的

最佳答案由网友  whoami1978  提供
通告: 为呼应国度净网举动,局部内容曾经删除,感激网友了解。
11

分享到:

其他答复

暂无别的答复!

    引荐
  • (grade=cin.get())!=EOF与grade=cin.get()!=EOF有什么区别