我想创建一个菜单,我需要在这个菜单中创建一些汽车(汽车,型号,颜色,价格)。我还需要在这个菜单中修改或删除汽车。
这是我的代码:
#include <conio.h>
#include <stdio.h>
struct automobil
{
unsigned nr;
char Marca, Model, Culoare, Tara;
float Pret;
} *VA[40];
int main()
{
unsigned n, men;
do
{
printf("Crearea vectorului ---- 1\n");
printf("Afisarea datelor ---- 2\n");
printf("Adaugarea ---- 3\n");
printf("Modificarea ---- 4\n");
printf("Eliminarea ---- 5\n");
printf("Iesire ---- 6\n");
scanf("%u", &men);
switch(men)
{
case 1: n = Intr(0); break;
case 2: Afis(n); break;
case 3: n = Intr(n); break;
case 4: Modif(n); break;
case 5: n = Elim(n); break;
}
}
while (men != 0);
unsigned Intr(unsigned i)
{
char rasp;
do
{
VA[i].nr = i;
printf("Introduceti Marca, Modelul, Culoarea, Tara si Pretul automobilului %u\n",i);
scanf("%s, %s, %s, %s, %f", &VA[i].Marca, &VA[i].Model, &VA[i].Culoare, &VA[i].Tara, &VA[i].Pret);
gets(VA[i].Marca);
gets(VA[i].Model);
printf("Mai introduceti date? [d / n]\n");
scanf("%c", &rasp);
i++;
}
while (rasp == 'd');
return i;
}
void Afis(unsigned m)
{
unsigned i;
for (int i = 0; i < m; i++)
{
printf("%u\n", VA[i].nr);
puts(VA[i].Marca);
puts(VA[i].Model);
puts(VA[i].Culoare);
puts(VA[i].Tara);
printf("%f\n", VA[i].Pret);
}
getch();
}
void Modif(unsigned m)
{
unsigned i;
Afis(m);
printf("Introduceti numarul automobilului, datele carora le modificati.\n");
scanf("%u", &nr);
if (i >= m)
{
printf("Nr. inexistent!\n");
}
else
{
printf("Introduceti Marca, Modelul, Culoarea si Tara.\n");
gets(VA[i].Marca);
gets(VA[i].Model);
gets(VA[i].Culoare);
gets(VA[i].Tara);
scanf("%f", &VA[i].Pret);
}
}
unsigned Elim(unsigned m)
{
unsigned i;
char rasp;
do
{
Afis(m);
printf("Nr de Automobile.\n");
scanf("%u", &i);
if (i >= m)
{
printf("Numar inexistent.\n");
}
else
{
for (int j = i; j < m-1; j++)
{
VA[j] = VA[j+1];
m--;
}
}
printf("Mai eliminati automobile? [d / n]\n");
scanf("%c", &rasp);
}
while (rasp == 'd');
return m;
}
}
我得到的错误消息如下:
In function 'int main()':
25:23: error: 'Intr' was not declared in this scope "case 1: n = Intr(0); break;"
26:19: error: 'Afis' was not declared in this scope "case 2: Afis(n); break;"
28:20: error: 'Modif' was not declared in this scope "case 4: Modif(n); break;"
29:23: error: 'Elim' was not declared in this scope "case 5: n = Elim(n); break;"
35:3: error: a function-definition is not allowed here "before '{' token {"
115:2: error: expected '}' at end of input }
[Finished in 0.3s with exit code 1]
答案 0 :(得分:0)
要点1.正确缩进代码。您在}
main()
的右大括号while ();
要点2.您需要具有main()
中使用的函数的前向声明,但稍后会定义。否则,更好的是,在main()
之前定义所有其他函数(在使用前定义)。
解决这些问题之后,scanf("%c", &rasp);
会产生下一个痛苦。在最后一次输入后,这将吃剩余的 ENTER 键。最好使用scanf(" %c", &rasp);
类型的陈述。
接下来,虽然unsigned i;
通常默认为unsigned int i;
,但恕我直言,最好使用后一种形式。