我对被跳过的scanf语句有一些问题,但我读到它倾向于吃掉前一个,\n等等. 现在我已经让我的char实际通过了if语句,它只返回我的ELSE,即使我可以保证if语句获得了一个正确的值.

你们看到我的代码可能在哪里出错了吗?

我没有包括我的头文件或任何东西,因为它是一个相当大的代码块,但它到目前为止运行得很好.

  char oakAns;

  printf("Would you like the sign made out of oak?\n");
  printf("Enter y for yes or n for no: ");
  
  scanf(" %c",&oakAns);
  
  printf("%c",oakAns);  //this is what i was using to confirm my oakAns input

  
  if((oakAns == 'y')||(oakAns == 'Y')){          // if y then add 15
    cost += 15;
  } 
  if((oakAns == 'n')||(oakAns == 'N')){    // but if n then remain same
    cost = 30;
  }
  else {                      // anything else returns false
    printf("Could not compute cost...\n");
  
    return false;
  }

推荐答案

这似乎是if语句中的问题所在.而不是这些语句

  if((oakAns == 'y')||(oakAns == 'Y')){          // if y then add 15
    cost += 15;
  } 
  if((oakAns == 'n')||(oakAns == 'N')){    // but if n then remain same
    cost = 30;
  }
  else {                      // anything else returns false
    printf("Could not compute cost...\n");

你应该写信给我

  if((oakAns == 'y')||(oakAns == 'Y')){          // if y then add 15
    cost += 15;
  } 
  else if((oakAns == 'n')||(oakAns == 'N')){    // but if n then remain same
    cost = 30;
  }
  else {                      // anything else returns false
    printf("Could not compute cost...\n");

这就是这条if语句

  if((oakAns == 'n')||(oakAns == 'N')){    // but if n then remain same

应该是上一个IF语句的其他部分.

否则,此print tf调用输出的消息

    printf("Could not compute cost...\n");

如果用户输入'y''Y',则始终输出.

C++相关问答推荐

如何将一个integer与一个数组进行比较?

修改pGM使用指针填充2-D数组但不起作用

C指针地址和转换

在 struct 中强制转换空指针

实现简单字典时C语言中的段错误

GCC不顾-fno-Builtin-SINCOS旗帜向SINCOS发出呼唤

从uint8_t*转换为char*可接受

在for循环中指向数组开头之前

如何使用FSeek和文件流指针在C中查找文件的前一个元素和前一个减go 一个元素

如何使用libgpio(d)为Raspberry Pi编译C程序?

Tic-tac-toe:从文件加载存储

C语言中的数字指针

我应该在递归中使用全局变量吗

从CentOS 7到Raspberry PI 2B的交叉编译-无法让LIBC和System Include标头一起工作

使用正则表达式获取字符串中标记的开始和结束

使用fread()函数读取txt文件

`void foo(int a[static 0]);` 有效吗?

段错误try 访问静态字符串,但仅有时取决于构建环境

无法在 C 中打开文本文件,我想从中读取文本作为数据并将其写入数组

将帧从相机 (/dev/video0) 复制到帧缓冲区 (/dev/fb0) 会产生意外结果