A Simple Problem

Create a user-defined program that will compute the price per square inch of a cake using an “overloading”. The cake could be a circle or a square type. In the main function, ask for the price of a circle cake and its diameter, while for the square cake, ask for the price, its length and width. The process must be done inside the different function of each cake.
(Note: two sub function for the computation of a circle and a rectangle)

Note that when I made this program something weird happen. After inserting the name, all things follow. Any help?

<code>
#include "stdafx.h"
#include <iostream>
using namespace std;

double areaRectangle(int l, int w);
double radiusCircle(int r);
double diameter(int d);
int main()
{
double price;
int d;
int l;
int w;
int r;
char name;
cout << "Welcome to Joie's Pastries and Bakeshop. How may I serve? " << endl;
cout << "Enter the customer's name please. " << endl;
cin >> name;
cout << "Enter the price for the circular cake. " << endl;
cin >> price;

//Recall that the radius is the half of the diameter.
cout << "Enter the diameter of a rounded cake. " << endl;
cin >> d; // Diameter of the cake

//This part focuses on the rectangular cake.

cout << "Enter the price for the recatngular cake. " << endl;
cin >> price;
cout << "Enter the length of the cake. " << endl;
cin >> l;
cout << "Enter the width of the cake. " << endl;
cin >> w;

//The output of the program begins here. This is where the quotient of the price fee and the computed area of
//the circle and rectangle will appear.

cout << "Hello " << name << ", the price of the circluar cake is: " << "Php" << price << endl;
cout << "and the price per square inch is: " << price << "Php" << "." << endl;
cout << "For the rectangular cake, the price is: " << "Php" << price << "." << endl;
cout << "And the price per square inch is: " << "Php" << price << "." << endl;
system("pause");
return 0;
}

//This is the first function. It computes for the area of the circular cake and its price fee.
//This holds the Area of the circle.
double radiusCircle(int r)
{
const double pi = 3.14;
return (pi*r*r);
}

//This is the second function. It computes for the area of the circular cake and its price fee.
//This holds the Area of the Rectangle.
double areaRectangle(int l, int w)
{
return (l * w);
}
//This is the third function. It computes for getting the radius of the circle.
double diameter(int d)
{
return (d / 2);
}</code

you are using a char for your name. try using a string.
Code fix:
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
///#include <stdafx.h>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

double areaRectangle(int l, int w);
double radiusCircle(int r);
double diameter(int d);
const double pi = 3.14159265;
double price,price2;
int d,l,r,w;
string name;
int main()
{
cout << "Welcome to Joie's Pastries and Bakeshop. How may I serve? " << endl;
cout << "Enter the customer's name please. " << endl;
cin >> name;
cout << "Enter the price for the circular cake. " << endl;
cin >> price;

///Recall that the radius is the half of the diameter.
cout << "Enter the diameter of a rounded cake. " << endl;
cin >> d; /// Diameter of the cake

///This part focuses on the rectangular cake.

cout << "Enter the price for the rectangular cake. " << endl;
cin >> price2;
cout << "Enter the length of the cake. " << endl;
cin >> l;
cout << "Enter the width of the cake. " << endl;
cin >> w;

///The output of the program begins here. This is where the quotient of the price fee and the computed area of
///the circle and rectangle will appear.

cout << "Hello " << name << ", the price of the circluar cake is: " << "Php" << price << endl;
cout << "and the price per square inch is: " << price << "Php" << "." << endl;
cout << "For the rectangular cake, the price is: " << "Php" << price2 << "." << endl;
cout << "And the price per square inch is: " << "Php" << price << "." << endl;
system("pause");
return 0;
}

///This is the first function. It computes for the area of the circular cake and its price fee.
///This holds the Area of the circle.
double radiusCircle(int r)
{
return (pi*(r*r));
}

///This is the second function. It computes for the area of the circular cake and its price fee.
///This holds the Area of the Rectangle.
double areaRectangle(int l, int w)
{
return (l * w);
}
///This is the third function. It computes for getting the radius of the circle.
double diameter(int d)
{
return (d / 2);
}
One more thing, it worked but this should be the output as lifted from the example document my professor gave me. It worked but this should look like this:

Hello Mary, the price of a rounded cake is : Php 500
and the price per square inch is: Php 6.36
For the rectangular cake, the price is: 400
And the price per square inch is: Php 9.53

Last edited on
No, it didn't work, you haven't called any of your functions so the program just does input and output, it doesn't do the computation you want.

Some tips:
You need to call diameter(), pass the result to radiusCircle() and divide price by the result.
You also need to call areaRectangle() and divide price2 by the result.
Better not to define your variables globally, define them as late as reasonable, in as small a scope as reasonable.
Last edited on
The instructions call for using overloading. That means same function name, different parameters, like:

1
2
double area (double diameter){ return PI * diameter * diameter / 4 }
float area (double length, double width){ return length * width }


If the cake is round, call area( diameter )
If the cake is rectangular, call area( length, width )

You could also send the price per unit to the function and have it return the cost:

1
2
double price (double diameter, double cost){ return cost * pi * diameter * diameter / 4 }
double price (double length, double width, double cost){ return cost * length * width }


If the cake is round, call price( diameter, cost )
If the cake is rectangular, call price( length, width, cost )
Last edited on
Topic archived. No new replies allowed.