Originally Posted by
jimblumberg
You may want to consider getline() instead of the extraction operator>> for all of your strings. Also a "phone number" isn't really a number and should probably be stored as a string.
I made the edits and my program worked perfectly:
Code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct UserInfo {
string firstName;
string lastName;
string address;
string phoneNumber;
};
int main(){
UserInfo user[1];
for (int i = 0; i < 1; i++) {
cout << "Please enter your first name: ";
getline(cin, user[i].firstName);
cout << "Please enter your last name: ";
getline(cin, user[i].lastName);
cout << "Please enter your address: ";
getline(cin, user[i].address);
cout << "Please enter your phone number: ";
getline(cin, user[i].phoneNumber);
cout << "\n";
}
system("pause");
return 0;
}
Can to you explain to me why getline() is better to use in this situation instead of cin?