I'm very new in C programming and I need to create a phone book. Each entry must have a name, a phone number and a notes section. The thing is that the size of notes section should be changeable according to input. For example, if the user enters a string with 30 characters, size of note should be 30. I couldn't figure out how to do it.
Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <note>
struct Entries {
char name[50];
char phoneNumber[11];
char * note;
note = (char *)malloc(5*sizeof(char));
}Entries;
int main()
{
int command;
int counter = 0;
char tempNote[50];
char tempName[50];
char tempPhoneNumber[11];
while(1){
printf("Welcome to myPhoneBook! Please select an option:\n 1) New entry\n 2) List all entries\n 3) Edit\n 4) Delete\n 5) Search\n");
scanf("%d", &command);
if(command == 1){
struct Entries entry;
printf( "Enter a name:\n");
scanf("%s",tempName);
strcpy(entry.name, tempName);
printf( "Enter a phone:\n");
scanf("%s",tempPhoneNumber);
strcpy(entry.phoneNumber, tempPhoneNumber);
printf( "Enter a note:\n");
scanf("%s",tempNote);
entry.note = (char *)realloc(entry.note,strlen(tempNote));
strcpy(entry.note, tempNote);
counter++;
}
}
return 0;
}
When the user enters name, phoneNumber and note for the first time, the program automatically stops working despite the fact that it should've asked to user the attributes forever.