为什么变量存储的值与用户输入的值不同?
Why does variable store different value than what user inputs?
我在设置用户输入的变量时遇到问题。我输入了一些东西,它存储了另一个值。非常令人沮丧。所以任何指导都会很棒。
请注意,有很多 printf()
,因为我试图找出问题所在。另外我还在努力争取C.
#include <stdio.h>
int vehicletype, in, out; // User Entered Info
int vehicleID[5000];
float vehicleCharge[5000];
int i;
// Variables for Main Function
int q; // Loop Control
float z;
int main (){
for(q = 1; q != 1518944; q++) {
printf("Enter your position in the parking queue: ");
// Take the queue entered by the user and assign it to i
scanf("%d\n", &i);
// Take the user input(which is being held in the variable i) and place it into the 'i'
//position of the ID array
vehicleID[q] = i;
printf("Enter the time(past 0600) you wish to start parking: \n");
//take the time and pass it to the time function to determine roundup
scanf("%d\n", &in);
printf("Enter the time(before 2200) you wish to leave: \n");
scanf("%d\n", &out);
printf("Time in: %d\nTime out: %d\n", in, out);
}
return 0;
}
@M.M 我应该能够在 "in" 变量中输入 0617,在 "out" 中输入 1547(稍后我用它来找出他们停了多少车)但是通过打印 "in" 和 "out" 检查变量时得到的输出分别是 1 和 399。
这里有一些或多或少的工作代码。我不明白 1518944 的限制,但代码会采取措施确保无论您输入多少条目,代码都不会超出数组 vehicleID
的范围。它还会检查一些数字的有效性,并回应其输入。当通过另一个程序写入数据时,某些输出上的前导换行符使输出显得半正常(随机数生成器是我用来生成数字 1-5000 和两次 0600-2200 的东西)。
#include <stdio.h>
static int vehicleID[5000];
int main(void)
{
for (int q = 1; q != 1518944; q++)
{
int in, out;
int i;
printf("\nEnter your position in the parking queue: ");
if (scanf("%d", &i) != 1)
break;
vehicleID[q % 5000] = i;
printf("Enter the time (past 0600) you wish to start parking: ");
if (scanf("%d", &in) != 1)
break;
printf("Enter the time (before 2200) you wish to leave: ");
if (scanf("%d", &out) != 1)
break;
if (in < 600 || in > 2200 || out < 600 || out > 2200 || in >= out)
printf("Invalid times (in %.4d, out %.4d)\n", in, out);
else
printf("\nPosition: %d; Time in: %.4d; Time out: %.4d\n", i, in, out);
}
putchar('\n');
return 0;
}
请注意,输入已被检查并回显。检查是一项重要的编程技术。打印(回显)是一种基本的调试技术。
我在设置用户输入的变量时遇到问题。我输入了一些东西,它存储了另一个值。非常令人沮丧。所以任何指导都会很棒。
请注意,有很多 printf()
,因为我试图找出问题所在。另外我还在努力争取C.
#include <stdio.h>
int vehicletype, in, out; // User Entered Info
int vehicleID[5000];
float vehicleCharge[5000];
int i;
// Variables for Main Function
int q; // Loop Control
float z;
int main (){
for(q = 1; q != 1518944; q++) {
printf("Enter your position in the parking queue: ");
// Take the queue entered by the user and assign it to i
scanf("%d\n", &i);
// Take the user input(which is being held in the variable i) and place it into the 'i'
//position of the ID array
vehicleID[q] = i;
printf("Enter the time(past 0600) you wish to start parking: \n");
//take the time and pass it to the time function to determine roundup
scanf("%d\n", &in);
printf("Enter the time(before 2200) you wish to leave: \n");
scanf("%d\n", &out);
printf("Time in: %d\nTime out: %d\n", in, out);
}
return 0;
}
@M.M 我应该能够在 "in" 变量中输入 0617,在 "out" 中输入 1547(稍后我用它来找出他们停了多少车)但是通过打印 "in" 和 "out" 检查变量时得到的输出分别是 1 和 399。
这里有一些或多或少的工作代码。我不明白 1518944 的限制,但代码会采取措施确保无论您输入多少条目,代码都不会超出数组 vehicleID
的范围。它还会检查一些数字的有效性,并回应其输入。当通过另一个程序写入数据时,某些输出上的前导换行符使输出显得半正常(随机数生成器是我用来生成数字 1-5000 和两次 0600-2200 的东西)。
#include <stdio.h>
static int vehicleID[5000];
int main(void)
{
for (int q = 1; q != 1518944; q++)
{
int in, out;
int i;
printf("\nEnter your position in the parking queue: ");
if (scanf("%d", &i) != 1)
break;
vehicleID[q % 5000] = i;
printf("Enter the time (past 0600) you wish to start parking: ");
if (scanf("%d", &in) != 1)
break;
printf("Enter the time (before 2200) you wish to leave: ");
if (scanf("%d", &out) != 1)
break;
if (in < 600 || in > 2200 || out < 600 || out > 2200 || in >= out)
printf("Invalid times (in %.4d, out %.4d)\n", in, out);
else
printf("\nPosition: %d; Time in: %.4d; Time out: %.4d\n", i, in, out);
}
putchar('\n');
return 0;
}
请注意,输入已被检查并回显。检查是一项重要的编程技术。打印(回显)是一种基本的调试技术。