Thread: Validating input in a do-while loop

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    Validating input in a do-while loop

    Hello, first post here! I'm taking my first programming course, and working on an assignment. We are to accept input as r, p, s or q (rock, paper scissors, quit). It is a 2 player game both responses input by user, so not using rand fn. I have mine accepting the proper inputs, and incrementing and decrementing properly for wins/ties and printing output to screen once terminated.

    My issue is how do I get it to reprompt the user for a response if they enter an invalid one, not sure if I need 2 loops, or if an if statement in between each input may suffice?

    Any help much appreciated, source code at bottom of this post.

    Here's a sample interaction from the assignment handout:

    "Welcome to Rock, Paper, Scissors! It's Player 1's turn.
    Choose Rock (r),Paper (p), Scissors (s), or Quit (q): p
    It's Player 2's turn.
    Choose Rock (r), Paper (p), Scissors (s), or Quit (q): s
    Player 2 wins!

    It's Player 1's turn.
    Choose Rock (r),Paper (p), Scissors (s), or Quit (q): k
    Player 1 bad input. Try again.
    It's Player 1's turn.
    Choose Rock (r),Paper (p), Scissors (s), or Quit (q): r
    It's Player 2's turn.
    Choose Rock (r), Paper (p), Scissors (s), or Quit (q): d
    Player 2 bad input. Try again.
    It's Player 2's turn.
    Choose Rock (r), Paper (p), Scissors (s), or Quit (q): s
    Player 1 wins!

    It's Player 1's turn.
    Choose Rock (r),Paper (p), Scissors (s), or Quit (q): p
    It's Player 2's turn.
    Choose Rock (r), Paper (p), Scissors (s), or Quit (q): p
    Tie!

    It's Player 1's turn.
    Choose Rock (r),Paper (p), Scissors (s), or Quit (q): q
    Here are the final results:
    Player 1's wins: 1
    Player 2's wins: 1
    Ties: 1"


    Code:
    #include <stdio.h>int main (void)
    
    
    {
    
    
    char response1, response2;
    int wins1 = 0, wins2 = 0, ties = 0;
    
    
    printf("\nWelcome to Rock Paper Scissors!\n\n");
    
    
    do {
    
    
    printf("\nIt's Player 1's Turn\n");
    printf("\nEnter Rock (r), Paper (p), Scissors (s), or Quit (q):  ");
    scanf("%c", &response1); 
    getchar(); 
    
    
    printf("\nIt's Player 2's Turn\n");
    printf("\nEnter Rock (r), Paper (p), Scissors (s), or Quit (q):  ");
    scanf("%c", &response2); 
    getchar();    
    // Output winner of match or if it's a tie and increment/decrement number of ties/wins/losses
    
    
    if ((response1 == 'r' && response2 == 'r') || (response1 == 'p' && response2 == 'p') || (response1 == 's' && response2 == 's'))
    
    
    {
    ties++;
    printf("\nIt's a tie!\n"); 
    
    
    }
    else if ((response1 == 'r' && response2 == 's') || (response1 == 'p' && response2 == 'r') || (response1 == 's' && response2 == 'p'))
    {
    wins1++;
    printf("\nPlayer one wins\n\n");
    }
    
    
    else if ((response2 == 'r' && response1 == 's') || (response2 == 'p' && response1 == 'r') || (response2 == 's' && response1 == 'p'))
    wins2++;    
    printf("\nPlayer two wins!\n\n");
    
    
    }
    while ((response1 == 's' || response1 == 'r' || response1 == 'p') && (response2 == 'r' || response2 == 's' || response2 == 'p'));
    
    
    
    
         
    printf("\nTotals:\nPlayer 1:%2d\nPlayer 2:%2d\nTies:%2d\n", wins1, wins2, ties);
    
    
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    scanf("%c", &response2);
    Do not read your code; but, every time I see an "%c" I think the user likely needs to skip white space using " %c". Tim S.
    Code:
    scanf(" %c", &response2);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    2
    Quote Originally Posted by stahta01 View Post
    Code:
    scanf("%c", &response2);
    Do not read your code; but, every time I see an "%c" I think the user likely needs to skip white space using " %c". Tim S.
    Code:
    scanf(" %c", &response2);
    yeah I didn't realize this, used the getchar() functionality to fix it initially, have changed that since.

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    If you want to continue the loop through an "invalid input". One other than 'r','p','s'. The do while loop will need some logic work.
    Code:
    while ((response1 == 's' || response1 == 'r' || response1 == 'p') && (response2 == 'r' || response2 == 's' || response2 == 'p'));
    This is saying keep doing, while the responses are what you want(r,p,s). So the loop will end when it gets an invalid input (any character other than r,p,s).

    (Hint)Think about when you want the loop to end.
    Last edited by Laerehte; 02-28-2019 at 10:32 PM.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validating Input.
    By CrunchyMarvin in forum C Programming
    Replies: 7
    Last Post: 03-26-2013, 03:07 AM
  2. Need help with validating an input.
    By morrjame in forum C Programming
    Replies: 9
    Last Post: 12-20-2010, 09:55 PM
  3. validating input
    By c-rookie in forum C Programming
    Replies: 15
    Last Post: 08-23-2003, 07:55 PM
  4. Validating an input
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-22-2003, 11:25 PM
  5. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 09:42 PM

Tags for this Thread