-1

Suppose savings and expenses are variables of type double that have been given values. Write an if-else statement that outputs the word Solvent, decreases the value of Savings by the value of expenses and sets the value of expenses to 0, provided that savings is more than expenses. If however, savings is less than expenses, the if-else statement simply outputs the word Bankrupt and does not change the value of any variable.

Specific question: Why does my program skip immediately through after I type in a value for savings? How can I remedy it?

#include <stdio.h>

void solvent();

int main(void)
{
   double savings, expenses;

   printf("\nEnter a number for savings: ");
   scanf("%2lf", &savings);
   printf("Enter a number for expenses: ");
   scanf("%2lf", &expenses);


   if(savings > expenses)
      solvent();
   else
      printf("Bankrupt!");

   system("pause");
   return 0;
}

void solvent(double savings, double expenses)  
{
   printf("Solvent!");

   savings -= expenses;
   expenses = 0;
}
Tormund Giantsbane
  • 355
  • 1
  • 4
  • 12
ACH
  • 339
  • 1
  • 2
  • 12
  • 2
    Stop using scanf. Just stop. It is a terrible function to try to learn at the same time that you are learning the language. Pass parameters as command line arguments, and read inputs using `fread` and `fgets`. – William Pursell Mar 08 '18 at 21:15
  • 1
    This has to be a dupe; put a space in your `scanf` format to eat the whitespace. – Carl Norum Mar 08 '18 at 21:15
  • 2
    Remove the `2` from `"%2lf"`. Function `scanf` has some similarities with `printf` but is different. Also, please see [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Weather Vane Mar 08 '18 at 21:17
  • 1
    @CarlNorum, `%lf` will skip whitespaces. The problem might be that the user is entering more than 2 digits. – R Sahu Mar 08 '18 at 21:17
  • @CarlNorum means donthis `scanf (" %2lf", &saving)` –  Mar 08 '18 at 21:19
  • @GRC those are [HTML entities](https://www.w3schools.com/html/html_entities.asp). Neither C nor console output will recognise them. – Weather Vane Mar 08 '18 at 21:24
  • I'd love to not use scanf(), but this work for a class and it's the only thing we've covered so far so I have to use it. Weather Vane, thank you for solving. – ACH Mar 08 '18 at 21:28
  • 1
    At least check the return value of any of the scanf family of functions, I'm sure that is not forbidden. If you don't, parse errors will lead to really confusing behavior, quite often even Undefined Behavior... – hyde Mar 08 '18 at 21:29

1 Answers1

0

Okay, for reasons I don't quite understand, making %2lf be just %lf was sufficient to fix this. Thanks to Weather Vane.

ACH
  • 339
  • 1
  • 2
  • 12
  • 1
    Reading the man pages will help you. The `scanf` and `printf` family functions are complicated, similar but different, so it will take some detailed study to understand them. – Weather Vane Mar 08 '18 at 21:29
  • What's to understand? Printf takes a precision value for floats; scanf doesn't. That's how they're defined. – Lee Daniel Crocker Mar 08 '18 at 21:30