I'm fairly new to C (high school student), and my goal is to make a grocery list by inputting a set of characters. Then, the output would print out what I currently added to the list. This would go on forever until I exited the program or went to the main menu.
SCREEN1
00 - GO TO GREETINGS SCREEN
01 - ADD MORE ITEMS TO THE LIST
CODE ENTRY: ___
SCREEN2
Then I entered 01 to add items:
Input "DONE" to end program and see final list.
LIST ENTRY: ______________
SCREEN3
Then I add "apples", then it takes me to this screen:
GROCERY LIST:
POTATOES
FISH
APPLES
After that, it takes me back to SCREEN1, where I would choose whether to go to the greetings screen or add some more.
MY CURRENT CODE:
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n;
char * grocery;
mainMenu:
system("cls");
n = 0;
printf("00 - Go to greetings screen\n01 - Add groceries to list\nENTRY: ");scanf("%d",&n);
if(n == 0)
{
greetings();
goto mainMenu;
}
else if(n == 1)
{
printf("GROCERY ENTRY: ");scanf("%s",grocery);
add(grocery);
goto mainMenu;
}
else
{
printf("Wrong value added. Try again.");
sleep(2);
goto mainMenu;
}
}
void greetings()
{
system("cls");
printf("hello! press any key to go back to menu");
getch();
system("cls");
}
void add(char * a)
{
system("cls");
char listData[1000] = "",slashN[4] = "\n";
strcat(listData,a);
strcat(listData,slashN);
printf("THINGS TO BUY:\n");
puts(listData);
}
NOTES
I used strcat so that it remembers the value of the original string. This'll make sure that the new input will just be put on top of the old data, right? (Hopefully, my logic is correct on that one)
Though, I have not been able to find out whether or not the string data will still be remembered by the program even if I am switching to other menus, such as the Greetings Menu (which is a placeholder for something like a calculator).
Right now, the initial functions work, except for the the grocery list one. Whenever I input a string, nothing comes out of it, even the printf("THINGS TO BUY:") part, leading me to think it's something with what I entered in void add(char * a)