C 学校编程的问题
我正在为我的班级编写一个研讨会(我是一个完全的初学者),我遇到了一些问题C 学校编程的问题,c,C,我正在为我的班级编写一个研讨会(我是一个完全的初学者),我遇到了一些问题 #include <stdio.h> #define arraylength 100 void GetInt(); void GetFloar(); int main (void) { int i, k, j; float price[arraylength]; int barcode[arraylength]; int quantity[arraylength];
#include <stdio.h>
#define arraylength 100
void GetInt();
void GetFloar();
int main (void)
{
int i, k, j;
float price[arraylength];
int barcode[arraylength];
int quantity[arraylength];
int lenght;
printf("Grocery Store Inventory\n");
printf("=======================\n");
for (i = 0; i < arraylength; i++) {
printf("Barcode: ");
scanf("%d", &barcode[i]);
GetInt;
}
if (barcode[i] == 0)
break;
printf("Price: ");
scanf("%f", &price[i]);
printf("Quantity: ");
scanf("%d", &quantity[i]);
return 0;
}
void GetInt()
{
int number;
printf("Barcode: ");
while (scanf("%d", &number != 1)){
printf("Wrong Input , Try Again!\n");
printf("Insert Correct Barcode:\n");
while(getchar() != "\n");
}
return number;
}
void GetFloat()
{
float number;
printf("Barcode: ");
while (scanf("%d", &number != 1)){
printf("Wrong Input , Try Again!\n");
printf("Insert Correct Barcode:\n");
while (getchar() != "\n");
}
return number;
}
有人能告诉我发生了什么,以及如何解决这个问题吗?我将尝试指出我在代码中看到的所有错误
#include <stdio.h>
#define arraylength 100
//this might as well be: const int arraylength = 100;
void GetInt();
void GetFloar(); //this is spelled wrong
int main (void)
{
int i, k, j;
float price[arraylength];
int barcode[arraylength];
int quantity[arraylength];
int lenght; //this is spelled wrong
printf("Grocery Store Inventory\n");
printf("=======================\n");
for (i = 0; i < arraylength; i++) {
printf("Barcode: ");
scanf("%d", &barcode[i]);
GetInt; //this does nothing; to call GetInt, use GetInt()
}
if (barcode[i] == 0) // i == arraylength here so this is illegal
break; //you can only break from a loop or a switch statement
printf("Price: ");
scanf("%f", &price[i]); // i == arraylength here so this is illegal
printf("Quantity: ");
scanf("%d", &quantity[i]); // i == arraylength here so this is illegal
return 0;
}
void GetInt() //this should be declared as int GetInt(void)
{
int number;
printf("Barcode: ");
while (scanf("%d", &number != 1)){
//&number != -1 is not a valid parameter for scanf
//Seems like it should be scanf("%d", &number) != 1
printf("Wrong Input , Try Again!\n");
printf("Insert Correct Barcode:\n");
while(getchar() != "\n");
}
return number; //you can't return from a void function
}
void GetFloat() //this should be declared float GetFloat(void)
{
float number;
printf("Barcode: ");
while (scanf("%d", &number != 1)){
//&number != -1 is not a valid parameter for scanf
//also you have the integer format string there
//Seems like it should be scanf("%f", &number) != 1
printf("Wrong Input , Try Again!\n");
printf("Insert Correct Barcode:\n");
while (getchar() != "\n");
}
return number; //you can't return from a void function
}
#包括
#定义阵列长度100
//这也可以是:const int arraylelength=100;
void GetInt();
void GetFloar()//这个拼错了
内部主(空)
{
int i,k,j;
浮动价格[排列长度];
整数条码[排列长度];
整数数量[排列长度];
int lengh;//这是拼写错误的
printf(“杂货店库存”);
printf(“========================================\n”);
对于(i=0;i
你应该使用双精度而不是浮点数。除了图形处理之外,我不确定浮动在哪里有用。如果函数中有返回值,它们不能为空,必须为int(在您的情况下): scanf()函数不正确,因为您无法比较其中的值,它需要:
while(scanf("%d", &number) != 1)
关于GetInt()函数和:
while(scanf("%f", &number) != 1)
在GetFloat()上,因为您正在使用浮点。
在main()函数中,您没有中断任何内容,这是一个错误。您的代码需要:
for (i = 0; i < arraylength; i++) {
printf("Barcode: ");
scanf("%d", &barcode[i]);
GetInt();
if (barcode[i] == 0){
break;
}else{
printf("Price: ");
scanf("%f", &price[i]);
printf("Quantity: ");
scanf("%d", &quantity[i]);
}
}
(i=0;i{
printf(“条形码:”);
scanf(“%d”和条形码[i]);
GetInt();
如果(条形码[i]==0){
打破
}否则{
printf(“价格:”);
scanf(“%f”和价格[i]);
printf(“数量:”);
scanf(“%d”和数量[i]);
}
}
有一些基本的安排。编译时带有警告:
void GetFloar()代码>-->void GetFloat()代码>,GetInt代码>-->GetInt()代码>提示:错误消息都有文件名、行号和列号,例如work.c:23:5
是“work.c”中的第23行第5列。顺便说一句,花一点时间正确格式化代码,可以更容易地看到错误。比如,你不能从main中断。在你退出main中的for循环后,有些事情看起来很不对劲。在那之后,我应该等于arraylength,但是你用i访问所有数组,它访问数组的末尾。我想他是说接下来的4行也在for循环中。谢谢你的建议,我没有注意到这一点。
while(scanf("%f", &number) != 1)
for (i = 0; i < arraylength; i++) {
printf("Barcode: ");
scanf("%d", &barcode[i]);
GetInt();
if (barcode[i] == 0){
break;
}else{
printf("Price: ");
scanf("%f", &price[i]);
printf("Quantity: ");
scanf("%d", &quantity[i]);
}
}