empty string

hi, i have problem with this program, i want the program to give a command after a certain time if user not put the input

#include <stdlib.h>
int main()
{

int you;
char years[8];
printf("how old are you : ");

gets(years);
if( years==0)
{
printf ("please enter your age!!!");
}
else;{
you=atoi(years);
printf("You are %d years old.\n",you);
}
return(0);
}
Why do you have years as a char array? Use int you instead
closed account (j3A5Djzh)
Hi,
your program is wrong, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* C version */
#include <stdlib.h>

int main(void)
{
    int age = 0;

    printf("How old are you?: ");
    scanf("%d", &age); // %d = decimal number, &age = pointer to variable "age"

    if( age == 0)
        printf ("ERR: Please enter your age!\n");
    else
        printf("You are %d years old.\n", age);

    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* C++ version */
#include <iostream>

using namespace std;

int main(void)
{
    int age = 0;

    cout << "How old are you?: ";
    cin >> age;

    if( age == 0 )
        cout << "ERR: Please enter your age!\n";
    else
        cout << "You are " << age << " years old.\n";

    return 0;
}
1
2
3
4
else;{
you=atoi(years);
printf("You are %d years old.\n",you);
}

no need of ; after else .
there's no need for void inside main()
Topic archived. No new replies allowed.