Im a newbie to C and i have some questions

This is a discussion on Im a newbie to C and i have some questions within the C Programming forums, part of the General Programming Boards category; Heres my main.c file, the questions are in the comments: BTW i just finished lesson 2 so dont flame me ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    Im a newbie to C and i have some questions

    Heres my main.c file, the questions are in the comments:

    BTW i just finished lesson 2 so dont flame me for not knowing the things I ask for help on.
    Attached Files Attached Files
    • File Type: c main.c (599 Bytes, 87 views)

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    this should help you, although I don't understand what your code is supposed to do.

    Code:
    #include <stdio.h>
    
    int main() 
    {
        int boolean;
        
        printf ( "Please Type the Number 1: " );
        /* how can i make it so i press a key and it goes to the next function?*/
        scanf ( "%d", &boolean ); /* scanf() will wait for you to press Enter, if you don't want it to wait for the Enter try using a function like getch() instead */
        printf ( "You Entered %d, now using boolean operators to determine true or false", boolean ); /* don't use the & reference operator on boolean, that will print the address of boolean */
        if ( boolean == 1 ) { /* test if the value of boolean is equal to 1, "%d" has no relevance here */
             int booleanvalue;
             
             printf ( "!( 1 || 1 && 0 )" ); /* I don't know what this is but I will let it be */
             scanf ( "%d", &booleanvalue ); /* I don't know what this is but booleanvalue is an int so use %d */
             printf ( "%d is TRUE", &booleanvalue ); /* I don't know what this is but again, booleanvalue is an int, not a character, so use %d */
        }
        /* Are both of these neccessay?*/
        getchar(); /* this is not necessary */
        return 0; /* this is necessary, read more about functions to understand it */
    }
    and here is my copy of your code, based off your code (and what it looks like you are trying to do):

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int boolean;
    
       printf("Please type the number 1 or 0: ");
       getch(&boolean);
       printf("You entered %d, now using boolean operators to determine true or false\n", boolean);
    
       if(boolean)
          printf("%d is TRUE\n", boolean);
       else
          printf("%d is FALSE\n", boolean);
    
       return 0;
    }
    the '\n' you see in those printf() strings is the newline escape sequence. those are just there to make the output look a little neater, they are not necessary.
    Last edited by Bleech; 07-05-2006 at 04:29 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Reading user input is usually line buffered. Functions that read it will block (halt a program) until there is input. Input comes when the user pushes enter. Otherwise, see this FAQ

    Code:
        /* Are both of these neccessay?*/
        getchar();
        return 0;
    Yes and no. The getchar() merely waits for input - you'll see it in examples becuase it will halt the program, keeping the console window from disappearing before you can read the output under some circumstanes. (It's not required, but helps sometimes.)
    The return 0; is. main() is declared to return and integer - yours is returning 0. 0 stands for success, anything else indicates failure.
    Finally:
    Code:
    printf ( "%c is TRUE", &booleanvalue );
    Use %d for variables of type int, and just pass booleanvalue, not &booleanvalue. For the scanf(), also use %d, but don't remove the &. (This goes for both printf()s.)
    Code:
        if ("%d" == 1 ) {
    Not sure what you mean to compare there, but that doesn't work. That'll always prove false - and you're trying to compare two very different things. Perhaps you mean if(boolean == 1) ?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    with that new code, when i press 1 it just closes the window, should i use a different program or the ms dos window?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int boolean;
    
       printf("Please type the number 1 or 0: ");
       getch(&boolean);
       printf("You entered %d, now using boolean operators to determine true or false\n", boolean);
    
       if(boolean)
          printf("%d is TRUE\n", boolean);
       else
          printf("%d is FALSE\n", boolean);
    
       return 0;
    }
    Last edited by pavel1104; 07-05-2006 at 06:27 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    ya, i meant the boonlean equals to 1 (True)

    Quote Originally Posted by Cactus_Hugger
    Code:
        if ("%d" == 1 ) {
    Not sure what you mean to compare there, but that doesn't work. That'll always prove false - and you're trying to compare two very different things. Perhaps you mean if(boolean == 1) ?

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    run the program from command prompt to stop the console window from closing like that when main() returns. this is explained in the FAQ here:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    getch() is NOT line buffered, like scanf() is. I used getch() in my example because you asked how you could input the key without having to press Enter (at least I think thats what you were asking?).

    you were using getchar() in your original program to wait for a carriage return (the Enter key is pressed) before main() returns. getchar() is another example of a line buffered function. you can add it to my example before the return statement, but I would suggest that you get used to executing your applications from the command line now, it will come in handy once you start learning how to pass arguments to main().
    Last edited by Bleech; 07-05-2006 at 10:14 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21