2014-12-27 98 views
0

我对C非常陌生,我想弄清楚为什么我的代码返回的值不正确。为什么我的代码返回一个不正确的值?

int main() 
{ 


    printf("Welcome to my number generator! \n"); 
    printf("What is the first number in the range? \n"); 
    int rng1 = scanf("%d", &rng1); 
    printf("What is the second number in the range? \n"); 
    int rng2 = scanf("%d", &rng2); 
    printf("What would increment would you like to go up in? \n"); 
    int inc = scanf("%d", &inc); 

    do 
     { 
     printf("%d\n"rng1); 
     rng1 += inc; 
     } 
    while(rng1 != rng2); 
    } 
    return 0; 
    } 

从这个代码,我希望第一范围和第二范围在一定数量的上升之间的号码清单,而是我得到1.我做错了的值? P.S.我试图'调试'它,发现当我用:

if(isalpha(rng1)); 
    printf("I am a String...") 
if(isdigit(rng1)) 
    printf("I am a Digit") 

它返回,“我是一个字符串...”。

谢谢!

+0

这些测试*无用*(对不起,虽然不错)尝试查找scanf的描述并阅读它的返回值。 – usr2564301

+5

仅供参考:'if(isalpha(rng1));'在if语句之后不应该有分号 - 'printf'将始终运行,因为如果条件与分号一致则“结束”。 – ipavl

+3

scanf返回成功初始化变量的数量,因此你的每一个返回1(如果你认为它是一个有效的输入数字) 你应该这样做:'int rng1; scanf(“%d”,&rng1);' –

回答

2

你是通过调用scanf并且scanf已经将输入值分配给变量rng1,rng2inc,并且scanf返回成功填充的参数列表的项目数量。因此,将scanf的返回值分配给这些变量是不正确的。只需使用输入数量的返回值即可。您应该读取值1,因为您只想读取每个scanf的一个值。此外,您还可以检查输入值以检测输入值是否有效。

除此之外,我想对您的代码进行一些修改。尤其是您的do{...}while();循环可能由于运算符!=而无限期地运行。请参阅下面的代码中的评论。

int main() 
{ 
    /* Declare the variables and do not assign the return value of scanf */ 
    int rng1, rng2, inc; 
    printf("Welcome to my number generator! \n"); 
    printf("What is the first number in the range? \n"); 
    /* repeat this check condition for each scanf, exit(EXIT_FAILURE) requires #include <stdlib.h>*/ 

    if (1 != scanf("%d", &rng1)) { 
     exit(EXIT_FAILURE); 
    } 
    printf("What is the second number in the range? \n"); 
    scanf("%d", &rng2); 
    printf("What would increment would you like to go up in? \n"); 
    scanf("%d", &inc); 

    do 
    { 
     printf("%d\n",rng1); 
     rng1 += inc; 
    } 
    /* Use <= instead of != and think about the case for rng1 is 1 rng2 is 5 and inc is 3, can you detect the end of the loop by adding 3 to the starting point 1 ? */ 
    while(rng1 <= rng2); 

    /* Remove the `}` here */ 
    return 0; 
} 
+0

谢谢你现在好像工作得很好。所以问题是我覆盖了变量的值,而不是读取scanf的返回值并将其设置为变量的值? – Dinar

+0

对于奇怪的价值问题,是的。但是,您还应该检查while(rng1 <= rng2);在您的原始代码中。 – Deniz

1

当您传递变量的地址(使用&运算符)时,允许scanf将扫描值写入变量。 scanf的返回值是而不是您正在查找的值(这是别的)。

因此,当您将scanf的返回值分配给您的变量时,您将覆盖它已有的正确值。这应该工作:

int rng1; 
scanf("%d", &rng1); 
+1

但是您应该检查返回值以确保您读取了正确的i电信设备制造商。 – clcto

+0

因此,通过使用'='符号,我用不同的结果而不是用户输入覆盖了'int rng1'的值? – Dinar

+0

@Dinar是的。其他人已经描述了'scanf'的实际返回值。您可能会使用该结果,或者您现在可能会选择忽略它,但无论如何您都不应该覆盖'scanf'已将用户输入的变量。 –

1

scanf()返回的值应始终进行检查,以确保手术成功。 格式字符串应该(几乎)总是包含一个前导空格“” 这样一个遗留的白色空间(如新行)被跳过/消耗 因此,代码应该更像:

int rng1; 
if(1 != scanf(" %d", &rng1)) 
{ // then, scanf failed 
    perror("scanf for rng1 failed"); 
    exit(EXIT_FAILURE); 
} 

// implied else, scanf for rng1 successful 
0

AS上面两个注释解释了scnaf()有返回值只是为了检查成功的操作。然而,你需要阅读的价值是作为第二个参数。 我也可以看到代码中的括号不匹配。

int main(int argc, char **argv) 
{ 
    printf("Welcome to my number generator! \n"); 
    printf("What is the first number in the range? \n"); 
    int rng1; 
    int t = scanf("%d", &rng1);     // First scanned value stored in rng1 variable 
    printf("What is the second number in the range? \n"); 
    int rng2; 
    int t1 = scanf("%d", &rng2);     // Second scanned value stored in rng2 variable 
    printf("What would increment would you like to go up in? \n"); 
    int inc; 
    int t2 = scanf("%d", &inc);     // Third scanned value stored in inc variable 

    do 
    { 
     printf("%d\n",rng1); 
     rng1 += inc; 
    } 
    while(rng1 <= rng2);    // repeat until rng1 reaches value of rng2 

    return 0; 
} 

这里使用临时变量t,t1和t2,您可以验证值的扫描是否成功完成。

相关问题