cin.ignore() 忽略 1 个字符,但如果删除,将跳过其他 getline

问题描述 投票:0回答:0

所以我正在编写这段代码

struct std_info{
    char name[50];
    char std_no[50];
    char dep[50];
    char program[50];
    char yearLvl[50];
};
struct std_grades{
    double rph;
    double hci;
    double hciLab;
    double prog;
    double progLab;
    double pe;
    double contemp;
    double math;
    double purcom;
};
float average(double rph, double hci, double hciLab, double prog, double progLab,
                double pe, double contemp, double math, double purcom);
int main {
   std_info stdInfo[2];
   std_grades stdGrade[2];
    cout << "--------Enter your Information--------" << endl;
        for(int i = 0; i < 2; i++){
                cout << "Name: " ;
                    cin.ignore();
                    cin.getline(stdInfo[i].name, 50);
                cout << "Student Number: ";
                    cin.getline(stdInfo[i].std_no, 50);
                cout << "Department: ";
                    cin.getline(stdInfo[i].dep, 50);
                cout << "Program: ";
                    cin.getline(stdInfo[i].program, 50);
                cout << "Year Level: ";
                    cin.getline(stdInfo[i].yearLvl, 50);
                cout << endl;
    cout << "----Enter Grades to get your GPA----"  << endl <<endl;
                cout << "RPH: ";
                    cin >> stdGrade[i].rph;
                cout << "HCI: ";
                    cin >> stdGrade[i].hci;
                cout << "HCI LAB: ";
                    cin >> stdGrade[i].hciLab;
                cout << "Programming: ";
                    cin >> stdGrade[i].prog;
                cout << "Programming LAB: ";
                    cin >> stdGrade[i].progLab;
                cout << "PE: ";
                    cin >> stdGrade[i].pe;
                cout << "Contemporary World: "; 
                    cin >> stdGrade[i].contemp;
                cout << "Discrete Math: ";
                    cin >> stdGrade[i].math;
                cout << "PurCom: ";
                    cin >> stdGrade[i].purcom;
    cout << "-------------------------------------" << endl;      
        }
    cout << "----------Your Info and GWA----------" << endl;
        for(int i = 0; i < 2; i++){
            cout << "Name: " << stdInfo[i].name << endl;
            cout << "Student number: " << stdInfo[i].std_no << endl; 
            cout << "Department: " << stdInfo[i].dep << endl;
            cout << "Program: " << stdInfo[i].program << endl;
            cout << "Year Level: " << stdInfo[i].yearLvl << endl;

    cout << "-------------------------------------" << endl;  
     
            
        }
return 0; }

这段代码没有错误,但是当我双关“cin.ignore();”在“cin.getline(stdInfo[i].name, 50);”的顶部第一个字母将被忽略,但仅限于第一个循环。然而,当我删除“cin.ignore”时,第二个循环中的名称将被跳过

有 cin.ignore 时的输出

    ----------Your Info and GWA----------
Name: ohn smith
Student number: 23123
Department: CEA
Program: it
Year Level: 1st
GWA: 1.2
-------------------------------------
Name: kratos
Student number: 3821
Department: CEA
Program: it
Year Level: 2nd
GWA: 1.35
-------------------------------------

没有cin.ignore的输入

-------------------------------------
Name: Student Number: 12382
Department: cea
Program: IT
Year Level: 2nd

没有 cin.ignore 的输出

----------Your Info and GWA----------
Name: John Smith
Student number: 21943
Department: cea
Program: IT
Year Level: 1st
GWA: 1.75
-------------------------------------
Name: 
Student number: 12382
Department: cea
Program: IT
Year Level: 2nd
GWA: 3.65
struct cin getline
© www.soinside.com 2019 - 2024. All rights reserved.