There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
black screen blue screen boot computer connection crash css dell display driver drivers error excel firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem problems ram router slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Help me write this noob code better


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
matrim's Avatar
matrim matrim is offline   matrim has a birthday soon!
Senior Member with 195 posts.
 
Join Date: Dec 2004
Experience: Little above average
17-Oct-2006, 01:06 AM #1
Help me write this noob code better
Hi,
I need to write a n00b program that calculates the best deal customers can get when buying pizza. The premise is, although an extra large pizza may appear a better buy, you need to calculate the cost per sq inch of both round and rectangular pizzas to really know.

What I need to do:
-use function overloading
-write functions that pass by value and by reference
-use the SAME named function twice(for a total of 4 functions), call them getData and computeUnitCost

The first function gets all user input for round pizza, and must store the values it gets from the user where they can be accessed by other parts of the program(no global variables)length, width, price.

The 2nd function gets all user input for square pizza and the rest of the info like the first function.


The 3rd and 4th functions are computeUnitCost and do the computations. One takes the diameter and price as parameters, and returns the cost of 1 sq in of pizza. The other take the length and price and returns the cost. The function should be overloaded.

I'm not sure how to call it correctly or how to pass the parameters. Please help?
Here's what I have so far:



Code:
#include <iostream>

using namespace std;

char pizzaType;
void getData(int length, int width, int price);
void getData(int diameter, int price);
double computeUnitCost(int diameter, int price);
double computeUnitCost(int length, int width, int price);

int main()
{
		bool okay = true;
		do {
		cout << "Please enter in the type of pizza: " << endl;
		cout << "r - a round pizza" << endl;
		cout << "s - a square pizza" << endl;
		cin >> pizzaType;
		
		cin.ignore(80, '\n');
		
		okay = (pizzaType == 'r' || pizzaType == 's');
		
			cout << "\nYou entered an invalid pizza type!" << endl;
		}while(!okay);
		if (pizzaType == 'r')
		int diameter;
		int price;
		void getData();
		cout << "The price per square inch for this pizza is " << getData(diameter, price) << endl;
	//return 0;
		if (pizzaType == 's')
			
		int squarePrice = getData(int& length, int& width, int& price);
		cout << "The price per square inch for this pizza is " << getData << endl;
}

void getData(int& length, int& width, int& price)
	{
		
		cout << "Enter in the length of the pizza: " << endl;
		cin >> length;
		cout << "Enter in the width of the pizza: " << endl;
		cin >> width;
		cout << "Enter in the price of the pizza: " << endl;
		cin >> price;
		double computeUnitCost(int&, int&);
	}
void getData(int& diameter, int& price)
	{
		cout << "Enter in the diameter of the pizza in: " << endl;
		cin >> diameter;
		cout << "Enter in the the price of the pizza in: " << endl;
		cin >> price;
	}

double computeUnitCost(int diameter, int price)
	{
		int roundCost = diameter / price;
	cout << "The price per square inch for this pizza is: " << roundCost << endl;
	return roundCost; // not sure if i need to return this or if its the right way
}

double computeUnitCost(int length, int width, int price)
	{
		int squareCost = ((length * width))/ price;
	cout << "The price per square inch for this pizza is: " << squareCost << endl;
		return squareCost; // not sure if this is the right way to return it

	}
__________________
Matrim
Windows Vista HP
256mb nvidia 7600 GT
3.0 Gig DDR2 RAM
Gigabyte Mobo
260 Gb Western Digital, 300 Gb Maxtor
AMD 4000+ Dual Core
GAMES: Battlefield 2 & BF 2142, Half LIfe 2, F.E.A.R, Anarchy Online, CS:S
www.nutrafitness.com/phpBB2
MUDThunderdome.nutrafitness.com
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
17-Oct-2006, 08:22 AM #2
Your code indicates a lack of understanding about using pointers.

Pointers are straight forward. The first thing you need to understand is the difference between an address or reference to a variable (a pointer) and the variable itself.

A pointer is not the same thing as a variable. It references the area of memory used to store the data of the variable. Here is a tutorial on pointers.

Two simple rules with pointers:
To use or declare a pointer as a variable you need to dereference it using *
To pass a variable as a pointer you need to reference it using &

Your function prototypes need to reflect you are passing pointers. Here are the correct prototypes for your problem:

Code:
void getData(int* diameter, double* price);
void getData(int* length, int* width, double* price);
double computeUnitCost(int diameter, double price);
double computeUnitCost(int length, int width, double price);
You pass pointers using the address of operator. Here is an example:

Code:
    double price = 0.0;

    if (pizzaType == 'r')
    {
        int diameter = 0;
        getData(&diameter, &price); // use the address of operator & to pass a var as an address (pointer)
        cout << "The price per square inch for this pizza is " << computeUnitCost(diameter, price) << endl;
    }
When you use pointers you need to dereference them using *

Code:
void getData(int* diameter, double* price)
{
	cout << "Enter in the diameter of the pizza in: " << endl;
	cin >> *diameter; // since these are pointers coming into the function
                      // you need to dereference them with *
	cout << "Enter in the the price of the pizza in: " << endl;
	cin >> *price;
}

Finally, make sure you calc the area of a circle correctly

Code:
#define PI 3.1416

double computeUnitCost(int diameter, double price)
{
    // area of a circle piR^2
    double radius = (double)diameter/2;
    cout << "Total square inches =" << (PI * radius * radius) << endl;
    // divide price by the total sq inches to get price / sq inch
    return (price / (PI * radius * radius));
}
Best,
Dan

Last edited by dquigley : 17-Oct-2006 09:16 AM.
matrim's Avatar
matrim matrim is offline   matrim has a birthday soon!
Senior Member with 195 posts.
 
Join Date: Dec 2004
Experience: Little above average
17-Oct-2006, 01:13 PM #3
Hi Dan,

Thanks for the help! HOwever, we haven't covered pointers yet! So I need to do this problem without pointers. But, yes I need to pass the function by value and reference. How do I do that without pointers?
redivivus's Avatar
Senior Member with 1,458 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
17-Oct-2006, 01:21 PM #4
string myFunction(string& ReferenceToMyString, int& ReferenceToMyInt)

...

whatever=myFunction(MyString,MyInt);

Should work...

When calling my reference it uses the actual variables rather than a copy, so the originals might be changed.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:23 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.