Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why this Code is Not Working

C++
 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
Updated 4-Nov-12 20:41pm
v2

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:
 
Share this answer
 
Comments
Rohit Pandit 27-Nov-12 23:38pm    
k Got it thxx........
OriginalGriff 28-Nov-12 1:57am    
You're welcome!
Try this



C#
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');
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900