Help a beginner out at c

Discussion in 'Programming General' started by Jason_x, Jun 27, 2010.

Help a beginner out at c
  1. Unread #1 - Jun 27, 2010 at 9:21 PM
  2. Jason_x
    Joined:
    Jan 31, 2007
    Posts:
    939
    Referrals:
    0
    Sythe Gold:
    0

    Jason_x Apprentice

    Help a beginner out at c

    Code:
    #include <stdio.h>
    int main()
    {
        int age;      /*I want to kill this language*/
        
        printf("Please enter your age\n");  
        scanf("%d", &age); {      
                    if (age < 100){     
                    printf("Youre young.");
                    }
                    else if (age == 100){
                    printf("Youre old as dirt.");
                    }
                    else {
                         printf("This is pointless\n");
                         
                         }
                    return 0;
                    }
                    }
    
    My problem isnt compiling the damn thing its executing it, whenever I run it and input a variable it does nothing and when I hit enter the return 0; closes it. I am a complete noob at C so what am I doing wrong?
     
  3. Unread #2 - Jul 2, 2010 at 12:14 AM
  4. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,075
    Referrals:
    468
    Sythe Gold:
    5,290
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Help a beginner out at c

    Why do you have an arbitrary codeblock inside main, and what is with your indentation?

    Code:
    
    #include <stdio.h>
    int main()
    {
        int age = 0; 
        
        printf("Please enter your age\n");  
        scanf("%d", &age);     
    
        if (age < 100) {     
            printf("You're young.\n");
        } else if (age == 100) {
            printf("You're 100 years old.\n");
        } else {
            printf("You're older than 100 years old.\n");
        }             
        
        return 0;
    }
    

    ^ This works fine for me.

    If you're having trouble receiving input from the keyboard, then try fgets. It will retrieve a whole line, then you can use sscanf on it to parse its logical elements.

    Like this:
    Code:
    char buffer[1024];
    buffer[0] = '\0';
    fgets(buffer, 1023, stdin);
    printf("line was: %s", buffer);
    
     
  5. Unread #3 - Jul 2, 2010 at 10:14 AM
  6. Jason_x
    Joined:
    Jan 31, 2007
    Posts:
    939
    Referrals:
    0
    Sythe Gold:
    0

    Jason_x Apprentice

    Help a beginner out at c

    Ok thank you alot I was beginning to think noone was gonna say anything.
    So my problem was not putting a variable at the beginning?
    The code block is the way the tutorial showed me since the college I go to doesnt offer c programming as a class I have to follow internet tutorials lol.
     
  7. Unread #4 - Jul 5, 2010 at 2:57 AM
  8. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Help a beginner out at c

    You problem was you had an extra brace (by the looks of it).
     
  9. Unread #5 - Jul 12, 2010 at 6:14 PM
  10. ET Phone Home
    Joined:
    Sep 8, 2008
    Posts:
    663
    Referrals:
    1
    Sythe Gold:
    0

    ET Phone Home Apprentice

    Help a beginner out at c

    sometimes i have trouble with my programs immediately closing because of the return 0;, so i usually put "getchar();" just before it, so the program will finish once you make a keystroke. I think it requires the conio.h library

    also this is probably bad programming but as a novice who frequently has the same problem thought id offer my 2 cents
     
< nid help pls | guys >

Users viewing this thread
1 guest


 
 
Adblock breaks this site