c - 为什么我的程序不允许我进行第二次输入?

标签 c

假设储蓄支出是已给定值的double类型变量。编写一个 if-else 语句,输出单词 Solvent,将 Savings 的值减去费用的值,并将费用的值设置为 0(前提是节省的费用大于费用)。但是,如果储蓄小于支出,则 if-else 语句仅输出单词破产,并且不会更改任何变量的值。

具体问题:为什么我的程序在我输入储蓄值后立即跳过?我该如何补救?

#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;
}

最佳答案

好吧,出于我不太明白的原因,将 %2lf 设为 %lf 足以解决此问题。感谢风向标。

关于c - 为什么我的程序不允许我进行第二次输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49182537/

相关文章:

c - 串行通信 C/C++ Linux 线程安全吗?

c - 使用 fscanf 从文件中读取字符串和整数

c - scanf 上的段错误并非在所有系统上都存在

c - 测试我是否应该释放内存

c - 如何修复C中的 ‘memory leak’

c++ - 符合标准的 C assert() 可以多次评估其参数吗?

c - 在 C 中实现有限状态机

c - 在c中写入一个2位int,仅包含<unistd.h>

c - linux c mmap/mprotect 问题

c - 读取/proc/pid/mem 文件不返回任何内容