d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Doesnt The Last Two Scanf Work
Add Reply New Topic New Poll
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Sep 20 2012 05:51pm
Code
int main(void)
{
 char Quit = 'n';

   do
   {
  // Declare four variables and choose their appropriate type, such as int, or float. Use a meaningful name based on the following uses:

  // A variable to store the age (in years) of the user
   int age_years;
  // A variable to store the age in months.
   int age_months;
  // A variable to store the height in inches of the user.
   int height_inches;
  // A variable to store the height in centimeters.
   float height_centimeters;

   char chars;

  // Ask the user to input their age in years and store it to your age variable.
   printf ("\nWhat is your age in years? \n");
   scanf ("%i", &age_years);

  // Ask the user to input their height and store it to your height variable in inches.
   printf ("What is your height in inches? \n");
   getchar ();
   scanf ("%i", &height_inches);

  // Calculate the age in months and store it to the appropriate variable.
  age_months = age_years * 12;
  // Calculate the height in centimeters (2.54 cm per inch) and store it to the appropriate variable.
  height_centimeters = height_inches * 2.54;

  // Output the user's age in months and years, height in inches, and height in centimeters.
  // The height in centimeters should only display 2 decimal digits.
  printf ("You are %i years old \nYou are %i months old \nYour height is %i inches \nYour height is %0.2f centimeters\n \n", age_years, age_months, height_inches, height_centimeters);

  printf ("Type a single char \n");
  getchar ();
  scanf ("%c", &chars);
  printf ("You printed this char: %c \n", chars);

  // Misc for fun.. restart program
  printf ("Restart Program? (y/n) \n \n");
  getchar ();
  scanf ("%c", &Quit);
   }

  while (Quit == 'n' || Quit == 'N');

}


it wont even take input for the char i type and skips the restart program part

and then gives me unexpected exit (Using hercules to display this)

This post was edited by Haxs on Sep 20 2012 05:51pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 20 2012 06:14pm
your while loop condition is backwards. right now, the program will keep restarting as long as the user types "n" or "N" for the last part.
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Sep 20 2012 08:27pm
Quote (irimi @ Sep 20 2012 05:14pm)
your while loop condition is backwards.  right now, the program will  keep restarting as long as the user types "n" or "N" for the last part.


yea i fixed that now.

having trouble making it prompt me for typing a single char instead it just skips to the restart program part

using Hercules to display it btw

Code
int main(void)
{
 char Quit = 'y';


   do
   {
  // Declare four variables and choose their appropriate type, such as int, or float. Use a meaningful name based on the following uses:

  // A variable to store the age (in years) of the user
   int age_years;
  // A variable to store the age in months.
   int age_months;
  // A variable to store the height in inches of the user.
   int height_inches;
  // A variable to store the height in centimeters.
   float height_centimeters;

   char test_one;



  // Ask the user to input their age in years and store it to your age variable.
   printf ("\nWhat is your age in years? \n");
   scanf ("%i", &age_years);

  // Ask the user to input their height and store it to your height variable in inches.
   printf ("What is your height in inches? \n");
   scanf ("%i", &height_inches);

  // Calculate the age in months and store it to the appropriate variable.
  age_months = age_years * 12;
  // Calculate the height in centimeters (2.54 cm per inch) and store it to the appropriate variable.
  height_centimeters = height_inches * 2.54;

  // Output the user's age in months and years, height in inches, and height in centimeters.
  // The height in centimeters should only display 2 decimal digits.
  printf ("You are %i years old \nYou are %i months old \nYour height is %i inches \nYour height is %0.2f centimeters\n \n", age_years, age_months, height_inches, height_centimeters);

  printf ("Type a single char \n");
  getchar();
  scanf ("%c", &test_one);
  printf ("You printed this char %c \n", test_one);



  // Misc for fun.. restart program
  printf ("Restart Program? (y/n) \n");

  scanf ("%c", &Quit);
   }

  while (Quit == 'y' || Quit == 'Y');

}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 20 2012 08:33pm
try using fflush() rather then getchar();

i bet what is happening is that your character buffer is containing '\n' at the time of its call and its skipping user input.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 21 2012 01:42am
oh C....switch over to the dark side of ++!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll