I'm not as advance in C as yet so I'd need help with few problems. 1. Let's say I want to enter character (y or n) and I don't want anything else other than that character, so I'll use a while loop to until its entered. I can't get it working, here's my code. It compiles and run but it won't carry out what I want, if I enter y it continues to loop likewise n
printf("Enter code(y/n): \n");
scanf(" %c", &code);
while (code != 'y' || 'n' ){
printf("Try Again: \n");
scanf(" %c", &code);
}
REPLACE
while (code != 'y' || 'n' ){ <-- condition evaluates always to TRUE
WITH
while ((code != 'y' )&&(code != 'n')){
The line you have
while (code != 'y' || 'n' ){
is equivalent to:
while ((code != 'y') || 'n' ){
which evaluates to true all the time.
The logic you need is:
while (code != 'y' && code != 'n' ){
Related
So I am trying to get a simple y/n input to work. I have it working and it will break a while(1) loop when n or N is entered, however, any other alphabetical character will set the loop to go again. I only want it to loop when Y is entered.
I have tried:
if (try_another != 'n' || try_another != 'N' || try_another != 'y' || try_another != 'Y'), after the !isalpha line, and this did not work.
I have tried scanf (" %c", try_another); and then comparing the assigned characters.
this is my current code set up:
printf("Do you wish to try another problem [y/n]: ");
do{
try_another = getchar();
/*keeps scanning for input until its a letter*/
}while(!isalpha(try_another));
/*when input is n or N it will end the program*/
if (try_another == 'n' || try_another == 'N'){
break;
}
Why don't you do this?
do {
try_another = getchar();
} while(try_another != 'n' && try_another != 'N' && try_another != 'y' && try_another != 'Y');
You want to loop while the character isn't this and isn't that. You were close, but accidentally wrote "or" instead of "and".
In the following code no matter what I do it loops forever and showing "syntax error" forever.
This code needs to check if the answer is neither 'y' nor 'n'. If it is ask again for answer and then if answer 'y' it will loop again all the func
and if answer is 'n' it should do nothing...
char answer = ' ';
int round = 0;
do
{
round = chooseLevel();
guess(round);
while ((answer != 'y') || (answer != 'n'))
{
printf("\nWould you like to play again? (y/n): ");
answer = getch();
putch(answer );
printf ("\nchar is %c\n", answer );
if ((answer != 'y') || (answer != 'n'))
{
printf("Syntax Error");
}
}
}
while (answer == 'y');
The condition should be with && and not || :
(answer != 'y') && (answer != 'n')
The answer cannot be both 'y' and 'n', so either answer != 'y' or answer != 'n' will be true. In case of || the whole statement will become true.
True || False => True
Switch || to &&. Currently, if answer is y, both if and while conditions will evaluate to ('y' != 'y') || ('y' != 'n') or (false) || (true) = true. The only way to get out of the while loop is to have answer somehow equal to both y and n at the same time.
I've been trying to get this code to work but the loop does not seem to work? I am very new to C and I sort of get confused with the syntax of this language. However my loop is not functioning like how I want it to be. I want the if and else statement to work but no matter what input (right or wrong) it always outputs "thank you".
#include <stdio.h>
#include <stdlib.h>
int confirm()
{
char c;
printf("Confirm (y/n): ");
scanf("%c", &c);
while (scanf("%c", &c))
{
if (c = 'Y' && 'y' && 'N' && 'n')
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
int main(int argc, char* agrv[])
{
confirm();
return 0;
}
it won't ask to enter another output when the output is incorrect. It just keeps ending from the if statement, thus the loop is not running?
Please help.
There's nothing wrong with your loop - it's the if statement that's wrong.
This code compiles, but it does not do what you want it to do:
if (c = 'Y' && 'y' && 'N' && 'n')
= is an assignment; you need == to do a comparison
&& means "AND"; you need ||, which means an "OR"
You combine logical expressions, not constants with && or ||
The condition should be
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')
Also note that when you read single characters with %c, your program "sees" all characters, including whitespace. This is a problem, because the '\n' left over in the buffer will be passed to your program before Y or N. To fix this, add a space before %c to your format string:
scanf(" %c", &c)
// ^
// |
// Here
Your code also ignores the first character that it reads. I think this is not intentional, so remove the call of scanf before the loop. You should also remove the second scanf from the loop, leaving the only call to scanf in the loop header.
int confirm()
{
char c;
printf("Confirm (y/n): ");
//scanf("%c", &c);// <---------- needless
while (scanf("%c", &c)) //<----while loop will do `scanf("%c",&c)`, so previous line should be remove.
{
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')// <- &&(AND); ||(OR). Also, be careful that don't be lazy, [c == 'Y' || 'y' || 'N' || 'n'] can't to communicate with computer
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
So i have this peace of code:
int choice_dig;
char choice_dup;
printf("Welcome to the mystery number game.\n");
printf("How many digits do you want to use (3 to 5)?");
scanf("%d", &choice_dig);
while (choice_dig<3 || choice_dig>5)
{
printf("\nPlease choose a number between 3 and 5.\t");
scanf("%d",&choice_dig);
}
printf("\nDo you want to allow duplicate digits (y or n)?");
scanf(" %c", &choice_dup);
while (choice_dup != 'y' || choice_dup != 'n')
{
printf("\nInvalid entry. Please choose y for yes and n for no.\t");
choice_dup = getchar();
getchar();
}
The choice_dup is already assinged as a char var at the start of the main. So when i run this its all good. But even when i press y or n it cant recognize it and the loop never ends. No matter what i type. Can someone help me and expain to me what wrong?
The loop will run forever because while (choice_dup != 'y' || choice_dup != 'n') will always evaluate to true.
You probably wanted: while (choice_dup != 'y' && choice_dup != 'n')
should be this:
printf("\nDo you want to allow duplicate digits (y or n)?");
scanf(" %c", &choice_dup);
while (choice_dup != 'y' && choice_dup != 'n')
{
printf("\nInvalid entry. Please choose y for yes and n for no.\t");
choice_dup = getchar();
getchar();
}
Just use
while (choice_dup != 'y' && choice_dup != 'n') instead
In the last while loop,i.e the one for choice_dup,change it to:
while(!(choice_dup=='y'| choice_dup=='n'))
I must tell you that i' ve already searched thi site and other, but i couldn't solve my problem. I'm doing a program that works on some lists. The code copies the values form the second list to the first, with this condition: the current value of list 2 must be lesser than the value in list 1. If this condition is true, the value is copied. But that's not the point. I made a function called GetExitValue(), which at the end of main, gets the input from the user whether to continue or not. And this is the problem. When i call the function in main (inside of a while) , it continues to run the program even if i insert n (No). Can you tell me what am i doing wrong?? Thank you very much guys!!
And that's my code semplified.
int main(){
do{
.
.
.
.
printf("\nProgram finished. Do you wish to re-execute the program? \n\nPress y/Y (Yes) to continue. Press n/N (No) to exit. ");
}
while(GetExitValue() == 'y' || 'Y' ); // **Problem here!!**
return 0;
}
char GetExitValue(){
char exit_value;
scanf(" %c", &exit_value);
while(exit_value != 'y' && exit_value != 'n' && exit_value != 'Y' && exit_value != 'N'){
printf("Value incorrect. insert y or n!!\n");
scanf(" %c", &exit_value);
}
return exit_value;
}
This part:
while(GetExitValue() == 'y' || 'Y' ); // **Problem here!!**
which you've correctly spotted, is probably not doing what you meant. This always is "true" because 'Y' (which is not 0 in ASCII) is always "true".
You literally have this in your code: while (condition || 1) which can be useful, but not in this case.
You probably mean this:
.
.
// run GetExitValue() only once...
char gev = GetExitValue();
}
while(gev == 'y' || gev == 'Y' );
And I'll just point out that you can do something like:
.
.
}
while(GetExitValue()); // no need to store value!
if only you'd change GetExitValue() to return 0 when user does not want it to run again:
char GetExitValue(){
char exit_value;
scanf(" %c", &exit_value);
while(exit_value != 'y' && exit_value != 'n' && exit_value != 'Y' && exit_value != 'N'){
printf("Value incorrect. insert y or n!!\n");
scanf(" %c", &exit_value);
}
return exit_value == 'Y' || exit_value == 'y'; //evaluates to 1 if true
}
It will probably be cleaner, and you can change it's name to shouldRunAgain() which to my personal taste is more informative :)
The while expression is evaluated as:
while( ( GetExitValue() == 'y' ) || 'Y' );
So the expression is always true because anything or true == true.
Your problem is here-
while(GetExitValue() == 'y' || 'Y' );
The 'Y' part will always be true, because in C, anything that isn't a zero(false) is true.
the ASCII value of 'Y' isn't zero, therefore, the while condition will always return true.
What you should do is this-
`char x=GetExitValue();
while(x == 'y' || x=='Y' );`