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

How to format code so while loop is triggered C

所以我有两个 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",它们将被视为字符串。因此你的循环没有迭代。

int stilltrue = "false"; //create integer to use in while
while (stilltrue == "false"){//initiate while loop

这些行绝对不会做你想做的,你的编译器应该在声明中抱怨 stilltrue 的初始化。 "false" 是一个 字符串文字 ,而不是您之前定义的 false 符号。

将它们更改为

int stilltrue = false; //create integer to use in while
while (stilltrue == false){//initiate while loop

或更好,

int stilltrue = false;
while ( !stilltrue ) {

注意truefalse已经在stdbool.h中定义了;无需自己定义它们。

如果scanf在读取整数时失败,输入缓冲区需要清除导致失败的字符。 scanset "%*[^\n]" 扫描并丢弃不是换行符的字符。输入缓冲区中留有换行符,但 %d 格式说明符将跳过前导空格。

#include <stdio.h>

#define true 1
#define false 0

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 %d", &user_number, &last_multiple)) == 2){
            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
            scanf ( "%*[^\n]");//clear input buffer of all characters up to newline
        }
    }
    return 0;
}