简体   繁体   English

10位学生输入的C ++ GPA Trunc

[英]C++ GPA Trunc for 10 Students Input

I am able to enter the first student, however, once I request the second student's input it does not accept the: getline(cin, s2) on to getline(cin,s10) . 我能够输入第一个学生,但是,一旦我请求第二个学生的输入,它就不会接受getline(cin,s10)上的getline(cin, s2) getline(cin,s10) Could you please help me understand where the mistake is made or why the output is not following the same format as s1 / GPA1 ? 您能否帮助我理解错误的出处或为什么输出的格式与s1 / GPA1

Here is my code: 这是我的代码:

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

int main()
{
    //Welcome 
    cout << "Welcome to your personal GPA Calculator! \n" << endl;

    float GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA8, GPA9, GPA10 = 0;
    string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;



    //Requesting User Input
    cout << "Student One| Please enter your Last Name, First Name: " << endl;
    getline(cin, s1);
    cout << "What is your current GPA: " << endl;
    cin >> GPA1;
    cout << endl;

    cout << "Student Two| Please enter your Last Name, First Name: " << endl;
    getline(cin, s2); 
    cout << "What is your current GPA: " << endl;
    cin >> GPA2;
    cout << endl;

    cout << "Student Three| Please enter your Last Name, First Name: " << endl;
    getline(cin, s3); 
    cout << "What is your current GPA: " << endl;
    cin >> GPA3;
    cout << endl;

    cout << "Student Four| Please enter your Last Name, First Name: " << endl;
    getline(cin, s4);
    cout << "What is your current GPA: " << endl;
    cin >> GPA4;
    cout << endl;

    cout << "Student Five| Please enter your Last Name, First: " << endl;
    getline(cin, s5);
    cout << "What is your current GPA: " << endl;
    cin >> GPA5;
    cout << endl;

    cout << "Student Six| Please enter your Last Name, First Name: " << endl;
    getline(cin, s6);
    cout << "What is your current GPA: " << endl;
    cin >> GPA6;
    cout << endl;

    cout << "Student Seven| Please enter your Last Name, First Name: " << endl;
    getline(cin, s7);
    cout << "What is your current GPA: " << endl;
    cin >> GPA7;
    cout << endl;

    cout << "Student Eight| Please enter your Last Name, First Name: " << endl;
    getline(cin, s8);
    cout << "What is your current GPA: " << endl;
    cin >> GPA8;
    cout << endl;

    cout << "Student Nine| Please enter your Last Name, First Name: " << endl;
    getline(cin, s9);
    cout << "What is your current GPA: " << endl;
    cin >> GPA9;
    cout << endl;

    cout << "Student Ten| Please enter your Last Name, First Name: " << endl;
    getline(cin, s10);
    cout << "What is your current GPA: " << endl;
    cin >> GPA10;
    cout << endl;

    //Store in Tabular Format
    cout << ("Student Name| \t\t |Student GPA Value \n");
    cout << ("____________________________________________\n");
    std::cout << s1 << "\t\t  " << std::setprecision(2) << std::fixed << GPA1 << endl;
    std::cout << s2 << "\t\t  " << std::setprecision(2) << std::fixed << GPA2 << endl;
    std::cout << s3 << "\t\t  " << std::setprecision(2) << std::fixed << GPA3 << endl;
    std::cout << s4 << "\t\t  " << std::setprecision(2) << std::fixed << GPA4 << endl;
    std::cout << s5 << "\t\t  " << std::setprecision(2) << std::fixed << GPA5 << endl;
    std::cout << s6 << "\t\t  " << std::setprecision(2) << std::fixed << GPA6 << endl;
    std::cout << s7 << "\t\t  " << std::setprecision(2) << std::fixed << GPA7 << endl;
    std::cout << s8 << "\t\t  " << std::setprecision(2) << std::fixed << GPA8 << endl;
    std::cout << s9 << "\t\t  " << std::setprecision(2) << std::fixed << GPA9 << endl;
    std::cout << s10 << "\t\t  " << std::setprecision(2) << std::fixed << GPA10 << endl;

    return (0);

}

You have to be careful when you mix cin and getline(cin, "string"). 混合使用cin和getline(cin,“ string”)时,必须小心。 Cin will ignore any empty spaces or lines while getline will not. Cin将忽略任何空白或行,而getline则不会。 What's happening is that after you input the first student's GPA, a newline character is stored in the buffer and is carried over to getline where it's entered automatically. 发生的情况是,在输入第一个学生的GPA后,换行符将存储在缓冲区中,并结转到getline中,并在其中自动输入该换行符。 Try using cin.ignore() after every cin >> gpa. 尝试在每个cin >> gpa之后使用cin.ignore()。

You generally don't want to read numbers directly with cin for interactive use. 通常,您不希望直接通过cin读取数字以进行交互使用。 It doesn't play well with newlines -- leaving the newline for the next entry. 它不能与换行符配合使用-将换行符留作下一项。 In this case, you get a blank name for person 2. 在这种情况下,您将获得人2的空白名称。

Try this: 尝试这个:

string temp;


//Requesting User Input
cout << "Student One| Please enter your Last Name, First Name: " << endl;
getline(cin, s1);
cout << "What is your current GPA: " << endl;

getline(cin, temp);
GPA1 = strtof(temp.c_str(), 0);
cout << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM