我能够输入第一个学生,但是,一旦我请求第二个学生的输入,它就不接受: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);
}
答案 0 :(得分:0)
当你混合使用cin和getline时,你必须要小心(cin,&#34; string&#34;)。 Cin将忽略任何空格或行,而getline则不会。发生的事情是,在您输入第一个学生的GPA之后,换行符存储在缓冲区中并被转移到自动输入的getline。尝试在每个cin&gt;&gt;之后使用cin.ignore() GPA。
答案 1 :(得分: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;