首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >10名学生输入C++ GPA Trunc

10名学生输入C++ GPA Trunc
EN

Stack Overflow用户
提问于 2016-02-22 02:58:04
回答 2查看 236关注 0票数 1

我可以输入第一个学生,但是,一旦我请求第二个学生的输入,它就不接受:getline(cin, s2) on to getline(cin,s10)。请您帮助我理解错误发生在哪里,或者为什么输出不遵循与s1/GPA1相同的格式?

这是我的代码:

代码语言:javascript
复制
#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);

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-22 03:14:41

您通常不希望直接用cin读取数字以供交互使用。它对换行符不太好--把换行符留给下一个条目。在本例中,您将得到person 2的空白名称。

试试这个:

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2016-02-22 03:13:11

当您混合cin和getline(cin,"string")时,您必须小心。Cin将忽略任何空空格或行,而getline则不会。所发生的是,在输入第一个学生的GPA后,换行符被存储在缓冲区中,并被转到自动输入的getline中。尝试在每个cin cin.ignore gpa之后使用cin.ignore()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35545100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档