我正在为我的班级编写一个研讨会(我是一个完整的初学者),我遇到了一些问题。
#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;
}
我一直收到这些错误:
work.c: In function ‘main’:
work.c:23:5: error: break statement not within loop or switch
work.c: In function ‘GetInt’:
work.c:35:26: warning: comparison between pointer and integer [enabled by default]
work.c:38:18: warning: comparison between pointer and integer [enabled by default]
work.c:40:3: warning: ‘return’ with a value, in function returning void [enabled by default]
work.c: In function ‘GetFloat’:
work.c:45:26: warning: comparison between pointer and integer [enabled by default]
work.c:48:25: warning: comparison between pointer and integer [enabled by default]
work.c:50:7: warning: ‘return’ with a value, in function returning void [enabled by default]
有人可以告诉我发生了什么以及如何解决这个问题吗?
答案 0 :(得分:0)
我会尝试指出我在代码中看到的所有错误
#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
}
你应该使用双打而不是浮点数。除了图形处理之外,我不确定浮点数在哪里有用。
答案 1 :(得分:0)
如果你在函数中有返回值,它们不能为void,它们必须是int(在你的情况下):
int GetInt();
int GetFloat();
你的scanf()函数是incorret,因为你无法比较它的值,它需要是:
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]);
}
}
有一些基本的安排。