1

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). Could you please help me understand where the mistake is made or why the output is not following the same format as 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);

}
  • It's difficult to answer without knowing your input, but just at a glance I feel that it's not a good idea to mix `getline` calls with formatted stream input, e.g., `cin >> GPA1`. Get all of your data line by line or item by item. – Logicrat Feb 22 '16 at 03:11
  • Reference: https://marcoarena.wordpress.com/2015/11/15/pay-attention-to-unformatted-nature-of-getline/ –  Feb 22 '16 at 03:12
  • 2
    Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) –  Feb 22 '16 at 03:14
  • Perhaps the compiler noticed that it was intended to be a "*personal* GPA calculator"...and felt students Two thru Ten should keep their hands off of it after student One had used it. – HostileFork says dont trust SE Feb 22 '16 at 03:19

2 Answers2

0

You have to be careful when you mix cin and getline(cin, "string"). Cin will ignore any empty spaces or lines while getline will not. 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. Try using cin.ignore() after every cin >> gpa.

0

You generally don't want to read numbers directly with cin for interactive use. 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.

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;
Rob L
  • 2,351
  • 13
  • 23