How to store the users input into an array?

Nathan Johnson Source

I know from my code it's a bit of a mess as I'm a beginner to programming. I just wanted to see if someone could just quickly help me out by letting me know how to get the users input stored into an array and do what my code already does. So, I want the user to input the amount of tickets and then that gets stored in an array then printed like my code already does. Just want to try and cut down my code as it is very messy and need to store it in an array I've been told but I'm stumped at the moment.

#include <stdio.h>
#include<conio.h>
#include<stdbool.h>
#define MAX 10

void showPrices();
void showRoutes();
void orderTickets();

void main()
{


    int userChoice;


    while(1)
    {
        // Looping the menu for the user
        printf("\n\n**** MENU ****");
        printf("\n*NOTE* MAX TICKETS PER CUSTOMER = 10");
        printf("\n1. Show Prices\n2. Show Routes\n3. Order Tickets\n4. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &userChoice);

        //gets to chose between the different options
        switch(userChoice)
        {
            case 1: showPrices();
                break;
            case 2: showRoutes();
                break;
            case 3: orderTickets();
                break;
            case 4: exit(0);
            default: printf("\nInvalid, try again!!!");
        }
    }

}

void showPrices()
{
    //for the switch statement, this is what I want to be displayed when they pick this option
    printf("\n\n---- Prices - ALL DAY ----");
    printf("\n1. Child = £3\n2. Teenager = £5\n3. Standard = £10\n4. Student = £7\n5. Senior = £6");
}
void showRoutes()
{
    //for the switch statement, this is what I want to be displayed when they pick this option
    printf("\n\n---- Routes - ALL DAY ----");
    printf("\n---- Leaving from Nottingham Station ----");
    printf("\nDestinations:\nDerby\nLeicester\nSheffield\nBirmingham");
}
void orderTickets()
{
    int ticketArray[tickets];
    int age;
    int userInput = 0;
    int tickets;
    int ticketSum = 0;
    int ticketNumber = 1;
    int sum;
    int userDesti;
     //Main function where the user oders the tickets!

     printf("\nWhere do you want to go?");
     printf("\n1. Derby\n2. Leicester\n3. Sheffield\n4. Birmingham");
     scanf("%d", &userDesti);

     //asks the user how many tickets they want
    printf ("\nHow many tickets do you need?: ");
    scanf  ("%d", &tickets);

    //loop to keep asking the user to input the information they need to enter for each ticket until it reaches 0
    while (tickets > 0)
    {
        /*only does this if they enter 10 or less and more than 0
        if (tickets <= 10 && tickets > 0)
        {

            printf("\nHow old are you?: ");
            scanf("%d", &age);

            if(age < 10)
            {
                printf("\nChild Ticket = £3\n");
                ticketSum = ticketSum + 3;

            }
                else if (age < 16)
            {
                printf ("\nTeenager Ticket = £5\n");
                ticketSum = ticketSum + 5;

            }
            else if (age >= 16 && age < 60)
            {
                printf("\nStandard Ticket = £10\n");
                ticketSum = ticketSum + 10;

            }
            else
            {
                printf("\nSenior Ticket = £6\n");
                ticketSum = ticketSum + 6;

            }

        }
         //if they enter an invalid number
        else
        {
            printf("Error, invalid number of tickets!!\n");
            break;
        }*/


        // this allows the student discount to be deducted from the final price
            printf("\nAre you a student? Enter 1 for yes or 2 for no: ");
            scanf("%i", &userInput);

            if(userInput == 1)
            {
                printf ("\nYou are eligible for the student discount = £3 deducted\n");
                //sum to deduct the discount
                ticketSum = ticketSum - 3;
            }
            //if they aren't a student then they will get this message
            else if (userInput == 2)
            {
                printf ("\nYou are not eligible for the student discount, sorry!\n");
            }




        // gives the user their number for that ticket
        printf("\n------------------");
        printf("\nTicket Number: %d ", ticketNumber);
        printf("\n------------------\n");
        ticketNumber++;


        // lets the user know how much each ticket adds onto the previous 
        printf("\n-----------------------");
        printf("\nThe summary so far: %d ", ticketSum);
        printf("\n-----------------------\n");

        // gets out of the main loop when the tickets are less than or equal to 0
        if (tickets <= 0 )
        {
            break; 
        }

        //takes away the value of tickets each time so if they want 2 tickets then it takes away 1 so then it equals 1 left umtil it reaches 0
        tickets--;

    }

    //the receipt of all the tickets added together. 
        printf("\n-----------------------------------");
        printf("\n-----------------------------------");
        printf("\nThe receipt for your order: £%d", ticketSum);
        printf("\n-----------------------------------");
        printf("\n-----------------------------------");

}
carrayssorting

Answers

comments powered by Disqus