Click here to Skip to main content
11,490,408 members (55,820 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
why this Code is Not Working

     void main()
     {
	char op;
	do
	{
          printf("Hello ");
 
	  printf("\nPress Y for continue and N for Exit");
	  scanf("%c",&op);
	}while(op == 'Y' || op == 'y');
     }

plz help me out similar code will working if i take op as integer and apply condition "while(op > 0)"

[edit]Code block added - OriginalGriff[/edit]
Posted 4-Nov-12 21:18pm
Edited 4-Nov-12 21:41pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It works fine for me - or at least it does exactly what I expect it to do:
Print "Hello"
Print "Press Y for continue and N for Exit"
Wait for me to type and press [ENTER]
Looks at the first character I type: If it is a upper of lower case 'Y' it repeats, otherwise it exits.

Have you tried typing "YYYYyyyyy" before you press [ENTER]?
Because you will find it loops 9 times if you do...

Basically, your code is looking for a reason to loop - the presence of a 'Y'. If it gets any other character, it exits. The [ENTER] key adds a '\n' character to the string, so when your code meets it, it exits the loop. You would be better off checking your inputs a bit more carefully! Laugh | :laugh:
  Permalink  
Comments
Rohit Pandit at 27-Nov-12 23:38pm
   
k Got it thxx........
OriginalGriff at 28-Nov-12 1:57am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this



char op;
    do
    {
       printf("Hello ");
 
      printf("\nPress Y for continue and N for Exit\t");
 
      scanf("%c",&op);
 
    }while(op == 'y' || op == 'Y' ||op =='\n');
  Permalink  
Comments
Rohit Pandit at 27-Nov-12 23:38pm
   
10 Qqqqqqqqqqqqqqqqq

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 460
1 Andy Lanng 258
2 Sascha Lefèvre 240
3 OriginalGriff 188
4 Maciej Los 160
0 Sergey Alexandrovich Kryukov 9,713
1 OriginalGriff 8,475
2 Sascha Lefèvre 3,544
3 Maciej Los 3,106
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100