【问题标题】:Can't read in first iteration of a for loop in C无法读取 C 中 for 循环的第一次迭代
【发布时间】:2018-03-03 21:01:27
【问题描述】:

我正在尝试创建一个能够计算拉格朗日多项式的程序,但我遇到了可能是一个微不足道的问题。我得到了一些 x 和 y 值,我应该用它们来近似某个其他 x 的函数。变量nodes指的是给我的x和y值对的数量。

我无法读取 x 值的第一次迭代,它会直接跳到读取 y 值。即它只打印 x(0),然后打印 y(0),而不让我为 x(0) 输入任何内容。对于第一个循环之后的任何循环,这都不是问题。任何帮助,将不胜感激。

#include "stdio.h"
#include "math.h"
#define SIZE 40

int main(){

    // Defining variables and arrays
    int i, j, nodes;
    float nodex[SIZE], nodey[SIZE], appx;

    // Find how many nodes there are
    printf("How many nodes are being referenced?\n");
    scanf("%d", &nodes);

    // Find what number we are approximating for x
    printf("For what value of x are we approximating?\n");
    scanf("%d", &appx);

    for(i=0 ; i < nodes ; i++) 
    {
        printf("\nEnter x(%d)", i);
        scanf("%f", &nodex[i]);
        printf("\nEnter y(%d)", i);
        scanf("%f", &nodey[i]);
    }
}

【问题讨论】:

  • 运行此代码时nodes 分配了什么值?
  • 您正在使用%dappx,而不是%f%d 停止在小数点处,&amp;nodex[0]%d 停止的地方恢复 - 在小数点处。当然,appx 中的值也是垃圾。您应该测试来自scanf() 的返回值;对于您显示的每个呼叫,它应该是1。您应该打印读取的值,以便您知道读取的内容与您的预期相符。
  • 谢谢@JonathanLeffler。这清除了它。

标签: c arrays for-loop scanf


【解决方案1】:

如 cmets 中所述:

您将%dappx 一起使用,而不是%f%d 在小数点处停止读取,&amp;nodex[0] 输入在 %d 停止的位置恢复 - 在小数点处。 appx 中的值当然也是垃圾。

你应该测试来自scanf()的返回值;对于您显示的每个呼叫,它应该是 1。您应该打印读取的值,以便知道读取的内容与您的预期相符。

一些固定的代码:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 40 

static void err_exit(const char *msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(EXIT_FAILURE);
}

int main(void)
{
    // Defining variables and arrays
    int nodes;
    float nodex[SIZE], nodey[SIZE], appx;

    // Find how many nodes there are
    printf("How many nodes are being referenced?: ");
    fflush(stdout);
    if (scanf("%d", &nodes) != 1)
        err_exit("failed to read number of nodes");
    if (nodes < 3 || nodes > SIZE)
        err_exit("number of nodes not in range 0..40");

    // Find what number we are approximating for x
    printf("For what value of x are we approximating?: ");
    fflush(stdout);
    if (scanf("%f", &appx) != 1)
        err_exit("failed to read value");

    for (int i = 0; i < nodes; i++)
    {
        printf("Enter x(%d): ", i);
        fflush(stdout);
        if (scanf("%f", &nodex[i]) != 1)
            err_exit("failed to read x-value");
        printf("Enter y(%d): ", i);
        fflush(stdout);
        if (scanf("%f", &nodey[i]) != 1)
            err_exit("failed to read y-value");
    }

    printf("Approximating: %g\n", appx);
    printf("%d nodes:\n", nodes);
    for (int i = 0; i < nodes; i++)
        printf("%2d (%g,%g)\n", i, nodex[i], nodey[i]);

    return 0;
}

我假设您支持 C99。如果没有,您需要在循环之外声明 int i; 并使用 for (i = 0; i &lt; nodes; i++) 作为循环。

编译:

gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
    read-float-83.c -o read-float-83 

示例运行:

$ ./read-float-83
How many nodes are being referenced?: 5
For what value of x are we approximating?: 3.67
Enter x(0): 1.23
Enter y(0): 2.95
Enter x(1): 1.98
Enter y(1): 3.46
Enter x(2): 2.47
Enter y(2): 4.51
Enter x(3): 3.02
Enter y(3): 2.87
Enter x(4): 4.18
Enter y(4): -1.96
Approximating: 3.67
5 nodes:
 0 (1.23,2.95)
 1 (1.98,3.46)
 2 (2.47,4.51)
 3 (3.02,2.87)
 4 (4.18,-1.96)
$

【讨论】:

    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多