Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,286 people online right now. Registration is fast and FREE... Join Now!




Passing Values to Functions

 
Reply to this topicStart new topic

Passing Values to Functions

oat
28 Feb, 2007 - 07:41 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
I have to write this code for my class and I do not understand how to do this.... This is the problem...

I have to pass those functions back up to main with no global variables... I get a lot of errors right now...

PLEASE HELP!

QUOTE

Write a program that calculates the cost of building a desk. The main() function calls four other functions. Pass all variables so that the functions make copies of any variables they receive:

* A function to accept as input from the keyboard the number of drawers in the desk. This function returns the number of drawers to the main program.

* A function to accept as input the type of wood----'m' for mahogany, 'o' for oak, or 'p' for pine.

* A function that receives the drawer number and wood type, and calculates the cost of the desk based on the following: Pine desks are $100, Oak Desks are $140, All other woods are $180, A $30 surcharge is added for each drawer, this function returns the cost to the main() function.

* A function to display the final price.


Here is what I have....

CODE
// Desks.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 6, Exercise 7

#include <stdafx.h>
#include <iostream>

using namespace std;


int main()
{
    double woodCost, drawerCost, calc, usersDrawers(), usersWood(), calculations();
    char woods;
        char woodType();

    drawerCost = usersDrawers();
    woodCost = usersWood();
    woods = woodType();
    calc = calculations();

    cout << "The total of your desk is $" << calc << endl;

}
//Users Enters Number of Drawers
double usersDrawers()
{
    int draw, totalDrawers;

    cout << "How many drawers will you like in your desk? ";
    cin >> draw;

    return totalDrawers;
}

//User Enters Wood Type
int woodType()
{
    char woodType;

    cout << "What type of Wood will your desk be? " << endl;
    cout << "m for mahogony" << endl;
    cout << "o for oak" << endl;
    cout << "p for pine" << endl;
    cin >> woodType;
}
int usersWood()
{

    const int moh = 180;
    const int oak = 140;
    const int pine = 100;
    int wood;

        if (woodType == 'm' || woodType == 'm')
        wood = moh;
        {
    else
        }
        (woodType == 'o' || woodType == 'O');
        wood = oak;
        {
    else
        }
        wood = pine;
    return wood;
}
void calculations(int, char)
{
    int totalDrawers;
        char wood;
    int calc;
        calc = totalDrawers * wood;

        system("Pause");
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Passing Values To Functions
28 Feb, 2007 - 09:42 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
Well I am putting the comments where the change is necessary.
hope you will make it...

CODE

// Desks.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 6, Exercise 7

#include <stdafx.h>
#include <iostream>

using namespace std;


int main()
{
//declare methods outside main
    double woodCost, drawerCost, calc, usersDrawers(), usersWood(), calculations();
    char woods;
        char woodType();
//usersDrawers() supposed to return number of drawers not the cost.
//so use variable name something like drawerNum
    drawerCost = usersDrawers();
//following line is not needed this functionality is supposed to be done
//by calculations function
    woodCost = usersWood();
    woods = woodType();
//send drawerNum and wood to calculations like
//calculations(drawerNum,woods)
    calc = calculations();

    cout << "The total of your desk is $" << calc << endl;

}
//Users Enters Number of Drawers
double usersDrawers()
{
    int draw, totalDrawers;

    cout << "How many drawers will you like in your desk? ";
    cin >> draw;
//taking number in draw then return draw
    return totalDrawers;
}

//User Enters Wood Type
//woodType is of char so it should be char woodType()
int woodType()
{
    char woodType;

    cout << "What type of Wood will your desk be? " << endl;
    cout << "m for mahogony" << endl;
    cout << "o for oak" << endl;
    cout << "p for pine" << endl;
    cin >> woodType;
//return woodType from here
}
//Your assignment says this stuff should be done by one function so
//take this full logic to calculations function. just remove return
int usersWood()
{

    const int moh = 180;
    const int oak = 140;
    const int pine = 100;
    int wood;

        if (woodType == 'm' || woodType == 'm')
        wood = moh;
        {
    else
        }
        (woodType == 'o' || woodType == 'O');
        wood = oak;
        {
    else
        }
        wood = pine;
    return wood;
}
//prototype should have formal parameters names
// like int calculations(int drawNum, char wood)
void calculations(int, char)
{
//first two variables are now not needed. as you will have int wood of
//above function here
    int totalDrawers;
        char wood;
    int calc;
//this should be calc =(drawNum*30)+wood;
        calc = totalDrawers * wood;
//return the calc as assignments says to return it.    

}


try to make those changes and you are there...
and if problem remains then we will meet here again... biggrin.gif

This post has been edited by AmitTheInfinity: 28 Feb, 2007 - 09:44 PM
User is offlineProfile CardPM
+Quote Post

Falke
RE: Passing Values To Functions
28 Feb, 2007 - 10:09 PM
Post #3

New D.I.C Head
*

Joined: 7 Dec, 2006
Posts: 14


My Contributions
I changed a lot of things for just to be able to be able to work with it. Your declarations were confusing and you mixed a lot of ints and double with your functions. I think if you look at it you will see what I changed to get it to work.

Here it is:
CODE

// Wood.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 6, Exercise 7

#include <stdafx.h>
#include <iostream>

using namespace std;


int main()
{
    int usersDrawers();
    int usersWood(char ws);
    double calculations(int Cost, int num);
    char woodType();
    double calc;
    int woodCost;
    char woods;
    int totalnum;

    totalnum = usersDrawers();
    woods = woodType();
    woodCost = usersWood(woods);
    calc = calculations(woodCost, totalnum);

    cout << "The total of your desk is $" << calc << endl;
}
//Users Enters Number of Drawers
int usersDrawers()
{
    int totalDrawers;

    cout << "How many drawers will you like in your desk? ";
    cin >> totalDrawers;

    return totalDrawers;
}

//User Enters Wood Type
char woodType()
{
    char Type;

    cout << "What type of Wood will your desk be? " << endl;
    cout << "m for mahogony" << endl;
    cout << "o for oak" << endl;
    cout << "p for pine" << endl;
    cin >> Type;

    return Type;
}
int usersWood(char ws)
{
    const int moh = 180;
    const int oak = 140;
    const int pine = 100;
    int wood;

    if (ws == 'M' || ws == 'm')
        wood = moh;
    if (ws== 'o' || ws == 'O')
        wood = oak;
    else
        wood = pine;
    
    return wood;
}
double calculations(int Cost, int num)
{
    
    double calc;
    
    calc = Cost * num;

    return calc;

}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Passing Values To Functions
1 Mar, 2007 - 05:40 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
QUOTE(Falke @ 1 Mar, 2007 - 01:09 AM) *

I changed a lot of things for just to be able to be able to work with it. Your declarations were confusing and you mixed a lot of ints and double with your functions. I think if you look at it you will see what I changed to get it to work.

Here it is:
CODE

// Wood.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 6, Exercise 7

#include <stdafx.h>
#include <iostream>

using namespace std;


int main()
{
    int usersDrawers();
    int usersWood(char ws);
    double calculations(int Cost, int num);
    char woodType();
    double calc;
    int woodCost;
    char woods;
    int totalnum;

    totalnum = usersDrawers();
    woods = woodType();
    woodCost = usersWood(woods);
    calc = calculations(woodCost, totalnum);

    cout << "The total of your desk is $" << calc << endl;
}
//Users Enters Number of Drawers
int usersDrawers()
{
    int totalDrawers;

    cout << "How many drawers will you like in your desk? ";
    cin >> totalDrawers;

    return totalDrawers;
}

//User Enters Wood Type
char woodType()
{
    char Type;

    cout << "What type of Wood will your desk be? " << endl;
    cout << "m for mahogony" << endl;
    cout << "o for oak" << endl;
    cout << "p for pine" << endl;
    cin >> Type;

    return Type;
}
int usersWood(char ws)
{
    const int moh = 180;
    const int oak = 140;
    const int pine = 100;
    int wood;

    if (ws == 'M' || ws == 'm')
        wood = moh;
    if (ws== 'o' || ws == 'O')
        wood = oak;
    else
        wood = pine;
    
    return wood;
}
double calculations(int Cost, int num)
{
    
    double calc;
    
    calc = Cost * num;

    return calc;

}


As a matter of structure, the user should be placing the function prototypes outside of the main function, not inside.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month