-1

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?

GeorgeS
  • 19
  • 6
  • 1
    One of the conditions is always true -> their disjunction will always be true. – EOF Nov 11 '14 at 18:39
  • `choice_dup` will always be either not `y` or not `n` – Dmitri Nov 11 '14 at 18:39
  • The problem is in your condition. You need to check using AND instead of OR. When you hit 'Y' it is different to 'N'. You want to validate when it's different to 'Y' AND different to 'N' – antorqs Nov 11 '14 at 18:41
  • The root cause is an inability to debug. If you had broken up the complex conditional into two expressions with intermediate booean vars for use in the while, you would surely have spotted the logical error, either with a debugger or with printf logging. You should stop writing code now and learn some debugging. – Martin James Nov 11 '14 at 20:06
  • after this line: scanf(" %c", &choice_dup); the code needs to perform: choice_dup = tolower( choice_dup ); so the user can use 'N' and 'Y' along with 'n' and 'y' – user3629249 Nov 12 '14 at 01:03

4 Answers4

3

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

Ben Griffiths
  • 1,676
  • 15
  • 13
  • Yea now that you mentioned it thats a pretty stupid mistake. Thanks again so much! :D – GeorgeS Nov 11 '14 at 19:03
  • I have another problem now xD. For some reason when i run it even if i press y or n it doesnt work. But when i press 'y' it works but again not properly. When i press 'y' or 'n' it runs again the printf order and then the proggram ends. Why? Can someone explain it to me? Also when i type more chars tha only one (exp. When i type: ysh it will run the printf coomand 3 times) it does this. – GeorgeS Nov 11 '14 at 19:11
  • Well, the first thing that needs addressing is the fact that getChar(), which I assume reads in one character from stdin, is called twice in the loop. But in order to get a definitive answer as to what is happening, I guess you would need to post more code :-) – Ben Griffiths Nov 11 '14 at 19:21
0

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();
}
DJ Burb
  • 2,346
  • 2
  • 29
  • 38
0

Just use

 while (choice_dup != 'y' && choice_dup != 'n') instead
Aadil Ahmad
  • 139
  • 9
0

In the last while loop,i.e the one for choice_dup,change it to:

while(!(choice_dup=='y'| choice_dup=='n'))
Miss J.
  • 351
  • 1
  • 5
  • 15