Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,470 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,954 people online right now. Registration is fast and FREE... Join Now!




help with c

 
Reply to this topicStart new topic

help with c, a mail order

ladiesman
9 Feb, 2008 - 03:20 PM
Post #1

New D.I.C Head
*

Joined: 9 Feb, 2008
Posts: 35

a mail order house sells five different products whose retail process are shown in the following table
product number retail price
1 2.98
2 4.50
3 9.98
4 4.49
5 6.87

write a program that reads a series of pairs of number as follows
A) product number
cool.gif quanitity sold for a day

your program should use a switch statmemnt to help determine the retail price for each product.
your program should calculate the display the total retail value of all products sold last week.
please help me



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With C
9 Feb, 2008 - 03:24 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
We require that you show us what you have already and then we can assist you in getting the program up and running. Give us some sort of attempt that makes some bit of sense and I am sure we can jump in with ya. smile.gif
User is offlineProfile CardPM
+Quote Post

ladiesman
RE: Help With C
9 Feb, 2008 - 03:32 PM
Post #3

New D.I.C Head
*

Joined: 9 Feb, 2008
Posts: 35

here's what i have

CODE

#include <stdio.h>
#include <math.h>
int main()

{
int product1 = 2.98;
int product2 = 4.50;  
int product3 = 9.98;
int product4 = 4.49;
int product5 = 6.87;
int quantity = 1
double unitprice;
int result;
double total;

printf( "Enter product number ");


printf( "Enter quantity : " );


switch(product) {
case 1:
unitPrice = 2.98;
break;
case 2:
unitPrice = 4.50;
break;
case 3:
unitPrice = 9.95;
break;
case 4:
unitPrice = 3.89;
case 5:
unitPrice = 9.98
break;
default:



total = unitprice * quantity;
    printf( "total is:",total);
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 9 Feb, 2008 - 03:33 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With C
9 Feb, 2008 - 04:06 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is the basic idea. We collect the product number and the quantity and use them in an if statement to determine the unit price and calculate the total value for that item. We then add the total to a running accumulator value (called total) until they either enter an invalid product number or enter zero.

Once they do that we spit out the total value.

To make this even better and something you can do to improve it, check to make sure that they can't enter a negative quantity and if they do, have it ignore the item they entered. Unless the teacher wants you to accept negative quantities as a sign of the product being returned.

CODE

#include <stdio.h>
#include <math.h>

int main()
{
    int quantity = 0;
    int result = 0;
    int product = 0;

    double unitPrice = 0.0;
    double total = 0.0;

    // Collect the product number
    printf("Enter product number (1-5) or 0 to quit: ");
    scanf("%d",&product);

    // While the product number is between 1 and 5 execute
    // our prompt for quantity and keep track of total.
    while ((product > 0) && (product <= 5)) {
        printf("Enter quantity: ");
        scanf("%d",&quantity);

        // Based on product number, set unitPrice
        switch(product) {
            case 1:
                unitPrice = 2.98;
                break;
            case 2:
                unitPrice = 4.50;
                break;
            case 3:
                unitPrice = 9.95;
                break;
            case 4:
                unitPrice = 3.89;
                break;
            case 5:
                unitPrice = 9.98;
                break;
            default:
                break;
        }
        
        // Keep track of total
        total += (unitPrice * quantity);

        // Prompt for next item and repeat process
        printf( "Enter product number (1-5) or 0 to quit: ");
        scanf("%d",&product);
    }

    // When they enter invalid product number or 0, spit out result.
    printf("\nTotal is: $%.2f\n\n",total);
}


Read the in code comments to see how this goes and looks. Enjoy!

"At DIC we be price and quantity calculating code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

etphonehome
RE: Help With C
13 Mar, 2008 - 11:32 AM
Post #5

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 2

Hi,

I need to do this exercise also. However, I need to do it only with iostream (cout, cin, endl...), can some one help me. Thank
User is offlineProfile CardPM
+Quote Post

red_4900
RE: Help With C
13 Mar, 2008 - 11:50 AM
Post #6

Code T(h)inkers
****

Joined: 22 Feb, 2008
Posts: 866



Thanked: 13 times
My Contributions
QUOTE(Martyr2 @ 9 Feb, 2008 - 04:24 PM) *

We require that you show us what you have already and then we can assist you in getting the program up and running. Give us some sort of attempt that makes some bit of sense and I am sure we can jump in with ya. smile.gif

it's stated there! clearly.
User is offlineProfile CardPM
+Quote Post

etphonehome
RE: Help With C
14 Mar, 2008 - 11:05 AM
Post #7

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 2

Hi,
Here is what I have done. It work well exept one little thing. If I enter a wrong product number (for example "6"), the program end. I would like my program to inform the user that the product ("6") is not valid and ask him to re-enter a product number, instead of ending the program. (As I said in the previous message I need to do this exercise with ''iostream'' only.

Thanks,



// Exercise 5.14

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int quantity = 0; // initialise variable integer
int product = 0;

double unitPrice = 0.0; // initialise variable decimal (price)
double total = 0.0;

cout<<"Enter the product number (1 to 5) or enter 0 to exit: "; // collect the product number
cin>>product;

while (product!=0) // while loop stop when user enter a product number different than 1, 2, 3, 4 and 5
{
cout<<"Enter quantity: ";
cin>> quantity;

switch(product) // switch begins
{
case 1: // when case 1 is enter, it store 2.98$ into unitPrice
unitPrice = 2.98;
break; // exit switch and go to while loop again
case 2:
unitPrice = 4.50;
break;
case 3:
unitPrice = 9.98;
break;
case 4:
unitPrice = 4.49;
break;
case 5:
unitPrice = 6.87;
break;
default:
cout<<"Invalid product"<<endl;
break;
}

total += (unitPrice * quantity); // compute the total and add it to final total


cout<<"Enter the product number (1 to 5) or enter 0 to exit: "; // collect the product number
cin>>product;
}

cout<<"\nTotal is :" <<total <<" $" <<endl; // print total
return 0; // endicate successful termination
}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Help With C
14 Mar, 2008 - 11:25 AM
Post #8

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Run this modified code, I have changed the placement of the total calculation, as you can see. Now the total is computed only when the product input is valid.

*edit : The program does exactly what you wanted to do when an input is invalid. Or is there something else you want to do?


CODE

#include <iostream>
using namespace std;

int main() {
    int quantity = 0; // initialise variable integer
    int product = 0;

    double unitPrice = 0.0; // initialise variable decimal (price)
    double total = 0.0;

    cout<<"Enter the product number (1 to 5) or enter 0 to exit: "; // collect the product number
    cin>>product;

    // while loop stop when user enter a product number different than 1, 2, 3, 4 and 5
    while (product!=0)  {

        cout<<"Enter quantity: ";
        cin>> quantity;

        // switch begins
        switch(product)  {
            case 1: // when case 1 is enter, it store 2.98$ into unitPrice
                unitPrice = 2.98;
                total += (unitPrice * quantity); // compute the total and add it to final total
            break; // exit switch and go to while loop again
            case 2:
                unitPrice = 4.50;
                total += (unitPrice * quantity); // compute the total and add it to final total
            break;
            case 3:
                unitPrice = 9.98;
                total += (unitPrice * quantity); // compute the total and add it to final total
            break;
            case 4:
                unitPrice = 4.49;
                total += (unitPrice * quantity); // compute the total and add it to final total
            break;
            case 5:
                unitPrice = 6.87;
                total += (unitPrice * quantity); // compute the total and add it to final total
            break;
            default:
                cout<<"Invalid product"<<endl;
            break;
        }

        


        cout<<"Enter the product number (1 to 5) or enter 0 to exit: "; // collect the product number
        cin>>product;
    }

    cout<<"\nTotal is :" <<total <<" $" <<endl; // print total
    return 0; // endicate successful termination
}



Hope that helps and Welcome to </dream.in.code> smile.gif

This post has been edited by letthecolorsrumble: 14 Mar, 2008 - 11:38 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month