Bryon.R
Bryon.R

Reputation: 11

C: Need to input my value twice after running program

New to programming in general. Learning from C Programming:Absolute Beginner. Tried writing my own code from scratch and it works but I have to input my value twice for the rest of the statements to run. Can you see the issue?

EDIT: I've probably added an over complicated code to add leap year (days) on to the days already calculated. I know the answer will be a float. How ever im getting an error saying my statements specify 'double' but my arguement is 'int'. How do I convert the answer to a float?

Thanks in advance!

//First Practice
#include <stdio.h>

  // This simple program lets user know how many days old they are
  // by how many times Earth has rotated in relation to their age.
int main(void)
{
   int ans, age, er, leap;
   float number_of_leap_years;
   er = 365; //Earths' rotations per year
   leap = 1460;


   {
      printf("How old are you in years? \n");
      printf("Input Years: ");
      scanf("%d", &age);
   }

   if (age <=30)
   {   number_of_leap_years = (age * er) / leap;
      ans = age * er + number_of_leap_years;

      printf("You are %d days old! \n", ans);
      printf("The earth has spun %2f times since you were born!\n", ans);

   }



   else

   {
      number_of_leap_years = (age * er) / leap;
      ans = age * er + number_of_leap_years;

      printf("You are  %d days old! \n", ans);
      printf("The earth has spun %2f times since you were born!\n", ans);
      printf("Arent you dizzy??\n");


   }
   return 0;
}

Upvotes: 1

Views: 380

Answers (1)

anoopknr
anoopknr

Reputation: 3355

You have mistake in your line :-

scanf(" %d years old", &age);

change it to :-

scanf("%d", &age);

Modified code :-

#include <stdio.h>

// This simple program lets user know how many days old they are
// by how many times Earth has rotated in relation to their age.
int main(void)
{
  int ans, age, er;
  er = 365; //Earths' rotations per year

  {
    printf("How old are you in years? \n");
    printf("Input Years: ");
    scanf("%d", &age);        // no extra characters in scanf
  }

  if (age <= 30)
  {
    ans = age * er;

    printf("You are %d days old! \n", ans);
    printf("The earth has spun %d times since you were born!\n", ans);
  }
  else

  {
    ans = age * er;

    printf("You are  %d days old! \n", ans);
    printf("The earth has spun %d times since you were born!\n", ans);
    printf("Arent you dizzy??\n");
  }
  return 0;
}

Output :-

How old are you in years? 
Input Years: 5
You are 1825 days old! 
The earth has spun 1825 times since you were born!

Why don't you take care of leap years ?

Upvotes: 1

Related Questions