I mixed my functions up and can't figure out how to swap them

I need to switch the work from main () to the userspec() function but it is going horribly. Can anyone guide me in the right direction? This is due today and I spent a week trying to figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//KA. 10.1.2013
#include <iostream>
#include <iomanip>
using namespace std;

void userSpec (int, int, double shipAndHand = 10.0);
void userSpec (int orderChoice, int stockLeft, double shipAndHand)
{
int shippingNow, subtoteNow, backorder = 0;
char specialShipAndHand;
double shipAndHandNow, toteNow;
{
	cout  <<"        Welcome to the Middletown Wholesale Copper Wire Company program! \n";
	cout << "==============================================================================\n";
    cout << "\nEnter number of spools to order:  ";
    cin >> orderChoice;
    cout << "Enter number of spools in stock:  ";
    cin >> stockLeft;
    cout << "\nAny special shipping and handling charge? Enter '1' = Yes | '2' = No:  ";
    cin >> specialShipAndHand;

while (specialShipAndHand != '1' && specialShipAndHand != '2') 
{	
    cout << "\nPlease enter '1' = Yes | '2' = No: ";
    cin >> specialShipAndHand;}

if (specialShipAndHand == '1')  
{
    cout << "\nEnter the special charges per spool ";
    cin >> shipAndHand;
} else shipAndHand = 10.0;
}

if (orderChoice <= stockLeft)
{
    shippingNow = orderChoice;
}

else
{
    shippingNow = stockLeft;
    backorder = orderChoice - stockLeft;
}

    subtoteNow = shippingNow * 100;
    shipAndHandNow = shippingNow * shipAndHand;
    toteNow = subtoteNow + shipAndHandNow;
	cout << "\n==============================================================================\n";
    cout << left << setw(25)<< "\nSpools shipping from in-stock:" << fixed<< setw(7) << right << shippingNow;
    cout << left << setw(25)<< "\nSpools in backorder: " << fixed<< setw(13) << right << backorder;
    cout << left << setw(25)<< "\n\nSubtotal of spools shipping: "<< setprecision(2) << fixed<< setw(8) << right << "$" << subtoteNow;
    cout << left << setw(25)<< "\nTotal shipping on spools shipping: " << setprecision(2) << fixed<< setw(2) << right << "$" << shipAndHandNow;
    cout << left << setw(25)<< "\nTotal of the order shipping: " << setprecision(2) << fixed<< setw(8) << right << "$" << toteNow << endl;
	cout << "\n==============================================================================\n\n\n";
}
//----------------------------------------------------------------------

int main()

{
int orderChoice = 0, stockLeft = 0;
double shipAndHand = 0.0;
userSpec (orderChoice, stockLeft, shipAndHand);

system("pause");
return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

Could you please be a little bit more specific about the problems you encounter? Does the code not compile, are you getting errors when the program is running, if so, which errors do you get, etc.

On a sidenote:

1
2
void userSpec (int, int, double shipAndHand = 10.0);
void userSpec (int orderChoice, int stockLeft, double shipAndHand = 10.0)


When your function is defined before main(), you don't need to declare it an extra time, the definition suffices.

All the best,
NwN
Hey, thank you for responding. The program runs perfectly but I wrote everything in the userspec function that should actually be under the main function. So I want to move the code around and make it correctly written.

I am struggling with learning functions, can't quite grasp them yet.

Thank you! :)
closed account (o3hC5Di1)
Hi there,

Why would you wan to move it into main()?

The idea behind functions is mostly to be able to reuse certain bits of code easily. They also give you small entities which have one specific task. This makes code more clear, readable and more modular.

In your program, you could, for instance have following functions (I've exaggerated a little bit to make the point clear):

print_welcome();
get_spools_to_order(int& orderchoice);
get_spools_in_stock(int& stockleft);
get_shipping(int& shipandhand);
process_order(); //pass necessary variables for these two too of course
print_result();

More information about functions is available here:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Hope that helps.

All the best,
NwN

Topic archived. No new replies allowed.