0

Please help, I cant seem to get the do-while loop to loop when I ask the user to input continue using scanf and printf. Could it be the wipe_buffer? I can't seem to get it right on this and I just need to finish the loop and that should be mostly functional. New to coding and this website please not too harsh. please and thanks.

    #include <stdio.h>
    #include <stdlib.h>
void wipe_buffer(void);
int main(int argc, char* argv[])

{
    char play1;
    char play2;
    char cont;

    do{
        printf("Player one pick Rock, Paper, or Scissors\n");
        scanf(" %c", &play1);
        wipe_buffer();
        printf("Player two pick Rock, Paper, or Scissors\n");
        scanf(" %c", &play2);
        wipe_buffer();

        switch(play1)
        {
            case 'r':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Paper Covers Rock\n");
                printf("Player two wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Rock Breaks Scissors\n");
                    printf("Player one wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Draw, Nobody wins\n");
                }
            break;
            case 'R':
                if(play2 == 'p' || play2 == 'P')
            {
                printf("Paper Covers Rock\n");
                printf("Player two wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Rock Breaks Scissors\n");
                    printf("Player one wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Draw, Nobody wins\n");
                }
            break;
            case 'P':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Draw, Nobody wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Scissors cuts Paper\n");
                    printf("Player two wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Paper covers rock\n");
                }
            break;
            case 'p':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Draw, Nobody wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Scissors cuts Paper\n");
                    printf("Player two wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Paper covers rock\n");
                }
            break;
            case 's':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Scissors Cuts Paper\n");
                printf("Player one wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Draw, Nobody wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Rock breaks Scissors\n");
                    printf("Player two wins\n");
                }
            break;
            case 'S':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Scissors Cuts Paper\n");
                printf("Player one wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Draw, Nobody wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Rock breaks Scissors\n");
                    printf("Player two wins\n");
                }
            break;
        }
    printf("Do you wish to continue?\n");
    scanf(" &c", &cont);
    wipe_buffer();
    }while(cont == 'y' || cont == 'Y');
}
void wipe_buffer(void)
{
    char t;
    scanf("%c", &t);
    while(t != '\n' )
    {
        scanf("%c", &t);
    }
    return;
}
4
  • Yet another RPS question. Commented Oct 6, 2013 at 22:25
  • 2
    I read scanf(" &c", &cont); before the end of your do-while loop. I suspect you must change that & inside the string by a %. It's been a while since I did C. Commented Oct 6, 2013 at 22:27
  • RPS question? lol sorry im noob if you could point me in the right direction that would be nice, and that did it lol thanks naitoon! I just couldn't find my mistake. Commented Oct 6, 2013 at 22:28
  • when you call wipe_buffer t is equal to \n so the loop isn't even considered just put a getchar() after each scanf and you'll be fine Commented Oct 6, 2013 at 22:52

3 Answers 3

3

In this section:

    printf("Do you wish to continue?\n");
scanf(" &c", &cont);

"&c" should be "%c"

Note that when I compiled this with gcc, I got a warning that basically addressed this exact problem, so make sure to read your warnings.

Sign up to request clarification or add additional context in comments.

2 Comments

that's odd i use codeblocks and it just ran through without error? I could be looking at the wrong place
I also ran it with codeblocks. See if -Wall is checked in the build options - it was for me by default.
2

In here:

printf("Do you wish to continue?\n");
    scanf(" &c", &cont);
    wipe_buffer();
    }while(cont == 'y' || cont == 'Y');

there should be:

scanf(" %c", &cont);

Compile with -Wall -pedantic options next time you face some problem(if you use gcc). It shows nicely where the problem lies, and if not - it narrows it down a little.

That's what it shows in your case:

test2.c:117:5: warning: too many arguments for format [-Wformat-extra-args]
test2.c:120:1: warning: control reaches end of non-void function [-Wreturn-type]

so it's also a nice reminder that your main() function has to return some value.

Another suggestion is to rad through this link:http://www.gidnetwork.com/b-60.html and follow to The Steps page.
To make it short - there's some information on how time and space consuming is reading one char from stdin with scanf in comparison to getchar.
Consider applying this suggestion in your code.

Comments

0

So, a really quick way to see what's going on is to drop a debugging printf statement right before the loop

    scanf(" &c", &cont);
    wipe_buffer();
    printf("Debug: [%c]\n", cont);
    }while(cont == 'y' || cont == 'Y');

Then look for the printf when the program runs.

I think what is going on is that each scanf is looking for a Enter keystroke before processing, so wipe_buffer is going to request a lot of Enter keystrokes before continuing. Try running the program without the wipe_buffer and you'll still need to press Enter before it continues.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.