It prints The printf statement twice - c

This is really strange it prints this line printf("Do you want to continue Yes (Y) or No (N): \n"); Not using any loop nothing but still it prints that statement twice
int main()
{
int led=0;
int ohm=0;
char check;
int flag=0;
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close: \n\n");
printf(" ******************** Press 1 for switch (LED) 1 ********************\n");
printf(" ******************** Press 2 for switch (LED) 2 ********************\n");
printf(" ******************** Press 3 for switch (LED) 3 ********************\n");
printf("Switch: ");
scanf("%d", &led);
}
printf("\n\n");
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: \n\n");
printf(" ******************** Press 1 for 10 ohm resistance ********************\n");
printf(" ******************** Press 2 for 20 ohm resistance ********************\n");
printf(" ******************** Press 3 for 30 ohm resistance ********************\n");
printf("Resistance: ");
scanf("%d", &ohm);
}
while (flag == 0)
{
//LED-1
if(led== 1 && ohm== 1 )
{
printf("LED-1 is blinking 2 times\n");
}
if(led== 1 && ohm== 2)
{
printf("LED-1 is blinking 4 times\n");
}
if(led== 1 && ohm== 3 )
{
printf("LED-1 is blinking 6 times\n");
}
//LED-2
if(led== 2 && ohm== 1 )
{
printf("LED-2 is blinking 2 times\n");
}
if(led== 2 && ohm== 2 )
{
printf("LED-2 is blinking 4 times\n");
}
if(led == 2 && ohm == 3)
{
printf("LED-2 is blinking 6 times\n");
}
//LED-3
if(led == 3 && ohm == 1 )
{
printf("LED-3 is blinking 2 times\n");
}
if(led == 3 && ohm == 2)
{
printf("LED-3 is blinking 4 times\n");
}
if(led == 3 && ohm == 3)
{
printf("LED-3 is blinking 6 times\n");
}
led = 0;
ohm = 0;
printf("Do you want to continue Yes (Y) or No (N): \n");
scanf("%c", &check);
if(check =='Y' || check =='y')
{
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close on: ");
scanf("%d", &led);
}
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: ");
scanf("%d", &ohm);
}
}
if(check=='N' || check=='n')
{
printf("Thanks for using the program");
flag = 1;
}
}
return 0;
}

Use scanf(" %d", &led); , scanf(" %c", &check); etc. in the code.
Adding an extra space before the format specifier will solve the problems caused by garbage/newline in the buffer.

The first time scanf("%c", &check); finds some garbage (that does not match y or n) so your program can do another loop.
As someone else already noticed, that garbage might be the newline after the ohm input.
Some ideas to solve the problem:
http://www.velocityreviews.com/forums/t436518-get-rid-of-trailing-newline-for-scanf.html
scanf(" %c", &input);
(leading space will eat whitespace in the input)
scanf() leaves the new line char in the buffer

scanf("%d", &ohm);
Here when you input a number and press ENTER, the number is processed by scanf, but the newline is still in the input buffer, then later
printf("Do you want to continue Yes (Y) or No (N): \n");
scanf("%c", &check);
check will actually store the newline, not 'N',
if(check=='N' || check=='n')
{
printf("Thanks for using the program");
flag = 1;
}
so flag will not be set to 1, the while loop will continue again. In the second loop, check can be assigned 'N', the loop will terminate after.

Please try scanf("%c ", &check);. Notice the extra space given after %c which will absorb extra new line character.

use getchar and it works just fine, you can try it here

Related

How do I change my program to end an incorrect output from continuously being printed to the console?

Im coding a Rock Paper Scissors program and eveything works completely fine except for one thing. In the line of the else condition, the one that contains the while loop (choice > 3 && choice < 1) it only ever works when an inncorect input like 4 is entered once but not a second time.
When an incorrect input is entered the first time the program will ask for the input again but if you enter another inncorrect input it will just continously just keep printing the same printf line
printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n"); and then proceed to just keep printing
Computer choice is ROCK. Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, randNum, seed, choice;
int counter = 0;
int counterUser = 0;
int counterComputer = 0;
printf("Please enter the random number generator seed.\n");
/* ANY integer value can be given as a seed */
scanf("%d", &seed);
/* Seed the random number generator */
srand(seed);
/* Counter to ensure only 10 games are played */
while (counter != 10) {
for (i=0;i<1;i++) {
/* Generate a random number and restrict it to the range 1 to 3 */
randNum = rand()%3+1;
/*printf("A random number between 1 and 3: %d\n", randNum);*/
}
printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
scanf("%d", &choice);
/* User picks an option */
if ( choice == 1 ){
printf("User choice is ROCK.\n");
}
else if ( choice == 2 ){
printf("User choice is PAPER.\n");
}
else if ( choice == 3 ){
printf("User choice is SCISSORS.\n");
}
else {
while (choice > 3 && choice < 1){
printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
scanf("%d", &choice);
}
}
/* Computer choses an option */
if ( randNum == 1 ){
printf("Computer choice is ROCK.\n");
}
else if ( randNum == 2 ){
printf("Computer choice is PAPER.\n");
}
else {
printf("Computer choice is SCISSORS.\n");
}
/* Compare the user and computers choices */
if ( randNum == 1 && choice == 1){
printf("It's a draw. Computer chose ROCK and User also chose ROCK.\n");
}
else if ( randNum == 2 && choice == 2 ){
printf("It's a draw. Computer chose PAPER and User also chose PAPER.\n");
}
else if ( randNum == 3 && choice == 3 ){
printf("It's a draw. Computer chose SCISSORS and User also chose SCISSORS.\n");
}
else if ( randNum == 1 && choice == 2 ){
printf("User wins because PAPER beats ROCK.\n");
counterUser = counterUser + 1;
}
else if ( randNum == 1 && choice == 3 ){
printf("Computer wins because ROCK beats SCISSORS.\n");
counterComputer = counterComputer + 1;
}
else if ( randNum == 2 && choice == 3 ){
printf("User wins because SCISSORS beats PAPER.\n");
counterUser = counterUser + 1;
}
else if ( randNum == 3 && choice == 2 ){
printf("Computer wins because SCISSORS beats PAPER.\n");
counterComputer = counterComputer + 1;
}
else if ( randNum == 2 && choice == 1 ){
printf("Computer wins because PAPER beats ROCK.\n");
counterComputer = counterComputer + 1;
}
else if ( randNum == 3 && choice == 1 ){
printf("User wins because ROCK beats SCISSORS.\n");
counterUser = counterUser + 1;
}
else {
}
counter = counter + 1;
}
printf("In 10 games, computer won %d", counterComputer);
printf(" times");
printf(" and user won %d", counterUser);
printf(" times.");
return 0;
}
UPDATE: I've tweaked it now so the only problem is now getting rid of the Computer choice is ___ when i input 4 or another inncorect value
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
2
User choice is PAPER.
Computer choice is ROCK.
User wins because PAPER beats ROCK.
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
4
Computer choice is PAPER.
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
4
Computer choice is ROCK.
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
4
Computer choice is ROCK.
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
This is the if statement that follows the code above and is outputing its responses when i dont want it to because when i enter or 4 or antoher incorrect value these responses should not be printing to the console aswell
/* Computer choses an option */
if ( randNum == 1 ){
printf("Computer choice is ROCK.\n");
}
else if ( randNum == 2 ){
printf("Computer choice is PAPER.\n");
}
else {
printf("Computer choice is SCISSORS.\n");
}
Sorry for not phrasing it properly before, so how do i get the the line Computer choice is ___ to stop printing because 4 is an incorrect value and hence the Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS. line should just appear on its own
I think your issue is that you're checking whether the user's input is valid or not at the very end. you should move the while loop:
while (choice > 3 && choice < 1){
printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
scanf("%d", &choice);
}
above the if statement:
if ( choice == 1 ){
printf("User choice is ROCK.\n");
}else if ( choice == 2 ){
printf("User choice is PAPER.\n");
}else if ( choice == 3 ){
printf("User choice is SCISSORS.\n");
}
So when the user inputs an invalid number, it will go over the if statement and then it will ask again. Once it gets a valid input i'll go to check the computers move and not print the users move.
I guess this is what you were trying to do?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void state_machine(int *choice, int user);
void request_input(int* choice);
char array[3][20] = {"ROCK", "PAPER", "SCISSORS"};
int main() {
int counter = 0, randNum = 0, choice = 0, counterUser = 0, counterComputer = 0;
srand(time(NULL));
while (counter < 10) {
randNum = rand()%3+1;
request_input(&choice);
state_machine(&choice, 1);
state_machine(&randNum, 2);
if(randNum == choice) {
printf("It's a draw. Computer chose %s and User also chose %s.\n", array[randNum-1],array[choice-1]);
}
else if ( randNum == 1 && choice == 2 || randNum == 2 && choice == 3 || randNum == 3 && choice == 1) {
printf("User wins because %s beats %s.\n", array[choice-1], array[randNum-1]);
counterUser = counterUser + 1;
}
else {
printf("Computer wins because %s beats %s.\n", array[randNum-1], array[choice-1]);
counterComputer = counterComputer + 1;
}
counter++;
}
printf("In %d games, computer won %d times and user won %d times.", counter, counterComputer, counterUser);
return 0;
}
void request_input(int* choice) {
printf("Please choose 1 for %s, 2 for %s or 3 for %s.\n", array[0], array[1], array[2]);
scanf("%d", choice);
}
void state_machine(int *choice, int user) {
if(*choice < 1 || *choice > 3) {
printf("Invalid Selection was made.\n");
request_input(choice);
state_machine(choice,1);
}
else if(user == 1) {
printf("User choice is %s.\n", array[*choice-1]);
}
else if(user == 2) {
printf("Computer choice is %s.\n", array[*choice-1]);
}
}
(choice > 3 && choice < 1) should never work as it is impossible for it to satisfy both predicates. Think, for it to succeed, you need first choice to be greater than 3. Let's assume it is 4, and evaluate the second test (4 is never less than 1, so the second test fails)
No number can be at the same time greater than 3 and less than 1, so the test you have written is always false (as if you had written if (false) instead) Every number that is greater than 3 is also greater than 1, and you are asking for a number less than 1 that is greater than 3. It cannot even work for the case of 4 you state in the question.

Loop terminates because of condition, but the condition is necessary?

I'm working on a program that is a guessing game.
The problem is I have to set remainingguesses to 0 so that the amount of guesses a person can make decreases by 1. But at the same time the condition set in the loop is based on the remainingguesses not being 0. Meaning once it is 0 the loop terminates and moves on.
I don't know how to solve this while making the condition in the loop work properly.
Here's the loop in question:
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
}
}
if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");
Here's the full code in question if needed:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int remainingguesses;
while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);
if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
}
}
if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
Simplified code:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int flag=0;
while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);
if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
while (guesses > 0 && flag==0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",guesses);
scanf(" %d",&secretnumberguess);
guesses=guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
flag=1;
}
}
if (guesses == 0 && flag==0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
}
Here is probably a simple way of coding this.
read guesses;
while (guesses > 0) {
read input;
if (input == secret) {
print "you win!!";
return;
}
else {
print "try again!";
}
guesses--;
}
print "Sorry! You are out of guesses";

how to repeat a menu after dealing with wrong input

So the assignemt is to print a menu with options, and if the user puts in an invalid choice (not 1,2,3,4,5 ,6), it prints an error and asking the user to choose again.
if the user puts in a wrong input 5 times total, the programm will exit.
int main() {
printf("Welcome, please choose one of the options below: \n ");
printf( "1.Exit \n ");
printf( "2.Print menu again \n ");
printf( "3. ");
printf( "4.. ");
printf( "5. ");
printf( "6. ");
printf("Enter your choice: ");
scanf("%d" , &choice);
if( (choice > 6) || (choice < 1) ) {
do {
count++;
printf(" Wrong input, please try again (Enter 2 for re-printing the menu). \n " );
printf("Enter your choice: ");
scanf("%d", &choice);
if(choice==2){
do {
printf("Welcome, please choose one of the options below: \n "); //prints of the screen the following in a loop
printf("1.Exit \n ");
printf("2.Print menu again \n ");
printf("3. ");
printf("4. ");
printf("5. ");
printf("6.");
printf("Enter your choice: ");
scanf("%d", &choice);
} while (choice==2);
}
} while (count < 4) ;
printf("%s" , "You have made 5 menu errors.Bye Bye!!! \n ");
}
while(1) {
.
}
*the while(1)is for the entire code, puts the entire code for re-use
** i didnt use switch-case, cuz it's forbidden to use it
Now, the problem is, that if i put a wrong input first, let's say for example, '7' (which isn't a choice from the menu), it will print "wrong input, please try again". So far so good.
But then,if I press 2 for re-printing the menu, and then I press any number, even if it's a valid choice, it's printing "wrong input".
also, if I press '2' for re-printing the menu, and then press 1, it will require to press 1 twice in order to exit the programm, instead of just pressing once.
The above answers look correct but you can go with the following code as its working and easy to understand for anyone!
#include <stdio.h>
void printMenu()
{
printf("Welcome, please choose one of the options below: \n ");
printf( "1.Exit \n ");
printf( "2.Print menu again \n ");
printf( "3. ");
printf( "4.. ");
printf( "5. ");
printf( "6. ");
}
int main()
{
int choiceValid=0, count=0, choice;
printMenu();
while(choiceValid==0 && count<=5)
{
printf("Enter your choice: ");
scanf("%d" , &choice);
if(choice==2)
{
printMenu();
continue;
}
if( choice<=6 && choice>=1 )
choiceValid=1;
else
{
count++;
printf("\nWrong input, please try again (Enter 2 for re-printing the menu). \n " );
}
}
return 0;
}
Replace your if(choice==2) with if((choice==2) || (choice > 6) || (choice < 1) )
Replace while (choice==2);with while (((choice==2) || (choice > 6) || (choice < 1) ) && (count < 4))
Put the following block inside the same while loop.
if(2 == choice) count = 0;
else if (choice > 6) || (choice < 1) count ++;

Monty hall show simulation unexpected results

#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf ("The door the host picked is %d\n", hostDoor);
do
{
printf("Do you want to switch doors? Please enter in the door you want:\n", hostdoor);
scanf("%d", &option);
if (option > 3 || option <= 0)
{return 0;}
}while (option == hostDoor);
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Hi, this is my version of the monty hall game show, im getting unexpected results though.
sometimes when I enter in a door for my option it just brings me back to the "pick one of the three doors infront of you" statement, when it should tell me if i have won or lost.
I think this is because the "option" door is equal to the "hostDoor.
I thought having "option != hostDoor" would fix it but it does not.
If i am correct in that assumption how can I fix it? If not why is it not working and how can I fix it?
To simulate correctly, OP needs to show the host door.
do {
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) {
return 0;
}
} while (option == hostDoor);
// instead of
#if 0
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) { return 0; }
#endif
To deal with OP " it should tell me if i have won or lost." problem, change
else if (option == remainingDoor)
to
else
Your scanf("%d", &option) is OK. I prefer the fgets()/sscanf() combo and its alway useful to check the result of scanf() family, but that is not your issue here.
Its because of these:
scanf ("%d", &pickedDoor);// reads \n after the last input
scanf("%d", &option); // reads \n after the last input
**option != hostDoor; // completely useless .. get rid of it**
I would suggest putting a getchar() after each scanf to get rid of the \n character
so something like this:
#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
char collect; //variable to collect any extra input like \n
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (option > 3 || option <= 0 )
{
return 0;
}
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else if (option == remainingDoor)
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Another more efficient way would be to use fgets or to have error checks on scanf() itself
srand and rand belongs to stdlib header file
#include<stdlib.h> // include this header file
option != hostDoor; // this statement does not do anything
and your else if (option == remainingDoor) should be else { printf("you lose");}

Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted

I have faced this problem : Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted in visual studio 12 . I also try this in codeblock but faced same problem . I run my code also in ideone.com it shows runtime error . IT works for Y but doesnot works for N
int main()
{
int led=0;
int ohm=0;
char check;
int flag=0;
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close: \n\n");
printf(" ******************** Press 1 for switch (LED) 1 ********************\n");
printf(" ******************** Press 2 for switch (LED) 2 ********************\n");
printf(" ******************** Press 3 for switch (LED) 3 ********************\n");
printf("Switch: ");
scanf("%d", &led);
}
printf("\n\n");
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: \n\n");
printf(" ******************** Press 1 for 10 ohm resistance ********************\n");
printf(" ******************** Press 2 for 20 ohm resistance ********************\n");
printf(" ******************** Press 3 for 30 ohm resistance ********************\n");
printf("Resistance: ");
scanf("%d", &ohm);
}
while (flag == 0)
{
//LED-1
if(led== 1 && ohm== 1 )
{
printf("LED-1 is blinking 2 times\n");
}
if(led== 1 && ohm== 2)
{
printf("LED-1 is blinking 4 times\n");
}
if(led== 1 && ohm== 3 )
{
printf("LED-1 is blinking 6 times\n");
}
//LED-2
if(led== 2 && ohm== 1 )
{
printf("LED-2 is blinking 2 times\n");
}
if(led== 2 && ohm== 2 )
{
printf("LED-2 is blinking 4 times\n");
}
if(led == 2 && ohm == 3)
{
printf("LED-2 is blinking 6 times\n");
}
//LED-3
if(led == 3 && ohm == 1 )
{
printf("LED-3 is blinking 2 times\n");
}
if(led == 3 && ohm == 2)
{
printf("LED-3 is blinking 4 times\n");
}
if(led == 3 && ohm == 3)
{
printf("LED-3 is blinking 6 times\n");
}
printf("Do you want to continue Yes (Y) or No (N): ");
scanf("%s", &check);
if(check =='Y' || check =='y')
{
led = 0;
ohm = 0;
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close on: ");
scanf("%d", &led);
}
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: ");
scanf("%d", &ohm);
}
}
if(check=='N' || check=='n')
{
printf("Thanks for using the program");
flag = 1;
}
}
return 0;
}
In the statement scanf ("%s", &check); you're trying to scan a string and stuff it into a char. There are a couple of ways to fix this issue:
Quick fix: replace scanf("%s", &check) with scanf (" %c", &check). Notice the whitespace in the format string: " %c", not just "%c". The %c format specifier does not skip leading whitespace, so you have to include whitespace before the %c in your format string to explicitly signal to scanf() that you want to skip the leading whitespace. If you don't, then the following will happen:
the carriage return left in the input stream from the previous prompt Enter the resistance of Rheostat: will be taken as the input character. check will be assigned \n.
Both if conditions will fail; check is neither Y nor y, neither N nor n.
The execution will return to the top of the while loop for flag, which is still zero. So the output regarding the appropriate values of ohm and led will be displayed again, and then the prompt for check will be displayed again.
scanf() will check the input stream again, this time reading the actual choice the user enters, and do the appropriate thing.
By including the leading whitespace in your format string, you will avoid this duplicate output.
A better fix: flush the input buffer after each call to scanf(). Define a macro:
#define FLUSH while (getchar() != '\n')
and after each call to scanf(), type FLUSH;. It's a lot safer.
scanf("%s", &check); should be scanf("%c", &check); as you reading a char not a string.
The problem is that your variable "check" is too small.
scanf("%1s", check);
Because you are saving a string (with terminating \0) you should use a larger variable:
char check[2];
Edit: But yes, scanf("%c", &check) is far better if input is only one single character.
Its working fine for me in code-blocks ....
flush all the data from buffers i.e after reading the data input buffer flush the input buffer
i.e scanf(" %d", &led);
fflush(stdin);
by this, it will clears the input buffer after reading the data.

Resources