我尝试编写的代码应该获取用户给定的数据并将其放入数据结构中,该数据结构工作得很好。问题是当我尝试删除一个条目( if(choice == 2) 时。它所要做的就是将该条目设为空,但我不喜欢空白的想法。for 循环我试图做的是获取顶部条目并将所有内容向下移动,但它所做的是获取第一个条目并将其复制到第二个条目,而不管其他条目。有关如何获取数据并将条目删除到的任何帮助将它们移下来一个就很好了。
#include <stdio.h>
#include <string.h>
typedef struct book { //This is the data structure
char personName[15];
char personLname[15];
char phoneNumber[15];
char null[4];
} Book;
static int loopValue;// this is going to loop the program forever until the
//loop is broke
main()
{
int index = 0;
int choice;
Book *ptrBook = (Book*)malloc(sizeof(Book));
int i,j;
int stopValue = 1;
while(stopValue=0)
{
printf("\t\tPhone Book Application\n");
printf("\t1. Add Contact\t\t2. Remove Contact\n");
printf("\t3. Show Contacts\t10. Exit\n");
printf("\tChoice: ");
scanf("%i",&choice);
printf("\n\n");
if(choice == 1) // this is the add person, it takes the print if
/and puts it into the data structure
{
ptrBook = (Book*)realloc(ptrBook, sizeof(Book)*(index + 1));
printf("What is the FIRST name: ");
scanf("%s",ptrBook[index].personName);
printf("What is the LAST name: ");
scanf("%s",ptrBook[index].personLname);
printf("What is the number: ");
scanf("%s",ptrBook[index].phoneNumber);
printf("\nAdded to the Phone Book.\n");
printf("\nName: %s %s\n",ptrBook[index].personName,
ptrBook[index].personLname);
printf("Phone Number: %s",ptrBook[index].phoneNumber);
index++;
}
else if (choice == 2) // this removes people from the data
//structure
{
loopValue == 0;
printf("Who would you like to remove?\n\n");
for(i=0;i<index;i++) // this loops prints out the names to
//choose from
{
printf("%i. %s %s\n",i+1,ptrBook[i].personName,ptrBook[i].personLname);
}
printf("Who would you like to remove? ");
scanf("%i",choice);
for(i=choice;i<0;i--) //THIS IS WHAT NEED HELP WITH PLZ
{ //
strcpy(ptrBook[i-2].personName,ptrBook[i-1].personName); //
strcpy(ptrBook[i-2].personLname,ptrBook[i-1].personLname); //
strcpy(ptrBook[i-2].phoneNumber,ptrBook[i-1].phoneNumber); //
} `//
printf("\n");
scanf("%i",&choice);
}
if(choice == 3) // this loops to print out all the values in the
//data structure
{
for(i=0;i<index;i++)
{
printf("%s %s\n",ptrBook[i].personName,ptrBook[i].personLname);
printf("%i. %s\n\n\n",index,ptrBook[i].phoneNumber);
}
}
else if(choice == 4)
{
//make code to sort names
}
else if(choice == 5)
{
//find a phone number for a given name
}
else if(choice == 6)
{
//random person for you to call
}
else if(choice== 7)
{
//delete everyone
}
else if(choice == 8) // this exits the program by changing the
//loop variable to something that makes the loop false
{
printf("Exiting");
stopValue = 0;
}
}
}
最佳答案
您的代码有很多问题。将来,请确保在发布问题之前彻底清理您的代码。我花了时间清理了很多东西,因为我讨厌自己,所以我希望你能欣赏。首先,错误列表:
1 和 2) while 循环中的条件实际上是一个赋值:请在编译器上打开警告,因为它们会捕获这样的内容。您的 stopValue
初始化也是错误的。
3) loopValue
在整个代码中使用不一致,并且不是控制 while 循环的东西。
4) 在 if (choice == 2)
中,loopValue == 0
表示将其与 0 进行比较,而不是将其设置为 0。这对您没有影响程序,但现在你已经犯了两次 =、== 错误。这确实是您必须掌握的基本内容。
5) 在同一个 if 部分中,设置 choice
的 scanf 在 choice 前面没有 &,这意味着变量实际上不会设置为输入的值。
6)同一部分,最后一个 printf 和 scanf 可能不应该在那里?无论如何,它们都会使输入停止,这会使程序对任何没有源代码的人来说毫无用处。
7) if (choice == 3)
不是 else if。
8) 退出选项在源代码中是8,但菜单上显示是10。
毕竟,这是代码(其中有很多与您所解决的问题无关的内容)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book { //This is the data structure
char personName[15];
char personLname[15];
char phoneNumber[15];
char null[4];
} Book;
static int loopValue;
int main(void) {
int index = 0;
int choice;
Book *ptrBook = (Book*) malloc(sizeof(Book));
int i, j;
int stopValue = 0; // was 1
while (stopValue == 0) { // was =
printf("\t\tPhone Book Application\n");
printf("\t1. Add Contact\t\t2. Remove Contact\n");
printf("\t3. Show Contacts\t8. Exit\n");
printf("\tChoice: ");
scanf("%i", &choice);
printf("\n\n");
if (choice == 1) {
ptrBook = (Book*) realloc(ptrBook, sizeof(Book)*(index + 1));
printf("What is the FIRST name: ");
scanf("%s",ptrBook[index].personName);
printf("What is the LAST name: ");
scanf("%s",ptrBook[index].personLname);
printf("What is the number: ");
scanf("%s",ptrBook[index].phoneNumber);
printf("\nAdded to the Phone Book.\n");
printf("\nName: %s %s\n",ptrBook[index].personName,
ptrBook[index].personLname);
printf("Phone Number: %s",ptrBook[index].phoneNumber);
index++;
} else if (choice == 2) {
loopValue = 0; // was ==
printf("Who would you like to remove?\n\n");
for (i = 0; i < index; i++) {
printf("%i. %s %s\n", i+1, ptrBook[i].personName, ptrBook[i].personLname);
}
printf("Who would you like to remove? ");
scanf("%d", &choice); // didn't have &
for (int i = (choice - 1); i < (index - 1); i++) {
ptrBook[i] = ptrBook[i + 1];
}
index--;
// used to be redundant/confusing/wrong printf and scanf
} else if (choice == 3) { // used to not be else if
for (i = 0; i<index; i++) {
printf("%s %s\n", ptrBook[i].personName, ptrBook[i].personLname);
printf("%i. %s\n\n\n", index, ptrBook[i].phoneNumber);
}
} else if (choice == 8) { // Should've been 10
printf("Exiting");
stopValue = 1;
}
}
}
关于c - 创建一个 for 循环来编辑数据结构中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55055037/