Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

包含类的C++程序如何做到换行输入?

题目描述:包含两个成员函数的C++程序

题目来源:谭浩强C++程序设计第三版

相关代码

//Author:Huang Zihan
//Function:The C++ programs containing classes
#include <iostream>
using namespace std;
class Student
{
   private:
       int num;
       int score;
   public:
   void setdata()
   {
       cin>>num;
       cin>>score;
   }
   void display()
   {
       cout<<"num="<<num<<endl;
       cout<<"score="<<score<<endl;
   };
};

Student stud1,stud2;

int main()
{
   stud1.setdata();
   stud2.setdata();
   stud1.display();
   stud2.display();
   return 0;

}

显示结果

图片.png

期待结果

按照这个程序,输入stud1.setdata();的数据之后应该要输入stud2.setdata();,但是一按下enter键就直接输出了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你输入的变量都是 int ,但是却输入了一个 98.5 ,于是 cin 进入了一个错误状态,罢工了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...