• 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
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?
 
Help a beginner out at c
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);
 
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.
 
Help a beginner out at c
You problem was you had an extra brace (by the looks of it).
 
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
 
Runescape Gamblingrunescape gambling
Back
Top