i need alil help with my switch question

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-28-2002
datainjector
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...
• 06-28-2002
quzah
> I have a question here that i cant solve...

Perhaps if you actually tried, you'd be able to solve it? I definately do not see your attempt posted.

Quzah.
• 06-28-2002
datainjector
Quaza my code
#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;
}
• 06-28-2002
Hammer
In the switch test, you can't use a float, which is what you have tried to do (the num var is a float).

There are some other bits I'd normally point out, but I expect you're still developing this code, so I'll keep quiet for now ;)

• 06-28-2002
datainjector
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;
}
• 06-28-2002
quzah
'5' is not the same as 5.

case '5': /* the character value of 5 */
case 5: /* the decimal value of 5 */

You don't want the character value of 5.
Here is the difference:

printf("5 is %d. '5' is %d.\n", 5, '5' );

Quzah.
• 06-28-2002
Hammer
You've posted exactly the same code again :confused:

Look:
Code:

```float num <snip lots> switch ( num ){ <snip lots more>```
• 06-28-2002
datainjector
yeah guys i will try to complete the code
Thanks for the points hammer and quzah....
• 06-28-2002
datainjector
/*
#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");
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
• 06-28-2002
red_baron
what happend to code tags this time? you umm forgot?
• 06-28-2002
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;

Ok, so 'num1' holds what value at this time? See? It ends up that it may just have some random junk value in it first. You should initialize 'num1' to one, or whatever it is that you want, before you use it.

Another problem may be that your buffer contains junk after your scanf, so your loop doesn't exit right.

Actually, the problem is that your test is not set up correctly. You check for a -1 at the betinning and not the end. This could be improved upon.

Actually... the reason it doesn't break is because you don't set the "breaking value". You need to set the while loop check value to "the break number" inside valid case statements.

ACTUALLY... the best way to do it would be to modify your check, and only set the "don't break" value in the default case.
[/edit]

Quzah.
• 06-29-2002
datainjector
hey quzah .Help me out
Well i declared num a value 1.But i dont still understand why isnt the break stuff working..
/*
#include <stdio.h>

int main()
{
int swt,num1=1, num2=1, num3=1, num4=1, num5=1;
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");
break;
} }

total = awn1+awn2+awn3+awn4+awn5;
printf ("\nThe total for you products is: %d",total);

getch();
return 0;
}*/

well the while loop is working properly .When i enter -1 the sentinel value.The loop does exit and the statment that is not in the loop is executed with is alright..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:

help me out:confused:
• 06-29-2002
Hammer
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.
• 06-29-2002
red_baron
if you havn't yet ready hammer's sig do so now and you'll have 1 less question to worry about.
• 06-29-2002
datainjector

#include <stdio.h>

int main()
{
int cnt,swt,num1, num2, num3, num4, num5;
float awn1, awn2, awn3, awn4, awn5, total;
clrscr();

printf ("Enter the product number: ");
while (( swt = getchar()) != EOF ){

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

} }

total = awn1+awn2+awn3+awn4+awn5;
printf ("\nThe total for you products is: %d",total);

getch();
return 0;
}

Well it still doent work but it does seem improved ..I am stuck on this question..Why dont you right the whole code for me.....
If i know how it really works i will be able to do the other switch questions in my book..
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last