Click to See Complete Forum and Search --> : Need help with code "BEGINNER"


Eric
April 13th, 1999, 09:02 AM
I am trying to write a code that has 3 products called 1,2,3. Product one is worth $1 and Product 2 is
worth $2 and Product 3 is worth $3.
Now I want to be able to ask the user to Enter the day of the week.
And then Enter the Product Number . eg: 1 , 2 or 3
How many sold?
And the results should come out.
But I am having trouble with it.
Could you be kind enough to look at the code and tell me what I need to add to it so it works.
And what is missing or wrong with the one I wrote.
I am having serious problems here. I am new to programming so please explain in the simplest
way you can.
I thank you in advance.
PS: If you compile it , you will see the result i get and what it should be.

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
main()
{
clrscr();
int P1=1,P2=2,P3=3,sum1=0,sum2=0,sum3=0,total=0,Pnumber=0,sold=0,tsold=0;
char day;

cout<<"To end program press crtl + z\n";
cout<<"\nEnter the Day please:\t";
cin>>day;
cout<<"\nEnter Part Number:\t";
cin>>Pnumber;
cout<<"\nEnter how many was Units sold:\t";
cin>>sold;

while(( Pnumber = getchar() ) !=EOF )
{
switch ( Pnumber )
{
case 1:
sum1 = ( P1 * sold );
total += sum1;
tsold = (sold + tsold);
break;

case 2:
sum2 = ( P2 * sold );
total += sum1;
tsold = (sold + tsold);
break;

case 3:
sum3 = ( P3 * sold );
total += sum1;
tsold = (sold + tsold);
break;

default:
cout<<"The Part Numbers are [ 1 , 2 , 3 ] only";
cout<<"\nEnter Part Number:\t";
}
}
cout<<day<<"\n";
cout<<"\n"<< "Partnumber Price Units Sold cost";
cout<<"\n\t1"<<"\t"<<"$1"<<" \t "<<tsold<<"\t\t"<<sum1;
cout<<"\n\t2"<<"\t"<<"$2"<<" \t "<<tsold<<"\t\t"<<sum2;
cout<<"\n\t3"<<"\t"<<"$3"<<" \t "<<tsold<<"\t\t"<<sum3;
cout<<"\n\t\t\t\t"<<" Total "<<total;

getch();
return 0;
}

Franky Braem
April 13th, 1999, 09:11 AM
Use a for-loop to enter the number of products
example:
for(int i = 0; i < 3; i++)
{
cout << "Enter the number of products for " << i;
cin >> pNumber;
}

Eric
April 13th, 1999, 09:22 AM
I read your responce and I think you BUT !!!!!! hehehehe.
I dont really understand what to do. I have 3 products and so I dont understand what you mean.
Could you explain in a little more detail if you have the time and if you dont mind.
OO
\__/ PLEAAAAAAAAAAAASE

Thank You

Dave Lorde
April 13th, 1999, 11:06 AM
It's hard to see how you could write all that code without seeing the obvious errors.

You did write that yourself, didn't you?

Dave

Eric
April 14th, 1999, 05:55 AM
Thanks Dave you been a great help.....
Sorry for not knowing about programming as much as you. I guess you are very intelligent.
I am just 30 hours into programming. But I am not upto your level. Sorry for that!!!!!!!!
OO
\__/ You have a nice day !!!!!!!

Dave Lorde
April 14th, 1999, 06:07 AM
Yeah right Eric; I notice you didn't answer the question...

I did write a message explaining the errors, but I decided not to post it because it looked clear from the code that the person who'd written most of it should be able to figure it out without much difficulty.

If you have a specific question about it, I'd be glad to help, but maybe I'm just getting tired of "fix it for me" posts.

Dave

April 14th, 1999, 07:26 AM
QUESTION ANSWERED: YES I DID WRITE THE CODE ON MY OWN
Look Dave, I sorry for not being as good as you or as smart. Obviously you are very good at what you
do. I am just 30 hours into programming. So I am very very new at it.
If you do not feel upto helping people then dont write anything at all. I like people like you as I get
more determined to learn. But others might get turned off, so it would be better if you dont write
anything.
May be I am a slow learner who makes very simple and obvious mistakes. SO WHAT !!!
At least I am trying. I will learn this stuff SOONER or LATER.
I thank you again for time to read and respond to me.

Franky Braem
April 14th, 1999, 08:15 AM
This is the code you can use for entering the sold units :


#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

void main( void )
{
char p;
cout << "Give the product Number (or press Ctrl-Z for end)" << endl;
while ((p = cin.get()) != EOF)
{
int nSold;
cout << "How many units have you sold of product " << p << "?" << endl;
cin >> nSold;
cin.ignore(); /* Ignore the enter key pressed. Ohterwise the get() in the while loop
will execute and not wait for input.*/
cout << "You sold " << nSold << " units." << endl;

int nProductId = p - '0'; // We have to do this because get returns the ASCII-value of the input

// Do your calculations here :

cout << "Give the product Number (or press Ctrl-Z for end)" << endl;
}
}




Hope this get you started for your project ...

Dave Lorde
April 14th, 1999, 11:00 AM
Hey, lighten up Eric. You may be smarter than me, who knows? I've just been doing it longer.

We get so many requests to do folk's homework assignments, diploma projects, etc., that it's natural to be suspicious of stuff that looks like that. The point being that people have to learn to think things out for themselves or they'll never progress.

In this case it seemed odd that you can put that code together, but miss the fact that unless the prompts and data input are inside the loop, they'll only get done once. You can see that by stepping through the code visually...

Something that might help with this kind of structural problem is not to jump right in and start coding, but to write out the basic program steps (i.e. pseudo-code), and walk through it on paper before writing any code:

1 Get Day
2 Get Part Number
3 Get Units Sold

Loop
4 - Get Part Number
5 - If Finished Exit Loop
6 - Do Calculations with Part Number, Units Sold
LoopEnd

7 Display Results

It's easier to see what's going on - for example, Units Sold is used every time round the loop, but it's only ever entered once...

Dave