gpt4 book ai didi

c++ - 我正在尝试从用户那里获取单个输入,但计算机以某种方式要求两个输入?

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:11 24 4
gpt4 key购买 nike

 void coordinateInput(int in){
int * input = (int *)malloc(in*(sizeof(int)));
for (int i = 0; i < in; i++){
printf("Please enter the x coordinate for control point #%d: ", i);
scanf("%d\n",&input[i]);
printf("Please enter the y coordinate for control point #%d: ", i);
scanf("%d\n",&input[i+1]);
}
}

在输出中,您可以在第 0 行之后看到它要求另一个输入: Screenshot of output

我只想得到一个输入,但由于某种原因我最终得到了两次输入。它仅适用于第一种情况。

最佳答案

我修复了它从 scanf 语句中删除 '\n' 并将其添加到 printf 中的问题。

int * input = (int *)malloc(in*(sizeof(int)));
for (int i = 0; i < in; i++){
printf("Please enter the x coordinate for control point #%d: ", i);
scanf("%d",&input[i]);

printf("\nPlease enter the y coordinate for control point #%d: ", i);
scanf("%d",&input[i+1]);
//[In output you can see after line 0 it asks for another input][1]}
printf("%d - %d\n", input[i], input[i+1]);
}

代码应该是这样的。

关于c++ - 我正在尝试从用户那里获取单个输入,但计算机以某种方式要求两个输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com