• 02-28-2010
SilentPirate007
If you have a look at the following code:

Code:

```#include <stdio.h> int main() {           int answer;//users answer input                   {           printf("\nIf:");           printf("\n2 + 3 = 10\n");           printf("7 + 2 = 63\n");           printf("6 + 5 = 66\n");           printf("8 + 4 = 96\n");           }           do           {           printf("\nThen:\n");           printf("\n9 + 7 = ");           scanf("%d", &answer);           }           while(answer != 144);//only loops if the answer is anything but 144           return 0;  }```
If you compile and run the program, it would look like this:

If:
2 + 3 = 10
7 + 2 = 63
6 + 5 = 66
8 + 4 = 96

Then:

9 + 7 = 27// red only implies its wrong in this example for demonstration purposes'.

Then:

9 + 7 = 144//The program will now end

What I really want is that the program give the statement "Try again y/n?" so the user can opt to try again. Example:

If:
2 + 3 = 10
7 + 2 = 63
6 + 5 = 66
8 + 4 = 96

Then:

9 + 7 = 23

Wrong! Try Again "y" or "n"? //if the answer is y, then the program will loop the same way until the user either types n or gets it right.
• 02-28-2010
nonpuz
Well you need to adjust the structure of your
Code:

` do { } while`
loop to fringe on the Y/N answer, not the answer == 144. Which also means you will need to add a
Code:

`if (answer == 144) break;`
statement if you want it to also terminate on the answer equaling 144, or you could also include that in the while condition but that would be a bit messy.

Reading in Y/N answer properly is beyond this response but I'll tell you using
Code:

`scanf("%c",&some_char)`
is definitely
not a good solution.

You should use fgets and an appropriate buffer, this is because you are likely to leave user input in the buffer. Which means it will likely pass that to the next scanf call instead of getting new user input. Even with fgets you could still easily leave user input in as you are not making an unbuffered call so the user is free to type pages of data when you only want a single Y/N input heh.
• 02-28-2010
karthigayan
Is this you want ,

Code:

```#include <stdio.h> int main() {         int answer;//users answer input         char opt[2];         {                 printf("\nIf:");                 printf("\n2 + 3 = 10\n");                 printf("7 + 2 = 63\n");                 printf("6 + 5 = 66\n");                 printf("8 + 4 = 96\n");         }         while(1)         {                 printf("\nThen:\n");                 printf("\n9 + 7 = ");                 scanf("%d", &answer);                 if(answer==144)                 {                         printf("It's right\n");                         break;                 }                 else                 {                         printf("Wrong ! Try again ( Y(y) / N(n)) : "); again:                         scanf("%s",opt);                         if(strcmp(opt,"Y")==0||strcmp(opt,"y")==0 )                                 continue;                         else if(strcmp(opt,"N")==0 ||strcmp(opt,"n")==0)                                 break;                         else                                 printf("Invalid option (Y(y) / N(n))");                         goto again;                 }         } }```
Thanks
• 03-01-2010
Elysia
Don't hand out solutions! Neither you nor anyone else will benefit for it!
Furthermore, you should work on your own code: do NOT use goto!
All can be solved with a little logic. Write a flowchart, then write out the code.
• 03-01-2010
Tool
Dont use scanf.
Code:

```do           {           printf("\nThen:\n");           printf("\n9 + 7 = ");           scanf("%d", &answer);           }           while(answer != 144);```
If your input is a letter, this will end up being a infinite loop.