Thread: "Very Basic" Double Posting

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    "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();
    }

    Thanks for reading,
    hopefully im not breaking any rules?

    ~Nerix
    Last edited by Nerix; 06-04-2008 at 01:15 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.
    Last edited by zacs7; 06-04-2008 at 01:11 AM.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    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 And im gunna try looking into a more detailed explaination of loops.

    ~Nerix

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There is a sticky about recommended books in this forum, http://cboard.cprogramming.com/showthread.php?t=74079

    There are a few tutorials on this site, http://www.cprogramming.com/tutorial/c/lesson3.html is about loops in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. New To C. Need Help
    By raortega3 in forum C Programming
    Replies: 3
    Last Post: 10-10-2007, 11:24 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM