can anyone help me finish my program please. thank you

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

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

printf( "Enter product number \n");
scanf("%f",&);

printf( "Enter quantity : \n" );
scanf("%f",&);

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);
}
}

Dani AI

Generated

The original post contains several common mistakes: scanf calls are missing the addresses of variables and use the wrong format specifiers; some variables (like unit price and total) are used uninitialized; the switch lacks proper breaks and the final printf has no format string. correctly pointed out that product/quantity should be read as integers, and 's reminders about posting readable code and compiling with warnings are also relevant.

A practical approach:

  • Use integer types for product number and quantity (unless fractional quantities are required) and a double for price/total.
  • Read transactions in a loop so multiple sales can be entered; use a sentinel (commonly 0) to finish.
  • In the switch, map product numbers to unit prices, include a default case for invalid numbers, and always use break.
  • Initialize total to 0.0 and accumulate total += unitPrice * qty.
  • Print the final total with a format specifier like %.2f.

Example (complete, standalone) implementation:

#include <stdio.h>

int main(void)
{
    int item = -1;
    int qty;
    double unitPrice;
    double total = 0.0;

    while (1) {
        printf("Enter product number (0 to finish): ");
        if (scanf("%d", &item) != 1) break;
        if (item == 0) break;

        printf("Enter quantity: ");
        if (scanf("%d", &qty) != 1) break;

        switch (item) {
            case 1: unitPrice = 2.98; break;
            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:
                printf("Invalid product number %d — skipped.\n", item);
                continue;
        }

        total += unitPrice * qty;
    }

    printf("Total retail value: $%.2f\n", total);
    return 0;
}

Troubleshooting tips: always check scanf return values, reject negative quantities, compile with -Wall -Wextra to find mistakes, and consider storing prices in an array indexed by product number for cleaner code. For exact monetary math in production use, prefer integer cents or fixed-point libraries to avoid floating rounding issues.

Recommended Answers

All 6 Replies

Sorry, but you seem to have missed several ReadMes in your rush to post. Firstly, we are not here to do your homework. We're happy to help and answer any question you may have, though. Secondly, use CODE tags when posting code! Without them code is very unreadable.

I'd like to know how you even expect us to figure out what the rest of your assignment is without you telling us.

a mail order hopuse sells five different products whose retail prices are shown below
product number retail price
1-5 2.98,4.50,9.98,4.49,6.87
your program should use a switch statements to help determine the retail price for each product. your program shoulds also calculate and display the total retail value of all products sold last week.

And...? Does the code that you posted work? What part of this is giving you problems?

i havent tried it yet caz i'm stuck here what shud i put after & so it can work?
printf( "Enter product number \n");
scanf("%f",&);

printf( "Enter quantity : \n" );
scanf("%f",&);

Variables that will hold what the user enters.

i havent tried it yet caz i'm stuck here what shud i put after & so it can work?
printf( "Enter product number \n");
scanf("%f",&);

printf( "Enter quantity : \n" );
scanf("%f",&);

int product;
scanf( "%d", &product );

int quantity;
scanf( "%d", &quantity );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.