The addInfo() adds information into the array of structures and the function printList() outputs all of the elements of that array. But my printList() is outputting only the last element that was added. What can be wrong? Does it add elemets into the array incorrectly?
#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;
}
In printList, you have the lines
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);
}
Use of *myArray[i] is incorrect. That is equivalent to *(myArray[i]). You need to use: (*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);
}
You can make code in printList simpler by changing the argument to
LocationArray *myArray and int 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);
}
}
and make sure to change the call to the function. Instead of
printList(myArray, count);
use
printList(*myArray, *count);
Further cleanup
scanf(" %[^\n]", &(*myArray)->locName);
scanf(" %[^\n]", &(*myArray)->locDesc);
are wrong on two accounts.
You don't need to use &.
You need to use the count-th element of myArray. The above always store the data in the first element of myArray.
use
scanf(" %[^\n]", (*myArray)[*count].locName);
scanf(" %[^\n]", (*myArray)[*count].locDesc);
Modify the other scanf lines also to:
scanf("%f", &(*myArray)[*count].latitude);
scanf("%f", &(*myArray)[*count].longitude);
Related
I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );
#include <stdio.h>
#include <string.h>
#define mL 5
#define NL 20
#define UL 6
struct LIST
{
char n[NL];
float am;
char u[UL];
};
struct array
{
struct LIST array;
};
void addCityInformation(struct array *add, int *items);
void printCities(struct array *all, int items);
int main(void)
{
struct array shopping[mL];
int choice, nrOfItemsAdded = 0;
do
{
printf("\nWhat du you want to do?");
printf("\n1 - add grocery");
printf("\n2 - print shopping list");
printf("\n3 - exit");
printf("\nYour choice: ");
scanf("%d", &choice);
while(getchar() != '\n');
switch (choice)
{
case 1:
addCityInformation(&shopping[nrOfItemsAdded], &nrOfItemsAdded);
break;
case 2:
printCities(shopping, nrOfItemsAdded);
break;
case 3:
printf("Exiting program\n\n");
break;
default:
printf("Invalid input\n\n");
break;
}
}
while(choice != 3);
return 0;
}
int clean_stdin()
{
while (getchar()!='\n');
}
void addCityInformation(struct array *add, int *items)
{
if(*items == mL)
printf("No more space in the list\n");
else
{
printf("Enter name: ");
fgets(add->array.n, NL, stdin);
add->array.n[strlen(add->array.n)-1] = '\0';
do {
printf("Enter amount: ");
}while (scanf("%f", &add->array.am )); //loop untill other than float
getchar();
printf("Enter unit: ");
fgets((add->array.u), UL, stdin);
add->array.u[strlen(add->array.u)-1] = '\0';
(*items)++;
}
}
void printCities(struct array *all, int items)
{
printf("\n\n%-20s %-15s %-9s | %-6s\n", "Name", "amount", "unit");
printf("--------------------------------------------------------\n");
for(int i = 0; i < items; i++)
printf("%-20s %-15.1f %-9.4s \n", all[i].array.n, all[i].array.am, all[i].array.u);
}
This is my loop beside that i am only showing a part of the code. It now just continues to give enter amount and letting me register it in the struct. I want to restrict the user to only entering positive numbers and no character at all. And if he types a character it should rerun the loop even if it is 123Av123 it should run the loop and only register the correct number
Edit: now showing the whole code//loop untill other than float is what i want help with
int check=scanf("%f", &add->array.am )
if(check!=1||add->array.am<0){
printf("Incorrect input");
return 1;
}
I think that will do it.
Edit: you wanted it to rerun after so use continue; instead of return;
/*ARRAY INSERTION AT THE END*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,insrt;
char option;
int array[100];
printf("\nWHAT IS THE TOTAL NUMBER OF ARRAY YO WANT ?\n");
scanf(" %d", &n);
for(i=0;i<n;i++)
{
printf("\nENTER THE NO FOR YOUR ARRAY:\t");
scanf(" %d", &array[i]);
}
printf("\nYOU HAVE FOLLOWING NO IN THE LIST:\n");
for(i=0;i<n;i++)
{
printf(" %d==>", array[i]);
}printf("NULL");
do
{
printf("\nENTER THE VALUE TO BE INSERTED AT THE END:\t");
scanf(" %d", &insrt);
for(i=0 ;i<n;i++)
{
if (array[i] == 0)
{
array[i] = insrt;
printf("\nINSERTION SUCCESSFULL!!\n");
break;
}
else
{
printf("\nTHE ARRAY IS FULL.\n");
break;
}
}
printf("\nDO YOU WANT TO CONTINUE ? TYPE (Y FOR YES AND N FOR NO):\t");
scanf(" %c", &option);
}while(option == 'Y' || option == 'y');
printf("\nYOU HAVE FOLLOWING NO IN THE LIST:\n");
for(i=0;i<n;i++)
{
printf(" %d==>", array[i]);
}
printf("NULL");
getch();
}
Insertion at the end of the array is not working, tried everything I know but it is still not working and instead of insertion it only prints that array is full.
I know that there is a fault in loop statement but I am unable to fix it.
https://i.stack.imgur.com/xO6Jz.png
1st mistake:
You are taking input n after defining the array. It should be:
scanf(" %d", &n);
int array[n];
2nd mistake:
else
{
printf("\nTHE ARRAY IS FULL.\n");
break;
}
for i=0, a[i] = 432 this is not equal to zero, so it will go to else, and print "The Array is Full".
Just keep track of the number of elements inserted in array, if no_elem == n-1, then print "Array is full" and break;.
I want to run this code where I enter two strings and user put the option for performing the following task.
After I enter 'a' in the menu, the statement strcpy(s_1,s_2); the string is copied to s_1 but when I added
#include <string.h>
it asked me to use strcpy_s() which I did and code stopped working.
When I enter 'b' in the menu, I get only one output
Both Strings are equal to each other
I don't understand why strcmp() returns 0 always.
It would be great if someone help me out in this issue.
By the way I'm using Visual Studio 2015 for compiling my C code.
#include< stdio.h>
#include< string.h>
#include< stdlib.h>
#include< process.h>
//USER-DEFINED FUNCTIONS
char top_down();
char copy_function();
char compare_function();
//char adder_function();
void header(void);
#define MAX 1000
void header()
{
printf("*-*-*-*-*TASK_PERFORMER*-*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
main()
{
//VARIABLE DECLARATION
char x =
{ 0 };
//HEADING FUNCTION
header();
//USER-DEFINED FUNCTION CONSISTING OF ALL INPUTS.
top_down();
//TERMINAL-PAUSE
system("pause");
}
char top_down()
{
char s1[MAX] =
{ 0 }, s2[MAX] =
{ 0 }, x =
{ 0 };
printf("Enter the First String : \n");
fgets(s1, MAX, stdin);
printf("\n");
printf("The Entered First String : \n");
printf("%s", s1);
printf("\n");
printf("Enter the Second String : \n");
fgets(s2, MAX, stdin);
printf("\n");
printf("The Entered Second String : \n");
printf("%s", s2);
printf("\n");
printf("*-*-*-TYPE ANY OPTION TO PERFORM TASK-*-*-*");
printf("\n");
//GIVEN OPTIONS FOR SELECTOR
printf("Enter one option from the following : \n\n");
printf("(a) To Copy one string to another. \n");
printf("(b) To Compare two string. \n");
printf("(c) To Add a string to the end of another string. \n");
printf("\n");
repeat:
printf("Enter Your Option : \n");
scanf_s("%c", &x);
printf("\n");
//OPTION-SELECTOR
switch (x)
{
case 'a':
copy_function(s1, s2);
break;
case 'b':
compare_function(s1, s1);
break;
case 'c':
//adder_function(s1, s2);
break;
default:
printf("INVALID OPTION \n");
printf("Please Try Again \n");
goto repeat;
break;
return;
}
}
char copy_function(char s_1[], char s_2[])
{
int x = 0;
x = strlen(s_2);
printf("Second String will be copied to First string now \n");
//strcpy(s_1, s_2);
strcpy_s(s_1, x, s_2);
printf("\n");
printf("First String Output : \n");
printf("%s", s_1);
return;
}
char compare_function(char s_1[], char s_2[])
{
int a = 0, l1 = 0, l2 = 0, i = 0;
printf("First String will be compared to Second String now \n ");
//printf("\n");
if (strcmp(s_1, s_2) == 0)
printf("Both String are equal to each other \n");
else if (strcmp(s_1, s_2) > 0)
printf("First String is greater than Second String");
else
printf("First String is lesser than Second String \n");
return;
}
Not sure if your program is in progress. I modified it and removed anything I thought was not necessary.
The functions' signatures change to void
Removed x in main
Return type of main set to int (It is int by default, but this spares you the warning of the compiler)
I work on Linux, so I removed the system(...), feel free to add it.
Only left variables declarations in top_down (not important, but an initial value should have a meaning and in your case there is no meaning to 0)
Changed scanf_s("%c", &x); to scanf(" %c", &x); (note the space. not sure if on windows that makes a difference. please check.)
Changed the function call compare_function(s1, s1); to compare_function(s1, s2);
Removed the return statements in the void functions
In copy_function I removed int x = 0; and x = strlen(s_2);
In copy_function I changed strcpy_s(s_1, x, s_2); to strcpy(s_1, s_2);
In compare_function I removed int a = 0, l1 = 0, l2 = 0, i = 0;
You use label/goto. That's ok, but considered not cool nowadays. You could use a loop and break in case of an invalid option.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//USER-DEFINED FUNCTIONS
void top_down();
void copy_function();
void compare_function();
//char adder_function();
void header(void);
#define MAX 1000
void header() {
printf("*-*-*-*-*TASK_PERFORMER*-*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
int main() {
//HEADING FUNCTION
header();
//USER-DEFINED FUNCTION CONSISTING OF ALL INPUTS.
top_down();
return 0;
}
void top_down() {
char s1[MAX], s2[MAX], x;
printf("Enter the First String : \n");
fgets(s1, MAX, stdin);
printf("\n");
printf("The Entered First String : \n");
printf("%s", s1);
printf("\n");
printf("Enter the Second String : \n");
fgets(s2, MAX, stdin);
printf("\n");
printf("The Entered Second String : \n");
printf("%s", s2);
printf("\n");
printf("*-*-*-TYPE ANY OPTION TO PERFORM TASK-*-*-*");
printf("\n");
//GIVEN OPTIONS FOR SELECTOR
printf("Enter one option from the following : \n\n");
printf("(a) To Copy one string to another. \n");
printf("(b) To Compare two string. \n");
printf("(c) To Add a string to the end of another string. \n");
printf("\n");
repeat:
printf("Enter Your Option : \n");
scanf(" %c", &x);
printf("\n");
//OPTION-SELECTOR
switch (x) {
case 'a':
copy_function(s1, s2);
break;
case 'b':
compare_function(s1, s2);
break;
case 'c':
//adder_function(s1, s2);
break;
default:
printf("INVALID OPTION \n");
printf("Please Try Again \n");
goto repeat;
break;
}
}
void copy_function(char s_1[], char s_2[]) {
printf("Second String will be copied to First string now \n");
strcpy(s_1, s_2);
printf("\n");
printf("First String Output : \n");
printf("%s", s_1);
}
void compare_function(char s_1[], char s_2[]) {
printf("First String will be compared to Second String now \n ");
if (strcmp(s_1, s_2) == 0)
printf("Both String are equal to each other \n");
else if (strcmp(s_1, s_2) > 0)
printf("First String is greater than Second String");
else
printf("First String is lesser than Second String \n");
}
I'd like a user to enter the info into the array of structures but instead getting the 4 same type errors which is:
request for member 'locName' in something not a structure or union|
request for member 'locDesc' in something not a structure or union|
request for member 'latitude' in something not a structure or union|
request for member 'locName' in something not a structure or union|
in lines:
printf("Please enter the name for the location: \n");
scanf("%s\n", &*myArray->locName); //Gets the user input for the Location Name
printf("Now enter the description:");
scanf("%s\n", &*myArray->locDesc); //Gets the user input for the Location Description
printf("Now enter the value for the latitude:");
scanf("%lf\n", &*myArray->latitude); //Gets the user input for the Latitude
printf("Now enter the value for the longitude:");
scanf("%lf\n", &*myArray->longitude); //Gets the user input for the Latitude
Could somebody point to the mistake in the code that I have made? Note that if the array is full, I need to resize() the size of the array!
#include <stdio.h>
#include <stdlib.h>
//structure Location
typedef struct Location{
char locName[35];
char locDesc[85];
float latitude;
float longitude;
} LocationArray;
//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: ");
printf("\nType 'A' or 'a' for adding additional location to the array");
printf("\nType 'P' or 'p' for printing the current list of locations");
printf("\nType 'Q' or 'q' to quit the program");
char input = 0;
scanf("%c", &input);
//Handles the invalid character input
//exits if the character does not correspond
//to the valid menu option
if(input != 'A' || input != 'a'
|| input != 'P' || input != 'p'
|| input != 'Q' || 'q'){
printf("Invalid character! Try again!");
exit(0);
}
//Calls other functions
//based on the character input
else{
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); //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 max){
if(*count == max){ // Checks if the array is already full
resizeArray(*myArray, max); //Resizes the array if it's full
}
else{ //Else, a used fills it out
printf("Please enter the name for the location: \n");
scanf("%s\n", &*myArray->locName); //Gets the user input for the Location Name
printf("Now enter the description:");
scanf("%s\n", &*myArray->locDesc); //Gets the user input for the Location Description
printf("Now enter the value for the latitude:");
scanf("%lf\n", &*myArray->latitude); //Gets the user input for the Latitude
printf("Now enter the value for the longitude:");
scanf("%lf\n", &*myArray->longitude); //Gets the user input for the Latitude
*count++; //Increment the count
}
}
//Resizes(doubles) the array size if the max limit is achieved
int resizeArray(LocationArray **myArray, int numberOfLoc){
LocationArray *temp;
temp = (LocationArray*)realloc(*myArray, numberOfLoc*sizeof(LocationArray));
//Checks if the memory heap is exhausted
if(temp == NULL){
printf("The memory heap is exhausted!");
return 0; //returns 0 that represents the heap is exhausted
}
else{
*myArray = temp;
free(myArray);
return 1; //If the heap is not exhausted, then return 1 (success)
}
}
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));
//Print the menu
int count = 0;
printMenu(&myArray, &count, numberOfLoc);
//Free the pointer
free(myArray);
return 0;
}
It is an array of struct , we need an index to specify the specific struct you are going to add to in addInfo, look at the last line in this extract and replace the 0 in mvArrav[0] with mvArrav[i] where i is the index. You need to modify your logic to pass the index.
void addInfo(LocationArray **myArray, int *count, int max){
if(*count == max){ // Checks if the array is already full
resizeArray(*myArray, max); //Resizes the array if it's full
}
else{ //Else, a used fills it out
printf("Please enter the name for the location: \n");
scanf("%s\n", myArray[0]->locName); //Gets the user input for the Loca