C代码直接进入“其他”陈述......

时间:2013-12-17 03:32:43

标签: c

我的C代码存在问题。问题是它没有在do ... while循环中识别有效输入。

int stp;

do {

    printf("\nPlease enter the packet data (maximum of 50 numbers): ");
    while(getchar()!='\n');

    scanf("%s", dataTest);

    if(dataTest == '\n')
        scanf("%s", dataTest);

    if(dataTest==1) {

        length = strlen(dataTest); 

        if(length < 50) {

            for(i=0;i<=length && stp!=1;i++) {

                if ( (dataTest[i] >= '0' && dataTest[i] <= '9') || (dataTest[i] == 0) ) { 

                    valid=1;
                    win_linux();

                } else {

                    printf("\nData must contain only numbers, '%c' is not a number. Please try again. \n", dataTest[i]);
                    stp=1;
                    valid=0; 

                }

            }

        } else { 

            valid = 0; 
            while(getchar()!='\n');
            printf("\nData should have no more than 50 numbers, you have entered %i",length); 

        }

    } else { 

        printf("\nNot valid data, numeric only. Please try again"); 
        while(getchar()!='\n');
        valid=0; 

    }

} while(valid!=1);

请注意,dataTest是一个包含50 char s。

的数组

例如,如果我为扫描输入1(有效输入),则直接转到else语句“无效数据,仅限数字。请再试一次”

有什么想法吗?

提前谢谢你们。

4 个答案:

答案 0 :(得分:1)

if(dataTest==1)永远不会成真,因为你正在做scanf("%s",...)dataTest是一个字符串,而不是数字1.如果您输入1,它将是一个字符串("1"),而不是一个数字(1)。

编辑以解决评论: @Charlieabee:您说,“当我输入任意数字超过一位数时,它会显示错误,说'仅数字数据'。”对,因为任何数字都超过1位数,它不完全等于“1”。您的代码不会测试dataTest是否为数字;它测试完全是否等于“1”。要测试每个字符是否都是数字,您需要遍历整个字符串并验证每个字符是否在“0”和“9”之间。

执行您要执行的操作的更简单方法是scanf("%d", &intvar),其中intvar的类型为int。然后,您根本不必担心字符串验证。

但是你说你正在尝试接收50个字符的数组。在这种情况下,您别无选择,只能执行scanf("%d %d %d %d ...", &intvar1, $intvar2, ... &intvar50);(实际输入所有50 %d$intvarX s)或进行一些字符串解析。但实际上,您的代码注定要失败,因为您将整个输入与字符串"1"进行比较。

答案 1 :(得分:0)

这一行:

if (dataTest==1) {

询问“是地址1处的字符串dataTest吗?”,由于它不是,它将失败。您可能想知道的是字符串是否代表数字1.如果有办法,可以有几个:

if (0 == strcmp("1", dataTest)) {
. . .

if (1 == strtol(dataTest, NULL, 10)) {
. . .

答案 2 :(得分:0)

我修好了。

我改变了

if(dataTest == 1) {

if(dataTest >= 0) {

这使程序正确流动。感谢大家的帮助。

答案 3 :(得分:0)

鉴于各种评论和答案中出现的数据,我相信我可以对问题进行合理彻底的诊断。

由于dataTest是char [50](因而是char*),断言if(dataTest == 1)询问dataTest指向的内存的确切地址是否为1。似乎工作是偶然的。提问者将其更改为if(dataTest >= 0)的建议修正案正在说明问题。在大多数体系结构上,指针总是> = 0,0等于NULL。因此,OP的修复有效地删除了if语句及其相关的else。

因此,解决方案变为

int stp;

do {

    printf("\nPlease enter the packet data (maximum of 50 numbers): ");
    while(getchar()!='\n');

    scanf("%s", dataTest);

    if(dataTest == '\n')
        scanf("%s", dataTest);

    length = strlen(dataTest); 

    if(length < 50) {

        for(i=0;i<=length && stp!=1;i++) {

            if ( (dataTest[i] >= '0' && dataTest[i] <= '9') || (dataTest[i] == 0) ) { 

                valid=1;
                win_linux();

            } else {

                printf("\nData must contain only numbers, '%c' is not a number. Please try again. \n", dataTest[i]);
                stp=1;
                valid=0; 

            }

        }

    } else { 

        valid = 0; 
        while(getchar()!='\n');
        printf("\nData should have no more than 50 numbers, you have entered %i",length); 

    }

} while(valid!=1);

但是,应该注意的是,这个指针比较也会使一些其他行无效,例如if(dataTest == '\n'),它会检查地址dataTest是否恰好等于数值{换行符(几乎肯定是假的)。所以这条线也可能被移除。

还有一些微妙的错误。例如,如果输入50个或更多字符,scanf将溢出dataTest并损坏内存。

提问者的其余代码是有效的C I / O,尽管有点不干净。所述变更应使其按预期工作,但我建议清理它。我提供以下C程序,它应该或多或少地做OP想要的。

#include <stdio.h>
#include <strings.h>

int main( void ) {

    // Needs to be size 51 to allow for 50 chars
    char dataTest[51];

    int valid = 0;
    do {
        while(getchar()!='\n'); // Flush stdin
        printf("Please enter the packet data (max of 50 numbers): ");

        // Scanf returns the number of arguments filled
        // %50s says we want to read UP TO 50 chars before we hit whitespace
        int successful = scanf("%50s", dataTest);
        if(!successful) {
                valid = 0;
                continue; // If we didn't read right, try again
        }

        int length = strlen(dataTest);
        if(length == 0) {
               valid = 0;
               continue; // If we only had whitespace, assume invalid
        }

        int i;
        for(i = 0; i < length; i++) {
            if(dataTest[i] >= '0' && dataTest[i] <= '9' ) {
                valid = 1;
                win_linux(); // this was in the original code by the OP
            } else {
                valid = 0;
                break; // Takes the place of the stp variable
            }
        }
    } while(!valid);

    printf("%s\n", dataTest);
}
相关问题