• Log in or sign up for an account here: https://www.sythe.org/login

Help a beginner out at c

Jason_x

Apprentice
Joined
Jan 31, 2007
Sythe Gold
0
Messages
939
Location
Ohio
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?
 
Clefairy
Supporting Business
Discord Voice
Jigglypuff
Hell yeah boooi
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
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?

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);
 
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.
 
You problem was you had an extra brace (by the looks of it).
 
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
 
Runescape Gamblingrunescape gambling
Back
Top