Creating a grocery list via string inputs and void functions - Input string does not display

Viewed 59

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)

1 Answers

After trying out your code, I received a segmentation fault where you have scanf("%s",grocery);. Try allocating space to the variable, using malloc(3) (see http://man7.org/linux/man-pages/man3/malloc.3.html). For example, char *grocery = (char *)malloc(sizeof(char) * 100); will give you 100 characters to read into your grocery variable from input.

The same objective can also be achieved by simply using char grocery[100]; as you have with other variables.

Furthermore, as many programmers would suggest, try not to use goto(). This can cause spaghetti code (see https://en.wikipedia.org/wiki/Spaghetti_code). Instead, use a while loop that stops only when the user inputs some action designed to stop entering requests.

Lastly, just so you avoid unwarranted results, \n is actually a single character. Therefore, char slashN[4] = "\n" can be char slashN = '\n';.

Happy coding! :D

Related