I'm trying to learn how to receive input that includes whitespaces from the keyboard into a c++ string. I wrote this simple Person class and Tperson driver program to play with. Most of it works as expected, except for two lines indicated by comments where it prints the prompt "Enter first name > " & then immediately prints the next prompt without waiting for input.
What am I doing wrong?
(I'm pretty sure it has nothing to do with the difference between the parameters of the methods setFirstname(string s) and setLastname(string &s); I did that as an experiment to learn what difference the reference would make, but I haven't gotten to that issue yet.)
Code:
// Tperson.cpp:
// trial version: trying to enable 2-word names (not successful yet)
#include <iostream>
#include <string>
#include "person.h"
using namespace std;
int main()
{
string s1,s2;
int z;
char c;
double rate;
Person p1;
Person *p2;
cout.setf(ios::fixed);
cout.precision(2);
cout << "Testing default constructor:\n";
p1.print();
cout << "\nTesting mutator functions:\n";
cout << "Enter first name > ";
getline(cin,s1);
p1.setFirstname(s1);
cout << "Enter last name > ";
getline(cin,s1);
p1.setLastname(s1);
cout << "Enter age > ";
cin >> z;
p1.setAge(z);
cout << "Enter salary > ";
cin >> rate;
p1.setSalary(rate);
cout << "Enter sex > ";
cin >> c;
p1.setSex(c);
p1.print();
cout << "\nNow testing dynamic allocation with name constructor:\n";
cout << "Enter first name > ";
getline(cin,s1); // can't figure out why this line *********************
// is being skipped; printing to screen
// Enter first name > Enter last name >
// and then waiting for input
cout << "Enter last name > ";
getline(cin,s2);
p2 = new Person(s1,s2);
p2 -> print();
cout << "\nNow testing mutator functions on dynamically allocated object:\n";
cout << "Enter last name > ";
getline(cin,s1);
p2 -> setLastname(s1);
cout << "Enter age > ";
cin >> z;
p2 -> setAge(z);
cout << "Enter salary > ";
cin >> rate;
p2 -> setSalary(rate);
cout << "Enter sex > ";
cin >> c;
p2 -> setSex(c);
p2 -> print();
delete p2;
cout << "\nNow testing multiple inputs:\n";
for (int i=0; i<5; i++)
{
p2 = new Person();
cout << "Enter first name > ";
getline(cin,s1); // here too, this line ********************************
// is being skipped; printing to screen
// Enter first name > Enter last name >
// and then waiting for input
p2 -> setFirstname(s1);
cout << "Enter last name > ";
getline(cin,s1);
p2 -> setLastname(s1);
cout << "Enter age > ";
cin >> z;
p2 -> setAge(z);
cout << "Enter salary > ";
cin >> rate;
p2 -> setSalary(rate);
cout << "Enter sex > ";
cin >> c;
p2 -> setSex(c);
p2 -> print();
delete p2;
}
return 0;
}
////////////////////////////////////////////////////////////////////
// person.h
#ifndef _person_h
#define _person_h
#include <string>
#include <iostream>
using namespace std;
class Person{
public:
Person() {_firstname=_lastname="unknown"; _salary=_age=_sex=0;}
Person(string s1, string s2) { setFirstname(s1); setLastname(s2); _salary=_age=_sex=0;}
void setFirstname(string &s) {_firstname = s;}
void setLastname(string &s) {_lastname = s;}
void setAge(int z) {_age = z;}
void setSalary(double r) {_salary = r;}
void setSex(int x) {_sex=x;}
void print(void) { cout << _lastname << " "
<< _firstname << endl;
cout << " age: " << _age << " salary: " << _salary << " sex: " << (char)_sex << endl;}
private:
string _firstname;
string _lastname;
int _age;
double _salary;
int _sex;
};
#endif