[SOLVED] [C] Reusing User Defined Variable

AoN

Internet Programmer
Joined
Aug 1, 2012
Posts
114
This is actually just me messing around out of bordom in class, and this is the third time I've rewritten this thing in the last two hours, but still can't get it to work right. The issue first is seen when entering "n" or "N" for the answer to the first question. It'll display the next question like it's supposed to, but it also displays the first WHILE loop, which shouldn't have been called yet.

When I comment out the second scanf() it skips to the next level, defaulting with question one's answer (expected), but there is displays its WHILE loop which shouldn't have been called yet.

Even tried returning the yn variable to NULL before prompting, no change. Each version I've written has gotten dumber and dumber in the method of coding I've used and I'm starting to run out of ideas on how to dumb it down (almost to single-selection statements).

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char yn;
    int loop;
    
    printf("Does the damn thing work? (Y/N) ");
    scanf("%c", &yn);
    if(yn == 'y' || yn == 'n' || yn == 'Y' || yn == 'N')
    {
        if(yn == 'y' || yn == 'Y')
        {
            printf("Don't screw with it!\n");
        }
        else
        {
            printf("Did you screw with it? (Y/N) ");
            scanf("%c", &yn);
            if(yn == 'y' || yn == 'n' || yn == 'Y' || yn == 'N')
            {
                if(yn == 'y' || yn == 'Y')
                {
                    printf("You dumb ****!\n");
                    printf("Does anyone know? (Y/N) ");
                    scanf("%c", &yn);
                    if(yn == 'y' || yn == 'n' || yn == 'Y' || yn == 'N')
                    {
                        if(yn == 'y' || yn == 'Y')
                        {
                            loop = 1;
                            while(loop == 1)
                            {
                                printf("You poor *******...\n");
                                printf("Can you blame someone else? (Y/N) ");
                                scanf("%c", &yn);
                                if(yn == 'y' || yn == 'Y')
                                {
                                    loop == 0;
                                }
                            }
                        }
                        else
                        {
                            printf("HIDE IT!\n");
                        }
                    }
                    else
                    {
                        while(yn != 'y' || yn != 'n' || yn != 'Y' || yn != 'N')
                        {
                            printf("You did not enter in Y or N!\n");
                            printf("Does anyone know? (Y/N) ");
                            scanf("%c", &yn);
                        }
                    }
                }
                else
                {
                    printf("Will you catch hell? (Y/N) ");
                    scanf("%c", &yn);
                    if(yn == 'y' || yn == 'n' || yn == 'Y' || yn == 'N')
                    {
                        if(yn == 'y' || yn == 'Y')
                        {
                            loop = 1;
                            while(loop == 1)
                            {
                                printf("You poor *******...\n");
                                printf("Can you blame someone else? (Y/N) ");
                                scanf("%c", &yn);
                                if(yn == 'y' || yn == 'Y')
                                {
                                    loop == 0;
                                }
                            }
                        }
                        else
                        {
                            printf("****-can it!\n");
                        }
                    }
                    else
                    {
                        while(yn != 'y' || yn != 'n' || yn != 'Y' || yn != 'N')
                        {
                            printf("You did not enter in Y or N!\n");
                            printf("Will you catch hell? (Y/N) ");
                            scanf("%c", &yn);
                        }
                    }
                }
            }
            else
            {
                while(yn != 'y' || yn != 'n' || yn != 'Y' || yn != 'N')
                {
                    printf("You did not enter in Y or N!\n");
                    printf("Did you screw with it? (Y/N) ");
                    scanf("%c", &yn);
                }
            }
        }
    }
    else
    {
        loop = 1;
        while(loop == 1)
        {
            printf("You did not enter in Y or N!\n");
            printf("Does the damn thing work? (Y/N) ");
            scanf("%c", &yn);
            if(yn == 'y' || yn == 'n' || yn == 'Y' || yn == 'N')
            {
                loop = 0;
            }
        }
    }
    printf("NO PROBLEM!\n\n");
    system("pause");
    return 0;
}
 
You need a new line before you can ask for a character. The character is getting clobbered by the lack of a line change.

Code:
scanf("%c", &yn);

scanf("\n%c", &yn);
 
That makes absolutely no sense to me, have to put in a line break like that, but you were right, it did work. At least until I had to call on the WHILE loops. Appearantly, setting "loop = 0;" would not break the loop, so I just replaced it with "break;".

Thanks for the help, I'll have to remember that \n when reusing a variable for user input in C. ^^
 
It is due to the fact that %c is looking for one character, but you have a whole string of characters before the one you want. The \n clears the line to start with the input you give via the command line.

The while loops were not working because you had loop == 0, a logic check, instead of loop = 0, an assignment statement.
 
That's twice today I've been hit on that logic check thing. Thank you! ^^'
 
No problem. A common mistake when first learning, and even those of us with lots of experience make it once in a while, i.e.
Code:
if(a = b)

if(a == b)

The above mistake is very easy to make and most compilers allow the assignment in the if statement which can wreak havoc on the programmer. ;-}
 
Yeah, over a decade of programming HTML, CSS, JavaScript, PHP, and MySQL, yet working with VBS, batch, and C seem to make me ignore the basics. XD
 
Seeing as I don't normally program in C, probably never will, and just made the thing out of bordom in class, not that concerned. However, you did get me searching and I can't seem to find an alternate that works on both a Windows and *nix system.
 
Seeing as I don't normally program in C, probably never will, and just made the thing out of bordom in class, not that concerned. However, you did get me searching and I can't seem to find an alternate that works on both a Windows and *nix system.

getchar() - getchar - C++ Reference (It's from stdio.h which is a C header)
Function Descriptions : getchar

Seeing as I don't normally program in C, probably never will, and just made the thing out of bordom in class, not that concerned.

Yeah, I know... But it is not always bad to get some insight into what's wrong with boredom code. I post my boredom code all the time in my own forum, sometimes even if it's not optimized. Just take a look here: https://www.sysnative.com/forums/programming/5245-hilo-game-python.html for something I posted on this very forum that wasn't fully optimized. :thumbsup2:
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top