How do I input string variables that are multiple words.
I am using #include <string>
using namespace std;
Then:
string variableName;
then:
cin >> variableName;
cout << variableName;
If my input for variableName includes no spaces (for example "rRr" or "435td), the program runs fine, but if I try to input a string with a space "for example "r R r," the program malfuncions and does weird stuff.
Here is an example for an assignment I did for class:
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
//Create constant for screen width
constint SCREEN_WIDTH = 118;
int main(void)
{
//Define variables for player's position
string position1;
string position2;
string position3;
string position4;
//Define variables for player salary, bonus, and number of players needed at that position
double salary1;
double salary2;
double salary3;
double salary4;
double bonus1;
double bonus2;
double bonus3;
double bonus4;
double numberOfPlayersNeeded1;
double numberOfPlayersNeeded2;
double numberOfPlayersNeeded3;
double numberOfPlayersNeeded4;
//Set the formatting for the screen output of decimal values to 2 decimal places
cout << fixed << setprecision(2);
//Output the big divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH + 1) << " " << setfill(' ') << endl;
//Prompt user to enter the 1st player's position
cout << "Enter the 1st player's position: ";
cin >> position1;
//Promt user to enter the salary & bonus (in millions) for the 1st player's position
cout << "Enter the salary & bonus (in millions) for " << position1 << "s needed: ";
cin >> salary1 >> bonus1;
//Prompt user to enter the number of players needed at that position
cout << "Enter the number of " << position1 << "s needed for the team: ";
cin >> numberOfPlayersNeeded1;
//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH - 15) << " " << setfill(' ') << endl;
//Prompt user to enter the 2nd player's position
cout << "Enter the 2nd player's position: ";
cin >> position2;
//Promt user to enter the salary & bonus (in millions) for the 2nd player's position
cout << "Enter the salary & bonus (in millions) for " << position2 << "s needed: ";
cin >> salary2 >> bonus2;
//Prompt user to enter the number of players needed at that position
cout << "Enter the number of " << position2 << "s needed for the team: ";
cin >> numberOfPlayersNeeded2;
//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH - 15) << " " << setfill(' ') << endl;
//Prompt user to enter the 3rd player's position
cout << "Enter the 3rd player's position: ";
cin >> position3;
//Promt user to enter the salary & bonus (in millions) for the 3rd player's position
cout << "Enter the salary & bonus (in millions) for " << position3 << "s needed: ";
cin >> salary3 >> bonus1;
//Prompt user to enter the number of players needed at that position
cout << "Enter the number of " << position3 << "s needed for the team: ";
cin >> numberOfPlayersNeeded3;
//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH - 15) << " " << setfill(' ') << endl;
//Prompt user to enter the 4th player's position
cout << "Enter the 4th player's position: ";
cin >> position4;
//Promt user to enter the salary & bonus (in millions) for the 4th player's position
cout << "Enter the salary & bonus (in millions) for " << position4 << "s needed: ";
cin >> salary4 >> bonus4;
//Prompt user to enter the number of players needed at that position
cout << "Enter the number of " << position4 << "s needed for the team: ";
cin >> numberOfPlayersNeeded4;
//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH - 15) << " " << setfill(' ') << endl;
return 0;
}
If I input the value for "player's position" with no spaces, this is the output:
**********************************************************************************************************************
Enter the 1st player's position: Quarterback
Enter the salary & bonus (in millions) for Quarterbacks needed: 10
4
Enter the number of Quarterbacks needed for the team: 3
******************************************************************************************************
Enter the 2nd player's position: RunningBack
Enter the salary & bonus (in millions) for RunningBacks needed: 3
3
Enter the number of RunningBacks needed for the team: 4
******************************************************************************************************
Enter the 3rd player's position: WideReceiver
Enter the salary & bonus (in millions) for WideReceivers needed: 6
3
Enter the number of WideReceivers needed for the team: 6
******************************************************************************************************
Enter the 4th player's position: Kicker
Enter the salary & bonus (in millions) for Kickers needed: 3
0
Enter the number of Kickers needed for the team: 1
******************************************************************************************************
Press any key to continue . . .
But if I use spaces (notice the input on the second prompt was "Running Back" rather than "RunningBack", this is the output:
1 2 3 4 5 6 7 8 9 10 11
**********************************************************************************************************************
Enter the 1st player's position: Quarterback
Enter the salary & bonus (in millions) for Quarterbacks needed: 10
4
Enter the number of Quarterbacks needed for the team: 4
******************************************************************************************************
Enter the 2nd player's position: Running Back
Enter the salary & bonus (in millions) for Runnings needed: Enter the number of Runnings needed for the team: ******************************************************************************************************
Enter the 3rd player's position: Enter the salary & bonus (in millions) for s needed: Enter the number of s needed for the team: ******************************************************************************************************
Enter the 4th player's position: Enter the salary & bonus (in millions) for s needed: Enter the number of s needed for the team: ******************************************************************************************************
Press any key to continue . . .
The program stops running properly and does not allow me to input a value for the 3rd or 4th player's position.
Formatted input transforms space-separated items from input into the target something’s type and does not consume newlines.
Unformatted input just reads an entire line of data into a string and removes the newline.
To make that usable, make sure to always synchronize to EOL after every input:
1 2 3 4 5 6 7 8 9 10
// Ask for and get an integer
int x;
cout << "x? ";
cin >> x;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
// Ask for and get an entire line of text
string s;
cout << "s? ";
getline( cin, s );
This works just fine, since the user should always be expected to press ENTER after every input.