C++ 为什么一些字符串变量没有从结构中获取输入到void函数?

C++ 为什么一些字符串变量没有从结构中获取输入到void函数?,c++,string,c++11,structure,C++,String,C++11,Structure,在下面的代码中,有些字符串正在接受输入,但有些只是跳过。我检查了所有东西,没有发现任何东西。如果你能让我知道我哪里出了问题,那将是非常理想的。我需要提到的是,我在这个问题上还有很多工作要做,这就是我为什么要创建函数的原因 预期产量 :::::::::::: 从下面选择选项 :::::::::::: 1.对于添加新学生 2.增加新教师 3.添加新的通知 一, 输入学生的名字:Marry 输入学生的姓氏:lisa 输入学生卷号:245 进入学生部分:C 输入学生年份:1 进入学生学期:2 输入学生所

在下面的代码中,有些字符串正在接受输入,但有些只是跳过。我检查了所有东西,没有发现任何东西。如果你能让我知道我哪里出了问题,那将是非常理想的。我需要提到的是,我在这个问题上还有很多工作要做,这就是我为什么要创建函数的原因

预期产量

:::::::::::: 从下面选择选项 ::::::::::::

1.对于添加新学生

2.增加新教师

3.添加新的通知

一,

输入学生的名字:Marry

输入学生的姓氏:lisa

输入学生卷号:245

进入学生部分:C

输入学生年份:1

进入学生学期:2

输入学生所在部门:IR

输入学生的电子邮件:lisa2@hotmail.com

输入学生电话:15648955

输入学生地址:2/A XYZ街

现在输出

:::::::::::: 从下面选择选项 ::::::::::::

1.对于添加新学生

2.增加新教师

3.添加新的通知

一,

输入学生的名字:输入学生的姓氏:lisa

输入学生卷号:245

输入学生部分:输入学生年份:1

进入学生学期:2

输入学生的部门:输入学生的电子邮件:lisa2@hotmail.com

输入学生电话:15648955

输入学生地址:

进程返回0 0x0执行时间:52.725秒 按任意键继续

#include <iostream>
#include<string>
#include<iomanip>
using namespace std;

struct student{
    string firstName;
    string lastName;
    int Roll;
    string Section;
    int Year;
    int Semester;
    string Department;
    string Email;
    int Phone;
    string Address;
};

void student_part(void){
    struct student stu;
    cout<<"Enter student's first name :";
    getline(cin,stu.firstName);
    cout<<"Enter student's last name :";
    getline(cin,stu.lastName);
    cout<<"Enter student's roll no :";
    cin>>stu.Roll;
    cout<<"Enter student's section :";
    getline(cin,stu.Section);
    cout<<"Enter student's year :";
    cin>>stu.Year;
    cout<<"Enter student's semester :";
    cin>>stu.Semester;
    cout<<"Enter student's department :";
    getline(cin,stu.Department);
    cout<<"Enter student's email :";
    getline(cin,stu.Email);
    cout<<"Enter student's phone :";
    cin>>stu.Phone;
    cout<<"Enter student's address :";
    getline(cin,stu.Address);
}



void add_info(void){
    int choice;
    cout<<"::::::::::::::::::::::::::"<<endl;
    cout<<" Choose Option From Below"<<endl;
    cout<<"::::::::::::::::::::::::::"<<endl;
    cout<<"\n1.for add new student"<<endl;
    cout<<"2.for add new teacher"<<endl;
    cout<<"3.for add new notice"<<endl;
    cin>>choice;
    if(choice==1){
        student_part();
    }
}


int main()
{
    add_info();
}

正在从命令行获取一个整数,因此它不会读取getline之后立即读取的\n结尾字符


在getline之前,写fgetcstdin来阅读最新版本。

另一种可能是将所有getline功能更改为cin>>stu。。。电话


发生错误的原因是cin方法未将\n从输入缓冲区中取出。以下getLine将剩余的\n从输入缓冲区中取出并立即终止。

请缩进您的代码以提高可读性。@ThomasSablik否它与我需要的东西不匹配复制中描述的解决方案是:调用std::cin.ignorestd::numeric_limits::max,“\n”毕竟cin>>。。。;电话号码也不是问题的一部分,电话号码也不是问题的一部分,那么你就不应该把它贴在你的邮箱里
cin>>stu.Phone;