Babak Niroomand
Babak Niroomand

Reputation: 1

Run-time check failure #02

I'm working with C & I receive this error when I run my program. The error occurs when I enter the character "e" to close the program. Please help me!!! Thanks. :)

int main () {
while (true){
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
char wanted;
int c;
printf("Enter What You Want...\n");
printf("f for flower\n");
printf("m for mushroom\n");
printf("b for box\n");
printf("h for mario\n");
printf("e to close\n");
scanf("%s", &wanted);
if (wanted=='f'){
/*some codes here*/
}
else if (wanted=='m'){
/*some codes here*/
     }
    else if (wanted=='b'){
/*some code here*/
    }
    else if (wanted=='h'){
/*some codes here*/
    }
    else if (wanted=='e'){
         printf("Bye.\n");
         break;
    }
        else {
            printf("It Was'n a Possible Input. Try Again...\n");
        }
    }
    return 0;
 }

Upvotes: 0

Views: 41

Answers (1)

simonc
simonc

Reputation: 42175

scanf("%s", &wanted); should instead be scanf("%c", &wanted);

A format string of %s indicates that the argument to scanf will be a char array. You have a single char so need to use %c

Upvotes: 2

Related Questions