Click here to Skip to main content
12,356,784 members (62,485 online)
Rate this:
 
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 20:18pm
Edited 4-Nov-12 20: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 27-Nov-12 23:38pm
   
k Got it thxx........
OriginalGriff 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2016
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