C Projects | Create a Champagne Shopping Program Using Pointers and Arrays with C programming language

 

C projects
C Projects

In this project, we are going to create a champagne shopping program using pointers and arrays with C programming language. 

Table of Contents

Introduction

Before we see the code to create this program, I will like to drop a brief explanation on arrays and pointers. But this tutorial will not go into details but only the basics and then we write the codes. 

An array of characters is just a list of characters that has a name. An array of integers is a list of integers that has a name, and an array of floating-point values is a list of floating-point values that has a name. Hence an array contain values called elements. An array can only contain only elements that are of the same types. That is an integer array contains only integer, and a floating-point array contains only floating point. 

A pointer variable hold addresses of other variables, (that is their primary purpose). That is a pointer links or points to the address of other variables stored in that program. An array name is nothing more than a pointer to the first element in that array. The array name is not exactly a pointer variable; Array names are known as pointer constants

Warning: 

The explanation above don't do much, because it is assumed, that you already know about pointers and arrays before you practice this project. So do well to get acquitted with arrays and pointers.  

Lets Begin!

C Projects | Create a Champagne Shopping Program Using Pointers and Arrays with C programming language
Create a Champagne Shopping Program Using Pointers and Arrays with C programming language

Now lets write some codes!!!

This is a practice program to learn about about pointers and arrays. The program will be use to make an order for five different kinds of champagne. The program has two parallel arrays; the first array is a pointer for holding strings, and the second array holds float numbers. Each type of champagne will be displayed and the client will be required to click a 'Y' for the champagne he/she wants and to click 'N' for the champagne not needed. After the selection, the program will print out total drinks ordered and the total bill.

I used code::blocks for this project. It is an IDE used for writing and running C and C++ codes.

Procedures

Step 1

Type the following header codes in your editor; without them many parts of the projects will not run.

#include <stdio.h>
#include <ctype.h>

Step 2

Define the variables that will be required to run the program inside the main() function

main()
{
    int i;
    int ctr = 0;
    int count = 1;
    char ans;

Step 3

Define and initialize the pointer variable to hold the names of the champagne collection inside the main() function

// pointer variable define and initialized to hold names of champagne
    char * drinks[5] = {"Gout de Diamants, Taste of Diamonds",
                        "Moet & Chandon Dom Perignon Charles & Diana 1961",
                        "Krug Clos d'Ambonnay",
                        "Boerl & Kroff Brut",
                        "Boerl & Kroff Brut Rose"};

Step 4

Define and initialize a parallel array variable to hold the amount for the drinks and the total bill inside the main() function  

// parallel array variable define and initialized to hold amount for drinks
    float amount[5] = {9827.0, 4244.0, 2701.0, 2489.0, 2200.0};
    float total = 0.00;

Step 5

For the next line, write the introduction to give instruction to the user of the program.

printf("\n\n***Moltose Wine Collection***");
printf("\nThe following are old champagne available for shipping to any country");
printf("\nYou can only order one quantity for each champagne by clicking Y (for yes) or N (for No)");

Step 6

Write the loop code for the user to select the drinks they want still within the main () function. If the user click Y, the program the prints out the amount for the drink and continue the loop, but if the user click N, the program continues to the next drink.

for (i=0; i<5; i++)
    {
        printf("\n\nDo you want this (Y/N)?");
        printf("\n%s: ", drinks[i]);
        scanf(" %c", &ans);
        if ((toupper(ans)) == 'Y')
        {
            printf("\nThe amount is %.2f$", amount[i]);
            total += amount[i];

            continue;
        }
        else if ((toupper(ans)) == 'N')
        {
            amount[i] = 0.0;
        }
    }

Step 7

Write the code to print out all the drinks ordered, the amount and the total bill

//print the drinks ordered and the total price
    printf("\n\n**The following is your order!**\n");
    for (i=0; i<5; i++)
    {
        printf("%d. %s\t %.2f$\n", count++, drinks[i], amount[i]);
    }
    printf("Total Amount = %.2f$\n", total);

    return (0);

The video below shows, the full code, and how it works.


Conclusion

From testing the program, the drinks you selected will get their amount printed along side while the one you don't want will have zero printed along side. Run the program as many times as you want to be sure.

C project completed
Congratulations: C project completed

Don't forget to check for more C programming projects here

Follow us on our social media handles to stay updated.