|
-
October 26th, 2008, 11:12 PM
#1
Please Help
This is the first programming class I have ever taken, the teacher is horrible, and its only one night a week. I've done good so far, but I don't have a clue what I'm doing with this newest program. And I'm pretty embarrassed. I've re-written and deleted this code for hours, trying to make sense of the book, but I'm still lost. The main funtion is supposed to call four other functions, and the whole point is to display the total cost. Any help would be great, I'm hopeless at this point (as you will see):
// "Desks"
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numDrawers();
char woodtype();
int cost(numDrawers);
getch();
}
int numDrawers()
{
int numDrawers;
cout<<"Enter the number of drawers in the desk ";
cin>>numDrawers;
int cost(numDrawers);
}
char woodtype()
{
char woodtype;
cout<<"Enter p for pine, o for oak, or m for mahogany ";
cin>>woodtype;
return (woodtype);
}
int cost(numDrawers)
{
int cost;
if(woodtype == 'p)
cost = (numDrawers * 30) + 100;
cout<<cost<<endl;
else
if(woodtype == 'o')
cost = (numDrawers * 30) + 140<<;
cout<<cost<<endl;
else
if(woodtype == 'm')
cost = (numDrawers * 30) + 180<<;
cout<<cost<<endl;
else
cout<<"Error"<<endl;
return cost;
}
-
October 27th, 2008, 02:35 AM
#2
Re: Please Help
Please read this before you post. Also you can edit your post according to that.
http://www.codeguru.com/forum/announ...nouncementid=6
In function main()
you can't use that kind of syntax. If you wan't a return value. you must declare the variable type. And you are acctaully trying to declare function type;
Try using this:
Code:
int main()
{
int retNumDrawers = 0;
char retWoodType;
retNumDrawers = retCost = 0;
retWoodType = numDrawers();
char = woodtype();
retNumDrawers = cost(numDrawers);
getch();
}
And you never declared what type/scope of variables are numDrawers or else. And you also didn't predefine functions. in this case the function prototype of function cost should look like this.
Code:
int cost(int ndrawers, char wdType)
{
...
}
And then try prinnting the values.
Also you should really try reading a book on C++ before acctaully trying to program. This is thought in every book in first chapter.
You can't code by learning on forums.
Last edited by Odiee; October 27th, 2008 at 02:47 AM.
You just divided by zero, didn't you?
-
October 27th, 2008, 03:02 AM
#3
Re: Please Help
I changed some things in your code, and I think it is what your trying to do.
You should talk to your teacher, because the mistakes you were making demonstrate your lack of understanding of programming principles.
Here is your amended code:
Code:
// "Desks"
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int numDrawers()
{
int number;
cout<<"Enter the number of drawers in the desk ";
cin>>number;
return number;
}
char woodtype()
{
char type;
cout<<"Enter p for pine, o for oak, or m for mahogany ";
cin>>type;
return type;
}
void cost(int nd, char wt)
{
int cost;
if(wt == 'p')
{
cost = (nd * 30) + 100;
cout<<cost<<endl;
}
else if(wt == 'o')
{
cost = ((nd * 30) + 140);
cout<<cost<<endl;
}
else if(wt == 'm')
{
cost = ((nd * 30) + 180);
cout<<cost<<endl;
}
else
{
cout<<"Error"<<endl;
}
}
int main() /* note: main is AFTER the other modules, so they don't have to be foreward-declared */
{
int drawers; /* this is where we are going to keep track of how many drawers */
char wood; /* same here for the type of wood */
drawers = numDrawers(); /* here, drawers is being assigned the return value of the function numDrawers */
wood = woodtype(); /* ditto here for the type of wood */
cost(drawers, wood); /* were sending both pieces of information to cost() where cost() will decide what to do with that information */
return 0;
}
-
October 27th, 2008, 03:05 AM
#4
Re: Please Help
You just divided by zero, didn't you?
-
October 27th, 2008, 01:09 PM
#5
Re: Please Help
Thank you so much for your help everyone, I already understand it SO much better. I appologize for posting an assignment, I did feel pretty guilty about it... I have tried talking to the professor, I'm not sure he even understands what he's doing. I've talked to my advisor, who is also the head of the program at the university. Most likely the professor I have will not be with the university much longer, or at least not teaching programming. This is something I really am interested in and want to move forward with, but this class has definitely not been helpful.
-
October 27th, 2008, 01:26 PM
#6
Re: Please Help
 Originally Posted by Odiee
DEFINATELY (posts such as the one given actually cause more harm than good)....
And while you are at it, you should properly set up pyour profile, and enable PM so that things like this can be said in private.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 27th, 2008, 01:39 PM
#7
Re: Please Help
 Originally Posted by Odiee
Try using this:
Code:
int main()
{
int retNumDrawers = 0;
char retWoodType;
retNumDrawers = retCost = 0;
retWoodType = numDrawers();
char = woodtype();
retNumDrawers = cost(numDrawers);
getch();
}
And then try prinnting the values.
Also you should really try reading a book on C++ before acctaully trying to program. This is thought in every book in first chapter.
You can't code by learning on forums.
Wow.
Code:
int main()
{
int retNumDrawers = 0;
char retWoodType;
retNumDrawers = retCost = 0; // What is retCost. You've initializied retNumDrawers twice
retWoodType = numDrawers();//a char variable Type is getting what appears to be a count
char = woodtype(); // You didn't declare a variable here
retNumDrawers = cost(numDrawers); // numDrawers was initialized to zero and not changed. The cost function you provided takes two arguments
getch();
}
Last edited by GCDEF; October 27th, 2008 at 01:42 PM.
-
October 27th, 2008, 01:55 PM
#8
Re: Please Help
 Originally Posted by GCDEF
Wow.
Code:
int main()
{
int retNumDrawers = 0;
char retWoodType;
retNumDrawers = retCost = 0; // What is retCost. You've initializied retNumDrawers twice
retWoodType = numDrawers();//a char variable Type is getting what appears to be a count
char = woodtype(); // You didn't declare a variable here
retNumDrawers = cost(numDrawers); // numDrawers was initialized to zero and not changed. The cost function you provided takes two arguments
getch();
}
WOW.
I lold. I can't believe i actually made worst mistakes then OP.
I guess you really can't sleep 4 hours and then go to work.
I apologize for the lulz.
I my defense I was awake 'till 3AM, and went to sleep , but at that half-sleep I thought I made the clock adjustments (but didn't. I just set the alarm clock) and woke up hour eary. and still thought that I'm late for work, but I actually came first.
Last edited by Odiee; October 27th, 2008 at 02:00 PM.
You just divided by zero, didn't you?
-
October 27th, 2008, 01:57 PM
#9
Re: Please Help
 Originally Posted by Odiee
WOW.
I lold. I can't believe i actually made worst mistakes then OP.
I guess you really can't sleep 4 hours and then go to work.
I apologize for the lulz.
I my defense I was awake 'till 3AM, and went to sleep , but at that half-sleep I thought I made the clock adjustments (but didn't. I just set the alarm clock) and woke up hour eary. and still thought that I'm late for work, but I actually came first.
Happens to all of us, but probably didn't help the OP any.
-
October 27th, 2008, 03:49 PM
#10
Re: Please Help
I really am interested in learning all of this, and I thought I was doing ok, but for some reason this completely lost me. I've been able to write all the other programs up to this point. I did spend alot of time trying different things with this one. If this topic needs to be closed or removed, I completely understand. And again I appologize, and thank you for all of the advice and understanding.
Last edited by countyroad; October 27th, 2008 at 03:56 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|