i need alil help with my switch question
I have a question here that i cant solve...
4.19 A mail order house sells five different products whose retail price are shown in the following
Product Number ------ Retail Price
1 ------ $ 2.98
2 ------ $ 4.50
3 ------ $ 9.98
4 ------ $ 4.49
5 ------ $ 6.87
Write a programm that reads a series of pairs of numbers as follows:
1. Product number
2. Quantity sold for one day
Your program should use a switch statment to help determine the retail price for each priduct.Your programm should calculate and display the total retail values of all products sold last week.
This question is from a book which i am learning C from...
Quzah my code (I did try it)
#include <stdio.h>
int main()
{
int cnt;
float num,num1, num2, num3, num4, num5,nu1,nu2,nu3,nu4,nu5;
num1 = 2.98;
num2 = 4.50;
num3 = 9.98;
num4 = 4.48;
num5 = 6.87;
clrscr();
printf ("Enter a product number ");
printf ("Enter -1 to end");
scanf ("%d", & cnt );
while ( cnt != -1 ){
switch ( num ){
case '1':
nu1+=num1;
break;
case '2':
nu2+=num2;
break;
case '3':
nu3+=num3;
break;
case '4':
nu4+=num4;
break;
case '5':
nu5+=num5;
break;
default:
printf ("Incorrect product number");
printf ("\nEnter a product number");
break;
} }
printf (" The totals are: ");
printf ("1: %f", nu1 );
printf ("2: %f", nu2 );
printf ("3: %f", nu3 );
printf ("4: %f", nu4 );
printf ("5: %f", nu5 );
getch();
return 0;
}
yeah guys i will try to complete the code
Thanks for the points hammer and quzah....
what about this hammer( is it better ??)
/*
#include <stdio.h>
int main()
{
int swt,num1, num2, num3, num4, num5;
float awn1, awn2, awn3, awn4, awn5, total;
clrscr();
printf ("Enter the product number: ");
scanf ("%d", &swt );
while ( swt != -1 ){
switch ( swt ){
case 1:
printf ("Enter the amount for No.1: ");
scanf ("%d",&num1 );
awn1 = num1 * 2.98;
break;
case 2:
printf ("Enter the amount for No.2: ");
scanf ("%d", &num2 );
awn2 = num2 * 4.50;
break;
case 3:
printf ("Enter the amount for No.3: ");
scanf ("%d", &num3 );
awn3 = num3 * 9.98;
break;
case 4:
printf ("Enter the amount for No.4: ");
scanf ("%d", &num4 );
awn4 = num4 * 4.49;
break;
case 5:
printf ("Enter the amount for No.5: ");
scanf ("%d", &num5 );
awn5 = num5 * 6.87;
break;
default:
printf ("Incorrect number\n");
printf ("Enter your product number:");
break;
} }
total = awn1+awn2+awn3+awn4+awn5;
printf ("\nThe total for you products is: %d",total);
getch();
return 0;
}
*/
But it gives me an error it keeps on repeting the case .Example if i enter 2 ..It will ask me for the quantity after entering the quantity it wont break????
help me out
Re: hey quzah .Help me out
Quote:
..When i enter a number say 2 it then printf the statment that says to enter the quantity needed after i enter the quantity instead of it should break , it repeats the whole statment again .....And ya how do you insert code tags:confused:
This is because of the way you've coded the loop. The break statement exits the switch, not the while loop. The only point at which you can leave the while loop is when swt if -1, but you only update the value of swt BEFORE the while loop starts.
If you want it to ask for multiple product numbers, try adding
Code:
printf("Enter the product number: ");
scanf("%d", &swt);
as the last statements within the while loop.
Also, the following variables will need initial values
>awn1,awn2,awn3,awn4 and awn5
Can you see why?
Quote:
Originally post by quzah
One problem is you assign your variables 'num1' etc, a value without actually initializing them first:
int num1;
float answ;
answ = num1 * 3.14157;
All of the num1-5 variables don't need initialising to 1. They are initialised when they are used within scanf(). The thing that is missing is validation that scanf() actually worked properly (need to test the return code).
[EDIT] Sorry quzah, I see the original code has changed a bit compared to the latest post, and your comments were originally correct! Still, in the latest version, these variables don't need initialising.