#include <iostream>
using namespace std;
int main(){
//set variables
string name1;string name2;string name3;string n1;string n2;string n3;
int age1; int age2; int age3;
//get names and ages
cout << "Who Is The First Student?"<< endl;
getline(cin, name1); //name 1
cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1
cout << "Who Is The Second Student?"<< endl;
getline(cin, name2); //name 2
cout << "What is " << name2<< "'s Age?"<< endl;
cin >> age2; //age 2
cout << "Who Is The Third Student?"<< endl;
getline(cin, name3); // name 3
cout << "What is " << name3<< "'s Age?"<< endl;
cin >> age3; //age 3
// gets modified names
n1 = name1.substr(2, name1.size() -3);
n2 = name2.substr(2, name2.size() -3);
n3 = name3.substr(2, name3.size()-3);
// Output formatting
cout << "Name Age Modified"<<endl;
cout << name1<< " "<<age1<<" "<<n1<<endl;
cout << name2<< " "<<age2<<" "<<n2<<endl;
cout << name3<< " "<<age3<<" "<<n3<<endl;
return 0;
}
The output asks the first question which is for the name of the first student but it outputs as this:
Who Is The First Student?- John Doe- What is John's Age?- 19- Who Is The Second Student?- What is 's Age?-
It is skipping the user input of the second student's name and instantly asking for the age but I don't know why this is happening, is there something wrong with my code or do I have the formatting incorrect? I believe that I used the getline function correctly but I may be incorrect and unaware of it being skipped over by a more important function.