在c中使用scanf停止无限循环?

时间:2015-01-16 15:53:45

标签: c++ c while-loop scanf

我创建了一个程序,接受由注释分隔的3个数字,我将用它们进行一些计算。它将接受这些数字的用户输入,并且我使用scanf接受它。

这是我到目前为止所做的:

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{
    float a, b, c;

    bool continue_program = true;

    while (continue_program) {
        printf("Enter your coordinates: ");
        scanf("%f,%f,%f", &a,&b,&c);
        if(isdigit(a) && isdigit(b) && isdigit(c)){
            printf("Success!");
        } else {
            printf("Try again!");
        }
    }
}

示例输出:

Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!

我知道其他人也遇到了同样的问题,并为这些问题找到了答案,但无法让他们的实现为这段代码工作。

3 个答案:

答案 0 :(得分:2)

你错了scanf()将返回匹配的参数数量,而你没有检查它。

此外,isdigit()函数采用整数,如果传递的参数的ascii值对应于数字,则返回非0

要在满足条件时让程序停止,您应该更改循环内continue_program的值,假设您希望在scanf()没有3浮动时停止要读取的点数,它将返回与3不同的值,因此您可以将其设置为if条件

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{
    float a, b, c;

    bool continue_program = true;

    while (continue_program) {
        printf("Enter your coordinates: ");
        if (scanf("%f,%f,%f", &a, &b, &c) == 3){
            printf("Success!");
        } else {
            continue_program = false;
            printf("Try again!");
        }
    }
}

根据OP的评论建议使用此解决方案

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>

int main(void)
{
    float a, b, c;
    char line[100];

    printf("Enter your coordinates: ");
    while (fgets(line, sizeof(line), stdin) != NULL) {
        size_t length;

        length = strlen(line);
        if (line[length - 1] == '\n')
            line[length - 1] = 0;
        if (strcmp(line, "0,0,0") == 0)
            break;
        if (sscanf(line, "%f,%f,%f", &a, &b, &c) == 3)
            printf("\tSuccess!\n");
        else
            printf("\tTry again!\n");
        printf("Enter your coordinates: ");
    }
}

答案 1 :(得分:1)

您没有在代码中更新@Bean public FlatFileItemWriter<Person> myWriter() { System.out.println("FlatFileItemWriter*******************"); FlatFileItemWriter<Person> writer = new FlatFileItemWriter<Person>(); writer.setResource(new FileSystemResource("output.csv")); DelimitedLineAggregator<Person> delLineAgg = new DelimitedLineAggregator<Person>(); delLineAgg.setDelimiter(","); BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>(); fieldExtractor.setNames(new String[] {"firstName", "lastName"}); delLineAgg.setFieldExtractor(fieldExtractor); writer.setLineAggregator(delLineAgg); writer.setHeaderCallback(myFlatFileHeaderCallback); return writer; } @Component public class MyFlatFileHeaderCallback implements FlatFileHeaderCallback { @Override public void writeHeader(Writer writer) throws IOException { System.out.println("Header called"); } } 的值。因此它的值仍然是真的,因此是无限循环。你必须将它更新为假以阻止它。

答案 2 :(得分:-1)

您可以使用

break;

结束while循环。

通过这种方式你可以做一会儿(1)并摆脱continue_program变量。

像这样:

while(1)
{
    ....
    ....
    if (....)
    {
        // Stop now
        break;
    }
    ....
    ....
 }

所以在你的情况下你可以把

 break;
打印“再试一次”后