Saturday, August 29, 2020

int.c : get an integer from user

#include <cs50.h>
#include <stdio.h>

int main(void)
{

int age = get_int("What's our age?\n");
int days = age*365;
printf("You are at least %i days old\n", days);

}



better design:

#include <cs50.h>
#include <stdio.h>

int main(void)
{

int age = get_int("What's our age?\n");

printf("You are at least %i days old\n", age*365);

}



you can even do this: (but in terms of design, this may reach an inflection point at which the code becomes too hard to read)

#include <cs50.h>
#include <stdio.h>

int main(void)
{

printf("You are at least %i days old\n", get_int("What's your age?\n")*365);

}


[Source: harvard, cs50]


No comments:

Post a Comment