Trying to learn C. To that end, I'm coding a program that creates a TroubleTicket report. The user is prompted to enter, priority, name and problem. I'm using a struct to store the data and create a struck array. Using the array approach for now as I'm developing the code. Eventually, I'd to use a linked list.
Anyhow, the get_input() function reads in the user input and returns a char pointer. Included the get_input() and create_ticket() functions.
#define NUM_TICKETS 10
char* get_input(int maxlen)
{
static char s[110];
char ch;
int i = 0;
int chars_remain = 1;
while (chars_remain)
{
ch = getchar();
if ((ch == '\n') || (ch == EOF))
{
chars_remain = 0;
}
else if (i < maxlen - 1)
{
s[i] = ch;
i++;
}
}
s[i] = '\0';
return s;
void create_ticket()
{
struct ticket
{
int priority;
int number;
char * name;
char * problem ;
char * assigned_to;
char * status ;
};
struct ticket tkt_array[NUM_TICKETS];
struct ticket new_ticket;
printf("Enter your name: ");
new_ticket.name = get_input(20);
printf("new_ticket.name: %s \n", new_ticket.name);
printf("Enter problem description: ");
new_ticket.problem = get_input(100);
printf("new_ticket.problem: %s \n", new_ticket.problem);
printf("Assigned to: ");
new_ticket.assigned_to = get_input(20);
printf("new_ticket.assigned_to %s\n ", new_ticket.assigned_to);
printf("Enter ticket status: ");
new_ticket.status = get_input(10);
printf("new_ticket.status: %s \n", new_ticket.status);
}
I noticed that initial input was read and displayed correctly but subsequent inputs overwrote prior input.
For example, after name was entered, the entered value is displayed
printf("Enter your name: ");
new_ticket.name = get_input(20);
printf("new_ticket.name: %s \n", new_ticket.name);
But after problem description is entered, new_ticket.name was changed to display the problem description text. Took me a while to figure out that the problem is the return s in get_char(). It returns a pointer. The address is the same but the value changes and struct ticket pointers point to the same address of return s from get_char(). How can I save the return value in a variable and not get it reset on subsequent call to get_input? s is a char *, how can I save the return value of a pointer in a variable?
printf("Enter problem description: ");
new_ticket.problem = get_input(100);
printf("new_ticket.problem: %s \n", new_ticket.problem);
printf("new_ticket.name: %s \n", new_ticket.name);
How can I save the return value in a variable and not get it reset on subsequent call to get_input? s is a char *, how can I save the return value of a pointer in a variable? I hope I clearly stated the issue.
getcharreturns andintvalue. This is rather important when you want to compare it with theintvalueEOF.staticarray inget_input, I recommend that you create an array in the calling function and pass it in to thegetinput instead:char *get_input(char *buffer, size_t buflen);staticvariables in functions?}beforevoid create_ticket(). Please review posted code for compilation correctness.