为什么程序在调用函数时跳过接受输入
Why does the program skip taking input when function is called?
我正在编写一个简单的C++程序,该程序获取学生信息及其在不同科目中的分数。
#ifndef STUDENT_H_
#define STUDENT_H_
#include<string>
class Student {
private:
std::string name;
std::string dept;
unsigned char age;
std::string usn;
public:
Student();
void getinfo();
void display();
virtual ~Student();
};
class Score:public Student {
private:
int math;
int sci;
int chem;
int english;
int SS;
public:
void marks();
void dispscore();
Score();
};
#endif /* STUDENT_H_ */
以上是头文件。
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student()
{
age = 0x00;
}
Student::~Student()
{
}
Score::Score()
{
math = 0x00;
sci = 0x00;
chem = 0x00;
SS = 0x00;
english = 0x00;
}
void Student::getinfo()
{
cout<<"Enter the Students Name: "<<endl;
getline(cin,name);
cout<<"Enter Department--> "<<endl;
getline(cin,dept);
cout<<"Enter USN--> "<<endl;
getline(cin,usn);
cout<<"Enter Age--> "<<endl;
cin>>age;
}
void Student::display()
{
cout<<name<<endl;
cout<<dept<<endl;
cout<<usn<<endl;
cout<<age<<endl;
}
void Score::marks()
{
cout<<"Enter Math Score: "<<endl;
cin>>math;
cout<<"Enter Science Score: "<<endl;
cin>>sci;
cout<<"Enter Chemistry Score: "<<endl;
cin>>chem;
cout<<"Enter English Score: "<<endl;
cin>>english;
cout<<"Enter the Social Studies Score: "<<endl;
cin>>SS;
}
void Score::dispscore()
{
cout<< math << endl;
cout<<sci<<endl;
cout<<chem<<endl;
cout<<english<<endl;
cout<<SS<<endl;
}
int main()
{
Score s;
s.getinfo();
s.marks();
s.display();
s.dispscore();
}
以上是 cpp 文件。编译后,我得到如下所示的输出,并面临以下问题,它们是:1.跳过数学的输入分数,直接从科学的输入分数开始,2. 输入的年龄是 23 岁,但是在控制台中,它不会将其显示为"23",而是在一行显示为 2,下一行显示为 3,如输出所示 ->发生这种情况是因为我按回车
键吗?Enter the Students Name:
Smith Diaz
Enter Department-->
ECE
Enter USN-->
4So08ec112
Enter Age-->
23
Enter Math Score:
Enter Science Score:
77
Enter Chemistry Score:
66
Enter English Score:
89
Enter the Social Studies Score:
57
Smith Diaz
ECE
4So08ec112
2
3
77
66
89
57
age
的类型为 unsigned char
,因此cin
只读一个字符并将其存储在那里。
尝试将成员age
的类型更改为 int
。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 无法在windows控制台中为C++程序提供必要的输入
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我的程序没有从文件中读取并输入数据
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 编写一个不断要求用户输入的程序
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- 如何将输入C 程序的平均值输入
- Sudoku输入程序跳过提示
- 无法将文件输入 C++ 程序
- 简单的c++输入程序
- 简单输入程序崩溃
- 用户用于输入程序设置的文件约定