Do-While Only Repeats Once

Hello,
This asks a user to enter rock, paper, or scissor. Then it asks them to repeat the process based on yes or no to restart. But choosing yes only loops through a second time then exits? Any help would be appreciated!

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
#include <stdio.h>

void displayRules();
void userInput (char p1, char p2);

int main()
{
    char p1Choice, p2Choice, decison;
    int count = 0;
    
    displayRules();
    
    do{
    userInput (p1Choice, p2Choice);
    printf("Go Again ?\n");
    scanf("%c", &decison);
    }while(decison == 'y' || decison == 'Y');
    
}
    
    
    
    
    
}

void displayRules()
{
	printf("Welcome to the game of Rock, Paper, Scissors.\n\n");
	printf("This is a game for two players. For each game, \n");
	printf("each player selects one of the objects, Rock, Paper, or Scissors.\n\n");
	printf("The rules for winning the game are: \n\n");
	printf("1. If both players select the same object, it is a tie.\n\n");
	printf("2. Rock beats Scissors. \n\n");
	printf("3. Paper beats Rock. \n\n");
	printf("4. Scissors beats Paper.\n\n");
	printf("Enter R to select Rock, P to select Paper, and S to select Scissors.\n\n");

}

void userInput (char p1, char p2)
{
     
    printf("Player 1 Enter Your Choice\n");
    scanf("%c", &p1);
    getchar();
    
    printf("Player 2 Enter Your Choice\n");
    scanf("%c", &p2);
    getchar();
}
That's really strange.

I tried compiling in VS2010 and got the same thing. I'm not big on stdio.h mainly because I don't use it often. I was guessing that it had something to do with the userInput function where you use getchar() without putting the value somewhere. I tried to get rid of those lines and it exited even sooner. It must have something to do with what conditions are required to stop blocking as I think pressing "ENTER" makes everything to two steps which is killing your do-while.

Out of interest, I tried using <iostream> instead and it works perfectly:
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
#include <iostream>
using namespace std;

void displayRules();
void userInput (char p1, char p2);

int main()
{
    char p1Choice, p2Choice, decison;
    int count = 0;
    
    displayRules();
    
    do{
    userInput (p1Choice, p2Choice);
    cout << "Go Again ?\n";
    cin >> decison;
    }while(decison == 'y' || decison == 'Y');
    
}

void displayRules()
{
	cout<<"Welcome to the game of Rock, Paper, Scissors.\n\n";
	cout<<"This is a game for two players. For each game, \n";
	cout<<"each player selects one of the objects, Rock, Paper, or Scissors.\n\n";
	cout<<"The rules for winning the game are: \n\n";
	cout<<"1. If both players select the same object, it is a tie.\n\n";
	cout<<"2. Rock beats Scissors. \n\n";
	cout<<"3. Paper beats Rock. \n\n";
	cout<<"4. Scissors beats Paper.\n\n";
	cout<<"Enter R to select Rock, P to select Paper, and S to select Scissors.\n\n";

}

void userInput (char p1, char p2)
{
     
    cout << "Player 1 Enter Your Choice\n";
    cin>>p1;
    
    cout << "Player 2 Enter Your Choice\n";
    cin>>p2;
}


<stdio.h> is really only useful for C. <iostream> is much easier to work with and is the preferred IO when working in C++
closed account (1vRz3TCk)
Schwagmister,

When you ask the user to enter a choice, they press a letter and then the return, in userInput you are dealing with the return by calling getchar but in main you are not, so it is still there when you come to it again.
Thank you for all the input!

Stewbond,
I also perfer iostream and c++, but the second part of my programming class is using c -_-.

CodeMonkey, thank you so very much that fixed it right up!
Topic archived. No new replies allowed.