gpt4 book ai didi

c - 打印结构数组

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

addInfo() 将信息添加到结构数组中,函数 printList() 输出该数组的所有元素。但是我的 printList() 只输出添加的最后一个元素。有什么问题?它是否错误地将元素添加到数组中?

#include <stdio.h>
#include <stdlib.h>

//structure Location
typedef struct Location{
char locName[35];
char locDesc[85];
float latitude;
float longitude;
} LocationArray;

void addInfo(LocationArray **myArray, int*, int*);
void printList(LocationArray **myArray, int*);
void quitProgram();
void resizeArray(LocationArray **myArray, int*);

//Menu that receives the user input
//and performs corresponding operations based
//on the menu choice
void printMenu(LocationArray **myArray, int *count, int *max){
printf("Hello! Please choose from the menu: \n");
printf("Type 'A' or 'a' for adding additional location to the array\n");
printf("Type 'P' or 'p' for printing the current list of locations\n");
printf("Type 'Q' or 'q' to quit the program\n");

char input = 0;
scanf(" %c", &input);

//Handles the invalid character input
//exits if the character does not correspond
//to the valid menu option
while(input != 'A' && input != 'a'
&& input != 'P' && input != 'p'
&& input != 'Q' && input != 'q'){
printf("Invalid character! Try entering a char again: \n");
scanf(" %c", &input);
}
//Calls other functions
//based on the character input
switch(input){
case 'A':
case 'a':
addInfo(myArray, count, max); //Calls function that adds more locations into the array
break;
case 'P':
case 'p':
printList(myArray, count); //Calls the function that prints the current list of locations
break;
case 'Q':
case 'q':
quitProgram(); //Calls the function that terminates the program
break;
}
}

//Adds info into the array of structures
void addInfo(LocationArray **myArray, int *count, int *numberOfLoc){

if(*count == *numberOfLoc){ // Checks if the array is already full
resizeArray(myArray, numberOfLoc); //Resizes the array if it's full

printf("Please enter the name for the location:\n");
scanf(" %[^\n]", &(*myArray)->locName); //Gets the user input for the Location Name
printf("\nNow enter the description:\n");
scanf(" %[^\n]", &(*myArray)->locDesc); //Gets the user input for the Location Description
printf("\nNow enter the value for the latitude:\n");
scanf("%f", &(*myArray)->latitude); //Gets the user input for the Latitude
printf("\nNow enter the value for the longitude:\n");
scanf("%f", &(*myArray)->longitude); //Gets the user input for the Latitude

(*count)++; //Increment the count
}
else{ //Else, a used fills it out
printf("Please enter the name for the location:\n");
scanf(" %[^\n]", &(*myArray)->locName); //Gets the user input for the Location Name
printf("\nNow enter the description:\n");
scanf(" %[^\n]", &(*myArray)->locDesc); //Gets the user input for the Location Description
printf("\nNow enter the value for the latitude:\n");
scanf("%f", &(*myArray)->latitude); //Gets the user input for the Latitude
printf("\nNow enter the value for the longitude:\n");
scanf("%f", &(*myArray)->longitude); //Gets the user input for the Latitude

(*count)++; //Increment the count
}
}

//Resizes(doubles) the array size if the max limit is achieved
void resizeArray(LocationArray **myArray, int *numberOfLoc){
*numberOfLoc = *numberOfLoc * 2; //Double the size

LocationArray *temp;
temp = (LocationArray*)realloc(*myArray, *numberOfLoc *sizeof(LocationArray)); //Reallocates more memory

//Checks if the memory heap is exhausted
if(temp == NULL){
printf("The memory heap is exhausted!\n");
}
else{
*myArray = temp; //Copy from the temp struct variable to myArray
}
}

//Prints all of the elements of the array
void printList(LocationArray **myArray, int *count){
if((*count) == 0){ //If the list is empty, then return
printf("The list is empty!");
return;
}

int i;
for(i = 0; i < (*count); i++){
printf("Location name: %s\n", (*myArray[i]).locName);
printf("Description: %s\n", (*myArray[i]).locDesc);
printf("Latitude: %f\n", (*myArray[i]).latitude);
printf("Longitude: %f\n", (*myArray[i]).longitude);
}
}

//Quits the program
void quitProgram(){
printf("Bye!");
exit(0);
}


int main()
{
printf("How many locations would you like to be inside the array?\n");
int numberOfLoc = 0; //variable for storing the size of the LocationArray
scanf(" %d", &numberOfLoc); //gets the user input and stores in numerOfLoc

LocationArray *myArray; //declares a LocationArray with the size of numberOfLoc
myArray = (LocationArray*)malloc(numberOfLoc*sizeof(LocationArray));

int count = 0;
while(1){
//Prints the menu
printMenu(&myArray, &count, &numberOfLoc);
}
//Free the pointer
free(myArray);
return 0;
}

最佳答案

printList 中,你有这些行

for(i = 0; i < (*count); i++){
printf("Location name: %s\n", (*myArray[i]).locName);
printf("Description: %s\n", (*myArray[i]).locDesc);
printf("Latitude: %f\n", (*myArray[i]).latitude);
printf("Longitude: %f\n", (*myArray[i]).longitude);
}

*myArray[i] 的使用不正确。这相当于 *(myArray[i])。您需要使用:(*myArray)[i]

for(i = 0; i < (*count); i++){
printf("Location name: %s\n", (*myArray)[i].locName);
printf("Description: %s\n", (*myArray)[i].locDesc);
printf("Latitude: %f\n", (*myArray)[i].latitude);
printf("Longitude: %f\n", (*myArray)[i].longitude);
}

您可以通过将参数更改为使 printList 中的代码更简单LocationArray *myArrayint count

void printList(LocationArray *myArray, int count){
if(count == 0){ //If the list is empty, then return
printf("The list is empty!");
return;
}

int i;
for(i = 0; i < count; i++){
printf("Location name: %s\n", myArray[i].locName);
printf("Description: %s\n", myArray[i].locDesc);
printf("Latitude: %f\n", myArray[i].latitude);
printf("Longitude: %f\n", myArray[i].longitude);
}
}

并确保更改对函数的调用。而不是

printList(myArray, count);

使用

printList(*myArray, *count);

进一步清理

scanf(" %[^\n]", &(*myArray)->locName);
scanf(" %[^\n]", &(*myArray)->locDesc);

两个帐户都错了。

  1. 您不需要使用 &
  2. 您需要使用 myArray 的第 count 个元素。以上始终将数据存储在 myArray 的第一个元素中。

使用

scanf(" %[^\n]", (*myArray)[*count].locName);
scanf(" %[^\n]", (*myArray)[*count].locDesc);

将其他 scanf 行也修改为:

scanf("%f", &(*myArray)[*count].latitude);
scanf("%f", &(*myArray)[*count].longitude);

关于c - 打印结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338796/

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