Click to See Complete Forum and Search --> : Please Help Me with this code


April 18th, 1999, 05:45 AM
I am having some trouble with this code that I am trying to complete.
I know I should be adding a loop or something somewhere but I dont
know where. Also I am very new at programming and it is my first
time using switches. But it is not working ( I think ).
Could you be kind enough to guide me in the right direction. It would
be a great help to me.
I thank you in advance.

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
main()
{
clrscr();
int day,Pnumber=0,Usold=0,count1=0,count2=0,count3=0,P1=0,P2=0,P3=0;

cout<<"\nPress ctrl + z to end program.";
cout<<"\n\nThe day of the week should be in Numbers.";
cout<<"\neg: Monday = 1, Tuesday = 2 and so on.";

cout<<"\n\nEnter the Day of the Week:"; // Day of week is a number
cin>>day; // eg: Monday = 1, Tuesday = 2 and so on
cout<<"\nEnter the Part Number:";
cin>>Pnumber; // Part numbers are 1 , 2 or 3 only
cout<<"\nEnter Units Sold:";
cin>>Usold; // How many Units were sold

while (( Pnumber = getchar() ) !=EOF )
{

switch (Pnumber) //P1,P2,P3 will hold the subtotal
{ //in the end for the output
case 1:
int P1 = 0;
P1 = (Pnumber * 1);
int count1 = 0; //count1,2,3 will hold the output
count1 = (Usold + count1); //for Units sold
break;

case 2:
int P2 = 0;
P2 = (Pnumber * 2);
int count2 = 0;
count2 = (P2 + count2);
break;

case 3:
int P3 = 0;
P3 = (Pnumber * 3);
int count3 = 0;
count3 = (P3 + count3);
break;

default:
cout<<"\n\nWrong Part Number Used";
cout<<"\nPlease use only Part Numbers 1 or 2 or 3";
break;
}
}
if ( day == 1 )
cout<<"Monday";
if ( day == 2 )
cout<<"Tuesday";
if ( day == 3 )
cout<<"Wednesday";
if ( day == 4 )
cout<<"Thursday";
if ( day == 5 )
cout<<"Friday";
if ( day == 6 )
cout<<"Saturday";
if ( day == 7 )
cout<<"Sunday";

cout<<"\nPart Number Unit Price Units Sold Sub Total";
cout<<"\n1"<<"\t\t$1"<<"\t\t"<<count1<<"\t"<<"\t"<<P1;
cout<<"\n2"<<"\t\t$2"<<"\t\t"<<count2<<"\t"<<"\t"<<P2;
cout<<"\n3"<<"\t\t$3"<<"\t\t"<<count3<<"\t"<<"\t"<<P3;
getch();
return 0;
}

April 18th, 1999, 08:57 AM
change case 1 , case 2 ,,,,
to case '1' case '2' ,,,

April 18th, 1999, 09:39 AM
I did what you said but it still does not work. The compiler says there is no errors. But when I run it
it does not work. It asks me for the day and then the part number and then the Units sold but then
it says that I entered the wrong part number ?????????
But the part number is correct. I suspect there is an error of logic in the code somewhere but i cant find
it. Could you be kind enough to tell me where if you can find it. Obviously something is wroong but
where.????????????????////

Thank you in advance

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
main()
{
clrscr();
int day,Pnumber=0,Usold=0,P1=0,P2=0,P3=0;

cout<<"\nPress ctrl + z to end program.";
cout<<"\n\nThe day of the week should be in Numbers.";
cout<<"\neg: Monday = 1, Tuesday = 2 and so on.";

cout<<"\n\nEnter the Day of the Week:"; // Day of week is a number
cin>>day; // eg: Monday = 1, Tuesday = 2 and so on

do {

cout<<"\nEnter the Part Number:"; // Part numbers are 1 , 2 or 3 only
cin>>Pnumber;
cout<<"\nEnter Units Sold:"; // How many Units were sold
cin>>Usold;

switch (Pnumber) //P1,P2,P3 will hold the subtotal
{ //in the end for the output
case '1':
P1 = (Pnumber * 1); //count1,2,3 will hold the output
int Usold1 = (Usold + Usold1); //for Units sold
break;

case '2':
P2 = (Pnumber * 2);
int Usold2 = (Usold + Usold2);
break;

case '3':
P3 = (Pnumber * 3);
int Usold3 = (Usold + Usold3);
break;

default:
cout<<"\n\nWrong Part Number Used";
cout<<"\nPlease use only Part Numbers 1 , 2 or 3";
break;
}
} while (( Pnumber = getchar() ) !=EOF );

if ( day == 1 )
cout<<"\nMonday";
if ( day == 2 )
cout<<"\nTuesday";
if ( day == 3 )
cout<<"\nWednesday";
if ( day == 4 )
cout<<"\nThursday";
if ( day == 5 )
cout<<"\nFriday";
if ( day == 6 )
cout<<"\nSaturday";
if ( day == 7 )
cout<<"\nSunday";

cout<<"\nPart Number Unit Price Units Sold Sub Total";
cout<<"\n1"<<"\t\t$1"<<"\t\t"<<Usold1<<"\t"<<"\t"<<P1;
cout<<"\n2"<<"\t\t$2"<<"\t\t"<<Usold2<<"\t"<<"\t"<<P2;
cout<<"\n3"<<"\t\t$3"<<"\t\t"<<Usold3<<"\t"<<"\t"<<P3;
getch();
return 0;
}

April 19th, 1999, 11:10 PM
It seems to me that you're using wrong switch statement, which always go to default case displaying "Wrong Part Number Used". Since you declare Pnumber as integer, int Pnumber=0, the switch statement should be:

...
switch (Pnumber)
{
case 1:
....
case 2:
...
default:
...
}
...

Hopefully, it'll help.

Cheers

gradyc
April 20th, 1999, 03:47 PM
The reason you keep going to the default in the switch is the while loop

while (( Pnumber = getchar() ) !=EOF )

Pnumber is reset (Pnumber = getchar()) after the initial entry after the prompt. (cin >> Pnumber)

Why do you redeclare each of the variables within each case of the switch statement. They will go out of scope when you exit the swtich statement and the wrong values will be output at the end of the program.

Note that you will only be prompted once for what to enter. You may want to move the prompting to within the loop.

Hope this helps.

Chuck Grady

ValerieB
April 20th, 1999, 04:33 PM
You are redeclaring your variables within the case statement, which is wrong. Those varaibles will go out of scope as soon as you leave the switch statement, which means that you will lose any data that the user entered.

You need to remove the varaible declarations within your switch statement. In other words, you need to change this:

switch (Pnumber) //P1,P2,P3 will hold the subtotal
{ //in the end for the output
case 1:
int P1 = 0;
P1 = (Pnumber * 1);
int count1 = 0; //count1,2,3 will hold the output
count1 = (Usold + count1); //for Units sold
break;
}

to look like this:

switch (Pnumber) //P1,P2,P3 will hold the subtotal
{ //in the end for the output
case 1:
P1 = (Pnumber * 1);
count1 = (Usold + count1); //for Units sold
break;
}

You must do this for all cases in your switch statement. Good luck!

Valerie Bradley
http://www.synthcom.com/~val
val@synthcom.com

Allen Y
April 20th, 1999, 04:51 PM
I don't think your program is compiled with no errors. Because the variables Usold1, Usold2 and Usold3 are declared in the switch statement and never be initialized. Try to declare these variables out of switch statement and initialize these variables.

Allen