d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic C - Cash Register
12Next
Add Reply New Topic New Poll
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 28 2013 11:54am
Could anyone help me? I have an assignment to do, learning how to program in C.

My task is to essentially create a cash register using C and using a template provided by my professor which all it has is a do while loop since we haven't covered it yet.

We've really only learned basic I/O stuff and if/elseif stuff, so if you could please leave assistance to only those things or close to it.

Code block #2 is the assignment outline, the chart is kind of messed up in the first part but that is the part I don't know how to setup.

I stopped my approach but what I thought to do was essentially, enter the product code and store that in code variable. Now, if the the product code is one of the 5 product codes, it will multiply the quantity by the listed price. But the next line is what I don't know how to do. Say the product code is, 12199 that we chose. How do we get the program to read that "1" and "2" are the first 2 numbers of the product code so that it prints "Produce bulks, Local fruits" on the next line?

ANY HELP IS GREAT! THANKS!

Code

#include <stdio.h>

// function prototypes
void PrintHeader();
void PrintFooter();

// PrintHeader: Display the header information of the receipt
// Input: no paramenters required, call it at the start of a new receipt
// Output: no return, simply outputs the appropriate header messages
void PrintHeader()
{
// TODO: Print the header lines, title, and the welcome message
                 printf("----------------------------------------------------------------------------------------------------------------\n\n");
                 printf("                                        _-===Windsor Market Farm Fresh ===-_\n\n");
                 printf("Welcome!\n");
}


// PrintFooter: Display the footer information of the receipt
// Input: no paramenters required, call it at the end of a receipt
// Output: no return, simply outputs the appropriate footer messages
void PrintFooter()
{
// TODO: Print the footer lines and thank you message
                 printf("----------------------------------------------------------------------------------------------------------------\n\n");
                 printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n");
                 printf("----------------------------------------------------------------------------------------------------------------\n");
}


int main()
{
// TODO: Variable Declarations and Initializations here
int code=0; // use this variable to enter the product codes
                 float quantity;
                 float subTotal;


// Display Heading
PrintHeader();


// Main program loop
do
{
 // TODO: Prompt for item, get the quantity, update sub-total;
 // if the code is 00 you should compute totals and calculate the change.
                                    printf("Enter Item Code: ");
                                    scanf("%d", &code);
                                    printf("Enter Quantity: ");
                                    scanf("%f", &quantity);


}while(code != 0); // this loop will keep repeating until a code of zero is entered! (00=0)



// Display Footer
PrintFooter();

return 0;
}


Code

In this assignment you are asked to develop an interactive C program that implements an artificial calculator to generate a complete receipt for all shopping items. The idea is that the user first enters how much money he/she has saved and is ready to spend. The computer will then search for possible items from our list and provide the user with a revised list of items that he/she can afford. The complete list of shopping items is:
Department
Meaning
Example: corresponding display
1=Produce Bulk
1=local vegetables; 2=local fruits; 3=imports
12199 = Produce Bulk, local fruits, $1.99/lb
2=Produce Item
1=local vegetables; 2=local fruits; 3=imports
13350 = Produce Item, imports, $3.50 each
3=Floral
1=roses; 2=bouquet; 3=house plant
33999 = Floral, house plant, $9.99 each
4=Dairy
1=milk; 2=eggs; 3=butter; 4=yogurt
44099 = Dairy, yogurt, $0.99 each
5=Meats
1=beef; 2=chicken; 3=turkey; 4=pork; 5=rabbit
51299 = Meats, beef, $2.99/lb
6=Other
0=other item
60899 = Other, other item $8.99 each
7-9 unused yet
Program logic (Cash register logic): 1. The clerk types in the code for each item the customer is purchasing followed by the quantity.
2. As each item code is entered its ‘corresponding display’ is printed, and its price added to the accumulating total.
3. Once all the items are entered the cashier enters ‘00’ to compute the total with taxes at 7%.
4. The user provides the cash amount, the cashier enters the cash amount and the register calculates the change.


SAMPLE OUTPUT
----------------------------------------------------------------------
_-=== Windsor Market Farm Fresh ===-_
Welcome!
Enter Item code: 12199
Enter Quantity: 2.5
> Produce Bulk, local fruits, $1.99/lb @ 2.5 = $4.98
Enter Item code: 44099
Enter Quantity: 3
> Dairy, yogurt, $0.99 each @ 3 = $2.97
Enter Item code: 4444
<INVALID CODE, PLEASE TRY AGAIN>
Enter Item code: 00
----------------------------------------------------------------------
> SUB-TOTAL $7.95
> TAX@7% $0.56
----------------------------------------------------------------------
> TOTAL $4.45
----------------------------------------------------------------------
Enter CASH AMOUNT: 20
>CHANGE TO CUSTOMER DUE: $15.55
----------------------------------------------------------------------
THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN
----------------------------------------------------------------------

Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 28 2013 12:37pm
You could use some simple math to get the first 2 digits.
Since the code entered is an integer, just divide that number by 1000 to remove the last 3 digits.
so 12199/1000 = 12
if you want the last 3 numbers, use modulus: 12199 % 1000 = 199

Then do a series of If statements
like

Code
if ( code/1000 == 12 ) {
   printf("Produce Bulk, local fruits");
}


This post was edited by DirtyRasa on Jan 28 2013 12:37pm
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 28 2013 12:51pm
Wow thanks ! I completely forgot about that.

Another question I did not mention is, it tells you that entering 00 computes the subtotal.

Is it easier to just make it so that it adds all of the totals together or make it so that after each product code and quantity is typed update the subtotal variable with the new value then output the value at the end?
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 28 2013 01:34pm
Probably after each product code.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 28 2013 02:19pm
id use fgets to read the product code into a character array from stdin and then you can process the numbers away via loops and such.

dirtyrasa's way might be simpler for you though

edit:: i believe using strncmp() will make my method easier. could also use pointers but i guess this is already getting a bit out of what you learned so far

This post was edited by AbDuCt on Jan 28 2013 02:21pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 28 2013 02:27pm
I tried to keep it as simple as possible :)
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 29 2013 12:29pm
Modulus of a product of 12199 would give 199, how can I make that 1.99 and multiply by quantity?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 29 2013 02:11pm
Quote (Saterial @ Jan 29 2013 02:29pm)
Modulus of a product of 12199 would give 199, how can I make that 1.99 and multiply by quantity?


convert it to a float or double type and divide by 100? you wont get precision unless you change data types

This post was edited by AbDuCt on Jan 29 2013 02:12pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 30 2013 09:58am
Quote (AbDuCt @ Jan 29 2013 02:11pm)
convert it to a float or double type and divide by 100? you wont get precision unless you change data types


This

Since it's a very beginning course, I doubt the professor cares about precision.
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Feb 6 2013 10:32am
If someone can help that would be great! My "Enter Item Code:" keeps looping although I haven't coded the portion of what to do if it is a valid code yet. Even if it is an invalid code, it keeps on looping Enter Item Code or previously it would say the "<INVALID CODE, PLEASE TRY AGAIN> but it wouldn't loop back to the "Enter Item Code:, instead it would go onto "Enter Quantity:"

Any help would be great thanks!

Code
#include <stdio.h>

//function prototypes
void PrintHeader();
void PrintFooter();

void PrintHeader()
{
   printf("---------------------------------------------------------------------\n\n");
   printf("                _-=== Windsor Market Farm Fresh ===-_\n\n");
   printf("Welcome!\n");
}


void PrintFooter()
{
   printf("---------------------------------------------------------------------\n\n");
   printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n");
   printf("---------------------------------------------------------------------\n");
}

int main()
{
   int code;     //use this variable to enter the product codes
   float quantity;
   float subTotal;
   float paidCash;
   //float price;
   int validCode;
   
   //Display Heading
   PrintHeader();
   
   //Main Program loop
   do
   {
       do
       {
       printf("Enter Item Code: ");                //prompt for item code
       scanf("%d", &code);                         //retrieve input from keyboard
       
       
       //Check if 5 digits
       if (code < 10000 && code > 99999)
           validCode = 0;
       
       //Check 1st digit
       else if (code / 10000 == 7)
           validCode = 0;
       else if (code / 10000 == 8)
           validCode = 0;
       else if (code / 10000 == 9)
           validCode = 0;
       
       //Check 2nd digit
       else if (code / 1000 % 10 > 4)
           validCode = 0;
       else if (code / 10000 == 6 && code / 1000 % 10 > 0)
           validCode = 0;
       
       if (validCode == 0)                                            //if code is invalid, it will loop back to the beginning and ask for item code again
           printf("<INVALID CODE, PLEASE TRY AGAIN>\n");
       
       }while (validCode == 1);                                            // if code is valid, prompt for quantity
           printf("Enter Quantity: ");                                     // prompt quantity
           scanf("%f", &quantity);                                         // retrieve from keyboard
       
       
           }while(code !=0 || code != 00);                                 //loop will be repeated until item code 0 is entered
   
   //print and calculation statments to determine subtotal, total, tax and change
   printf("---------------------------------------------------------------------\n");
   printf("\t> SUB-TOTAL\t %.2f\n", subTotal);
   printf("\t> TAX@7%%\t %.2f\n", subTotal * 0.07);
   printf("---------------------------------------------------------------------\n");
   printf("\t> TOTAL\t %.2f\n", subTotal * 1.07);
   printf("---------------------------------------------------------------------\n");
   
   printf("Enter Cash Amount: ");
   scanf("%f", &paidCash);
   
   printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n", paidCash - subTotal * 1.07);
   
   //Display Footer
   PrintFooter();

   return 0;
}


This post was edited by Saterial on Feb 6 2013 10:40am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll