long callPlayerChoice; //completed code
long playGame; //in progress
char playerName[30]; //in progress (error allowing multiple names)
long Instructions; //in progress
long Quit; //completed code
using namespace std;
int main()
{
cout << "Please enter a number for the task you want to complete." << endl << endl;
cout << "1) Play" << endl;
cout << "2) Instructions" << endl;
cout << "3) Quit" << endl;
cin >> callPlayerChoice;
////////////////////////////////////////////////////////////////////////////
if(callPlayerChoice == 1)
{
playGame = 1;
}
else if(callPlayerChoice == 2)
{
Instructions = 2;
}
else{Quit = 3;
};
/////////////////////////////////////////////////////////////////////////////
if(Quit == 3)
{
system ("CLS");
cout << "Thanks for playing!!! Hope you enjoyed!!!" << endl;
cin.get();
};
/////////////////////////////////////////////////////////////////////////////
if(Instructions == 2)
{
system ("CLS");
cout << "There are currently no instructions for this program." << endl;
cin.get();
};
//////////////////////////////////////////////////////////////////////////////
if(playGame == 1)
{
system ("CLS");
cout << "Welcome to the game! Since you are starting on your journey," << endl;
cout << "let me know a little bit about you.";
cout << " What is your full name?" << endl << endl;
cin.getline(playerName , 29);
cout << "Are you sure your name is " << playerName << "?" << endl;
};
///////////////////////////////////////////////////////////////////////////////
system ("PAUSE");
return 0;
}
i run this and it completely skips the statement where i want to have them input for they're name at the bottom ,i want it to run so that if their name was "bob smith" it would say Are you sure your name is bob smith?(using dev c++ by the way,for testing)
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
long callPlayerChoice; //completed code
long playGame; //in progress
string playerName; //in progress (error allowing multiple names)
long Instructions; //in progress
long Quit; //completed code
int main()
{
cout << "Please enter a number for the task you want to complete." << endl << endl;
cout << "1) Play" << endl;
cout << "2) Instructions" << endl;
cout << "3) Quit" << endl;
cin >> callPlayerChoice;
////////////////////////////////////////////////////////////////////////////
if(callPlayerChoice == 1)
{
playGame = 1;
}
elseif(callPlayerChoice == 2)
{
Instructions = 2;
}
else{Quit = 3;
};
/////////////////////////////////////////////////////////////////////////////
if(Quit == 3)
{
system ("CLS");
cout << "Thanks for playing!!! Hope you enjoyed!!!" << endl;
cin.get();
};
/////////////////////////////////////////////////////////////////////////////
if(Instructions == 2)
{
system ("CLS");
cout << "There are currently no instructions for this program." << endl;
cin.get();
};
//////////////////////////////////////////////////////////////////////////////
if(playGame == 1)
{
system ("CLS");
cout << "Welcome to the game! Since you are starting on your journey," << endl;
cout << "let me know a little bit about you.";
cout << " What is your full name?" << endl << endl;
cin >> playerName;
cout << "Are you sure your name is " << playerName << "?" << endl;
};
///////////////////////////////////////////////////////////////////////////////
system ("PAUSE");
return 0;
}
Yes, CLman94's code works.
However, if you change line 55 to getline(cin,playerName); it seems to be skipped because the buffer is not empty. And I can't figure out what would fill the buffer, since the only other cin is the one at the beginning for an int.
I don't understand it.
i changed the code, however it will not add more than one name, i try to change line 55 togetline(cin,playerName); but it then skips the line of code. This is my current code.
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
long callPlayerChoice; //completed code
long playGame; //in progress
string playerName; //in progress (error allowing multiple names)
long Instructions; //in progress
long Quit; //completed code
int main()
{
cout << "Please enter a number for the task you want to complete." << endl << endl;
cout << "1) Play" << endl;
cout << "2) Instructions" << endl;
cout << "3) Quit" << endl;
cin >> callPlayerChoice;
////////////////////////////////////////////////////////////////////////////
if(callPlayerChoice == 1)
{
playGame = 1;
}
elseif(callPlayerChoice == 2)
{
Instructions = 2;
}
else{Quit = 3;
};
/////////////////////////////////////////////////////////////////////////////
if(Quit == 3)
{
system ("CLS");
cout << "Thanks for playing!!! Hope you enjoyed!!!" << endl;
cin.get();
};
/////////////////////////////////////////////////////////////////////////////
if(Instructions == 2)
{
system ("CLS");
cout << "There are currently no instructions for this program." << endl;
cin.get();
};
//////////////////////////////////////////////////////////////////////////////
if(playGame == 1)
{
system ("CLS");
cout << "Welcome to the game! Since you are starting on your journey," << endl;
cout << "let me know a little bit about you.";
cout << " What is your full name?" << endl << endl;
cin >> playerName;
cout << "Are you sure your name is " << playerName << "?" << endl;
};
///////////////////////////////////////////////////////////////////////////////
system ("PAUSE");
return 0;
}[code]
please help so i can add multiple names (like first and last)[/code]
After your cin >> statements, add the line cin.ignore(80,'\n');. This effectively removes up to 80 things from the cin buffer or '\n' (whichever comes first). The issue stems from when you input something into cin, it stores it to the buffer, then reads what it needs to (in this case it reads a long int (callPlayerChoice) and leaves everything else in the buffer (the character(s) from when you hit Enter/Return)). This can be an issue since when you try to use the cin.getline() function, the stuff left in the buffer is read first (typically the end of line character) until it reaches the end of line, therefore not allowing any input for a name.