Question

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;
        }
Was it helpful?

Solution

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 :)

OTHER TIPS

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' );`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top