gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:25 25 4
gpt4 key购买 nike

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

这是我的代码:

#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);

}

最佳答案

您通常不希望直接使用 cin 读取数字以供交互使用。它不能很好地处理换行符——将换行符留给下一个条目。在这种情况下,您将获得第 2 个人的空白名称。

试试这个:

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;

关于10 名学生输入的 C++ GPA Trunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com