not allowing me to input for this code

//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>


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)
Last edited on
closed account (j2NvC542)
1
2
3
4
5
6
7
if(callPlayerChoice == 1) {
     playGame = 1;
} else if(callPlayerChoice == 2) {
     Instructions = 2;
} else {
     Quit = 3;
};

There shouldn't be a semicolon after each if-statement.

And what's the point of this code anyway? Why don't you just do:
1
2
3
4
5
6
7
if(callPlayerChoice == 1) {
    // code for gameplay
} else if(callPlayerChoice == 2) {
    // code for instructions
} else {
    // code to quit
}


You don't need to define your variables as long. int is more than enough in this case.
Last edited on
Instead of using char playernames[30], change it to string playernames and keep line using namespace std; above your variables to allow string to work.

Your code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
using namespace 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; 
} 
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 >> playerName;
cout << "Are you sure your name is " << playerName << "?" << endl;
}; 
///////////////////////////////////////////////////////////////////////////////
system ("PAUSE");
return 0;
}


This worked with my compiler.
closed account (j2NvC542)
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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
using namespace 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; 
} 
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 >> 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]
Last edited on
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.
Last edited on
thanks, this solved my problem
Topic archived. No new replies allowed.