c - 如何格式化代码以便触发 while 循环 C

标签 c while-loop formatting boolean-logic

所以我有两个 while 循环。最外面的一个并没有按照我想要的方式循环。

我第一次尝试:

#define true 1
#define false 0

然后将其设置为我在下面所做的。

我的问题是,当我运行该程序时,它会在无效输入后终止。如果输入的内容不正确,我希望它能循环回到开头。

#include <stdio.h>

int main()
{
    int user_number;//create a new integer variable
    int last_multiple;//create a new integer variable for max in mult table
    int stilltrue = "false"; //create integer to use in while
    while (stilltrue == "false"){//initiate while loop
    printf("Please enter a positive integer you would like the table for and"
           " hit enter, then enter the max value of its multiplication "
           "table: \n");
//takes two inputs and makes sure they are integers
    if(scanf("%d",&user_number) == 1 && scanf("%d",&last_multiple)== 1){
        printf("The number you typed was %d and the "
           " value was %d \n\n",user_number,last_multiple);

    while (last_multiple > 0){

        int multiple;//multiple of user_number and last_multiple
        multiple = user_number * last_multiple;
        printf("%d x %d is %d \n\n", user_number,last_multiple,multiple);
        last_multiple = last_multiple - 1;//decrease the last_multiple value by 1
        stilltrue = "true";
    }

    } else {
        printf("That was not an integer. \n");
        stilltrue = "false"; //This isn't needed here

    }break;
    }
    return 0;

}

那么如果它到达这个位置,我该如何修改才能让它循环回来

else {
        printf("That was not an integer. \n");
        stilltrue = "false"; //This isn't needed here

    }

感谢您的帮助!

最佳答案

#define true 1
#define false 0

这些是指令,#define 指令会导致编译器用标记字符串替换源文件中每次出现的标识符。

只需使用 truefalse 代替 "true""false"。通过将它们用作 "true""false",它们将被视为字符串。因此你的循环不会迭代。

关于c - 如何格式化代码以便触发 while 循环 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486919/

相关文章:

c - C的数据流和控制流程图

python - 使用 os.system 重新启动程序以节省内存?

MATLAB m 文件帮助格式化

python - 使用许多 Pandas 列创建一个新列的字符串格式

C编程,GSL vector 库,错误: incompatible type for argument 1 of 'gsl_rng_uniform'

c - 局部静态变量如何在方法中工作?

c - 将 .a 库与 CMake 链接

C++ 用户输入关闭程序调试

python - 如何在Python中运行多线程进行多客户端套接字编程?

mysql - 我如何过滤掉最新的运行并将行交叉制表到列中?