# "Very Basic" Double Posting

• 06-04-2008
Nerix
"Very Basic" Double Posting
Hello,

Uhm I just started to learn C (It is very fun and challenging) however i have one little question. I cant think of a reason to why the function "printf" is posting 2 times?

Here is the code:
Code:

``` #include <stdio.h> int main() {     int x;     int y;     int v;     int c;     printf( "Please input a value to test the value of X. {Y = ___ + ( x - 4 )}: " );         scanf( "&#37;d", &v );     x = 13; y = v + ( x - 4 );     do {         printf( "Your answer is: %d\n", y);         getchar();     }  while (c != 13 ); /* Not actually sure how to use.... explain please? */ }```
I know its very basic (just started learning today). Also if you dont mind answering 2 more little questions, please do.

1) I could use "for" instead of "do" right? Whenever I do, i cannot get it to work correctly like i can with "do" however i dont know how to use the "while" function, and i cant find a way to leave that out.

2) Could I use the "while" function to return to the top, to redo the equation if the value returns false, and if true, use printf to say "correct" or whatever? Would that be efficent? Is there another way to do the exact same thing?

Here is a guess I made towards this?
Code:

```  int c; /* this is the int value ill assign at the beggining, just included so you know that i declared C as a intiger */       printf( "What is your guess for the value of X?: "             scanf( "%d", &c ) }         if ( c = 13 ) {             printf( "Your Correct!\n" ); }         else {             printf( "Please Try Again, Your Incorrect!" );  /* how would i tell it to return to the top if incorrect? */       } while (c != 13 ); /* Not actually sure how to use.... explain please? I need to tell this to return false, then make it return to the top? Is there a more efficent way? */       getchar(); }```

hopefully im not breaking any rules?

~Nerix
• 06-04-2008
zacs7
Code:

`if ( c = 13 ) {`
You're assigning the value of 13 to 'c', not checking if it is 13

Code:

```if(c == 13) {```
Is correct.

You could theoretically use any loop you want here, the real issue is picking the right one for the job.

Code:

```DO     READ n     IF n == 13         PRINT "Guessed right!"     END IF WHILE n != 13```
It will stop when n is 13, or keep looping until it is, a good book could really help.
• 06-04-2008
Nerix
Could you recommend a good book to read up on? Im really interested in learning, and there are plenty of tutorials, but they are somewhat jumbled, and dont go into as much details as i would like.

Btw I never thought about it like that, i shouldn't have just been assigning the value, my mistake :p And im gunna try looking into a more detailed explaination of loops.

~Nerix
• 06-04-2008
zacs7