c - While 循环 - 改变字符串变量

标签 c string while-loop

作为 c 编程的新手(我只有 visual basic 方面的经验),我不完全确定条件语句中带有不断变化的字符串变量的 while 循环应该如何运行。

下面的代码是我制作的一个简单的计算器,它允许用户输入一个运算和两个数字,然后输出相应的结果。我试图在一个 while 循环中编写代码,该循环不断重复该过程,直到用户决定退出它。但是,似乎行 scanf("%c", &quit);不影响 while 循环条件语句。

#include <stdio.h>

int main() {
float num1, num2;
char operation;
char quit = "n";
while (quit = "n"){
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
    switch(operation) {
        case '+':
            printf("%f\n", num1+num2);
            break;
        case '-':
            printf("%f\n", num1-num2);
            break;
        case '*':
            printf("%f\n", num1*num2);
            break;
        case '/':
            printf("%f\n", num1/num2);
            break;

    }
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}

提前感谢您的所有帮助。

最佳答案

你可以这样做

#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = 'n';
//while (quit = "n") //
  while (quit!= 'y')
 {
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
    case '+':
        printf("%f\n", num1+num2);
        break;
    case '-':
        printf("%f\n", num1-num2);
        break;
    case '*':
        printf("%f\n", num1*num2);
        break;
    case '/':
        printf("%f\n", num1/num2);
        break;

}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
 }
return 0;
}

关于c - While 循环 - 改变字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571422/

相关文章:

c - 获取注册文本文件的第一个计数

java - 有没有办法在 Java 中将字符串(逐个字符)转换为其 ASCII 值?

ruby-on-rails - String#pluralize 是幂等的吗?

c - 嵌套在循环中的公式将无法正确执行

php - 使用 fetch_assoc 从数据集中获取特定行?

c - 获取网关以用于 ANSI C 中的给定 ip

c - 系列使用程序

c++ - std::to_string 存储在 const char*

java - 为什么java中的while循环会运行?

c - Glade3 和 C 编程