gpt4 book ai didi

c - c编程结构错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:53 25 4
gpt4 key购买 nike

我的语法有一些问题,主要是说在“struct CustomerInfo s”行的“struct”之前有一个错误;但是我似乎没有发现问题。
我的程序应该首先询问这个人想做什么,他们可以存储第一个选项的记录,然后存储在一个结构中,可以通过选择菜单的第二个选项来查看,如果他们选择第三个选项,那么他们显然退出了程序。
任何帮助都将不胜感激谢谢。

#include <stdio.h>

void menu();
int id,first,last;
struct CustomerInfo
{
char FirstName[15]; /* These are the varibles for the customer infomation */
char LastName[20];
int ID;
};

int main()
{ /* program starts */

int selection = 0;

void menu()
{ /* Menu loop function */

do
{ /* menu start */
printf("\n\n - What would you like to do?");
printf("\n1 - Store a customer record");
printf("\n2 - View customer Records");
printf("\n3 - Quit program");
scanf("%i", &selection);

} while (selection > 3);

printf("You have entered an incorrect value"); /* If selection is greater than 3 then end program */
return 0;
}

switch(selection)
{
/* switch statement starts */
case 1:
struct CustomerInfo s;
printf("Please enter the customers details including First name, Lastname and ID.\n\n");
printf("Enter First name: ");
scanf("%s", s.FirstName); /* Option 1: Asks to enter the customers details to store then loops back to program */
printf("Enter Last name: ");
scanf("%s", s.LastName);
printf("Enter Customer ID: ");
scanf("%s", s.ID);
void menu();
break;

case 2:

printf("\nDisplaying Infomation\n");
printf("First name: %s\n",s.Firstname); /* Option 2: Prints the customer details as listed in option 1 */
printf("Last name: %s\n",s.Lastname);
printf("Customer ID: %s\n",s.ID);
void menu();
break;

case 3: /* Option 3: Program ends if option 3 is chosen. */
break;
}

返回0;

最佳答案

让我们先看看您创建的结构;接下来我们将尝试查看它是否可以修复。
我不说细节,所以我们可以看到大的轮廓:

main {
struct{}
void menu(){
do {
stuff
} while (selection > 3)
printf("you have entered an incorrect value"); // if selection is > 3
}
switch(selection) {
// do something if selection is 1 or 2, exit if 3
}

代码中没有最后的右大括号。我假设这是复制粘贴错误,所以我添加了它。使用 -Wall编译(以获得警告和报告的错误),我会得到许多错误:
sel.c:18: error: nested functions are disabled, use -fnested-functions to re-enable
sel.c: In function ‘menu’:
sel.c:31: warning: ‘return’ with a value, in function returning void
sel.c: In function ‘main’:
sel.c:38: error: expected expression before ‘struct’
sel.c:41: error: ‘s’ undeclared (first use in this function)
sel.c:41: error: (Each undeclared identifier is reported only once
sel.c:41: error: for each function it appears in.)
sel.c:61: warning: control reaches end of non-void function

让我们依次进行:
sel.c:18: error: nested functions are disabled, use -fnested-functions to re-enable

将一个函数放在另一个函数中就是“嵌套”。很少有人愿意这样做——这意味着,当你在另一个函数(有点像局部变量,但对于函数)中时,函数才是“可见的”。它不是标准的C-它是 gcc的扩展。使用非标准(因此不可移植)扩展几乎总是一个坏主意。见 http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
sel.c: In function ‘menu’:
sel.c:31: warning: ‘return’ with a value, in function returning void

当我们声明一个函数时,我们说它不会返回一个值。当您有一个类似 void的语句时,您将返回一个值。编译器将忽略这一点-但它警告您说了一件事,做了另一件事。只需使用不带参数的 return 0;,警告就会消失。
sel.c:38: error: expected expression before ‘struct’
sel.c:41: error: ‘s’ undeclared (first use in this function)
sel.c:41: error: (Each undeclared identifier is reported only once
sel.c:41: error: for each function it appears in.)

这是最棘手的一个。您可能希望在第38行中正确声明了一个变量 return;,但是编译器会抱怨。原因在
Why can't variables be declared in a switch statement?
作为旁白-如果你能像这样声明一个变量-你用它做什么?您的代码当前读入值并返回。但一旦离开变量的“作用域”(在您的情况下,因为您在 s中声明了 s,这就是它的作用域),变量就会消失(用于它的内存被标记为“空闲”并将被重新使用)
sel.c:61: warning: control reaches end of non-void function

这表示您已经到达期望返回值的函数的末尾,但您没有 switch类型的语句。再一次-这只会导致警告,因为默认行为是在没有给出值的情况下返回 return someValue;,但这表明你说了一件事,做了另一件事。
到目前为止,我已经解释了编译器给出的错误。让我们更仔细地看一下代码结构。我想你想做的是这样的:
define customerInfo structure
define menu function
main()
repeat:
call menu, get selection
switch(selection):
case 1: create new record
case 2: display records
case 3: quit program

为了使这个工作,我们需要对您的程序做一些更改。首先,让我们将 0函数定义移到 menu函数之外,这样我们就有了可移植的代码。第二,如果我们想创建多个客户记录,我们需要将它们存储在一个数组中。实际上,你需要一个列表,这样你就可以无限期地扩展,但是让我们保持它简单,并允许最多10个记录。然后我们需要改进菜单功能的逻辑(如果选择不是1、2或3,则给出一条消息并重试;在当前代码中
printf("You have entered an incorrect value");  

直到退出了测试不正确的值的循环时才被执行。因此,当您最终到达那里时,该值是有效的,而不是无效的。
在我们真正开始编写“正确”的代码之前,还有一件事值得注意。当您使用 main读取值时,可以执行以下操作:
           scanf("%s", s.FirstName);    

这是正确的,因为 scanf是指向字符串开头的指针。但是,您为字符串分配了有限的空间(即15个字符,包括终止的 s.FirstName),因此,如果有人输入长名称,您的程序将崩溃。”好的防御性编码“要求你抓住这一点-例如使用
       scanf("%14s", s.FirstName); 

上面写着“读不超过14个字符”。有更好的技巧,但至少这是一个开始。然而,当你这样做的时候,你实际上犯了一个错误
       scanf("%s", s.ID);

因为 '\0'被定义为一个 ID,现在您正在将字符串读入…不仅仅是它的地址,而是读入由 int值指向的某个位置。这很可能会导致分段错误(访问“不是您的”内存)。你应该做:
       scanf("%d", &s.ID);

“将整数读入 s.ID
另外-在某些地方使用 s.ID,而在其他地方使用 FirstName。同上。大小写很重要-当您修复其他编译器错误时,这些错误将开始出现。
因为您似乎希望能够读入多个客户记录,所以我们需要一个记录数组;正如我上面所说的,我们必须确保该数组在switch语句的作用域内可用,并“保留”该语句(这样您就可以使用它)。把所有这些东西放在一起,我们就会得到这样的结果:
#include <stdio.h>

// define function prototype:
int menu();

struct CustomerInfo
{
char FirstName[15]; /* These are the variables for the customer infomation */
char LastName[20];
int ID;
};

int menu()
{ /* Menu loop function */
int flag = 0;
int selection;
do
{ /* menu start */
if(flag > 0) printf("You have entered an incorrect value"); /* If selection is greater than 3 then end program */
printf("\n\n - What would you like to do?");
printf("\n1 - Store a customer record");
printf("\n2 - View customer Records");
printf("\n3 - Quit program\n>> ");
scanf("%i", &selection);
flag++;
} while (flag < 10 && (selection < 0 ||selection > 3));

return selection;
}

int main(void)
{ /* program starts */

struct CustomerInfo s[10];
int selection;
int customerCount = 0;

while(1) {
int ii; // loop counter we will need later
selection = menu();
switch(selection)
{
case 1:
printf("Please enter the customers details including First name, Lastname and ID.\n\n");
printf("Enter First name: ");
scanf("%s", s[customerCount].FirstName); /* Option 1: Asks to enter the customers details to store then loops back to program */
printf("Enter Last name: ");
scanf("%s", s[customerCount].LastName);
printf("Enter Customer ID: ");
scanf("%d", &s[customerCount].ID);
customerCount++;
break;

case 2:
printf("\nDisplaying Infomation\n");
for(ii = 0; ii < customerCount; ii++) {
printf("First name: %s\n",s[ii].FirstName); /* Option 2: Prints the customer details as listed in option 1 */
printf("Last name: %s\n",s[ii].LastName);
printf("Customer ID: %d\n---\n",s[ii].ID);
}
break;

case 3: /* Option 3: Program ends if option 3 is chosen. */
return 0; // program returns
break;
}
}
}

测试输出:
 - What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 1
Please enter the customers details including First name, Lastname and ID.

Enter First name: John
Enter Last name: Smith
Enter Customer ID: 123


- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 5
You have entered an incorrect value

- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> -1
You have entered an incorrect value

- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 1
Please enter the customers details including First name, Lastname and ID.

Enter First name: Harry
Enter Last name: Jones
Enter Customer ID: 654


- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 2

Displaying Infomation
First name: John
Last name: Smith
Customer ID: 123
---
First name: Harry
Last name: Jones
Customer ID: 654
---


- What would you like to do?
1 - Store a customer record
2 - View customer Records
3 - Quit program
>> 3

关于c - c编程结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19719709/

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