473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help straightening this noob code

9
I have trouble knowing how to call the function and passing parameters. Can someone help point out where things need to go? It's a basic program to figure out the cost per inch of pizza of either a round(diameter) pizza or square one.

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. char pizzaType;
  4. double getData(int length, int width, int price);
  5. double getData(int diamter, int price);
  6. double computeUnitCost(int diameter, int price);
  7. double computeUnitCost(int length, int width, int price);
  8.  
  9. int main()
  10. {
  11.         bool okay = true;
  12.         do {
  13.         cout << "Please enter in the type of pizza: " << endl;
  14.         cout << "r - a round pizza" << endl;
  15.         cout << "s - a square pizza" << endl;
  16.         cin >> pizzaType;
  17.  
  18.         cin.ignore(80, '\n');
  19.  
  20.         okay = (pizzaType == 'r' || pizzaType == 's');
  21.  
  22.             cout << "\nYou entered an invalid pizza type!" << endl;
  23.         }while(!okay);
  24.         if (pizzaType == 'r')
  25.  
  26.         double roundPrice = getData;
  27.         cout << "The price per square inch for this pizza is " << roundPrice << endl;
  28.     return 0;    
  29. }
  30.  
  31. double getData(int& length, int& width, int& price)
  32.     {
  33.  
  34.         cout << "Enter in the length of the pizza: " << endl;
  35.         cin >> length;
  36.         cout << "Enter in the width of the pizza: " << endl;
  37.         cin >> width;
  38.         cout << "Enter in the price of the pizza: " << endl;
  39.         cin >> price;
  40.  
  41.     }
  42. double getData(int& diameter, int& price)
  43.     {
  44.         cout << "Enter in the diameter of the pizza: " << endl;
  45.         cin >> diameter;
  46.         cout << "Enter in the the price of the pizza: " << endl;
  47.         cin >> price;
  48.     }
  49.  
  50. double computeUnitCost(int diameter, int price)
  51.     {
  52.         int roundCost = diameter / price;
  53.     return roundCost;
  54.     cout << "The price per square inch for this pizza is: " << roundCost << endl;
  55. }
  56. double computeUnitCost(int length, int width, int price)
  57.     {
  58.         squareCost = ((lenth * width))/ price;
  59.     return squareCost;
  60.     cout << "The price per square inch for this pizza is: " << squareCost << endl;
  61.     }
Oct 16 '06 #1
5 1664
Banfa
9,065 Expert Mod 8TB
Comments in bold

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. char pizzaType;
  4. /* The 2 declarations do not match there function definitions below */
  5. double getData(int length, int width, int price);
  6. double getData(int diamter, int price);
  7.  
  8. /* Currently you don't call these functions anywhere. */
  9. double computeUnitCost(int diameter, int price);
  10. double computeUnitCost(int length, int width, int price);
  11.  
  12. int main()
  13. {
  14.         bool okay = true;
  15.         do {
  16.         cout << "Please enter in the type of pizza: " << endl;
  17.         cout << "r - a round pizza" << endl;
  18.         cout << "s - a square pizza" << endl;
  19.         cin >> pizzaType;
  20.  
  21.         cin.ignore(80, '\n');
  22.  
  23.         okay = (pizzaType == 'r' || pizzaType == 's');
  24.  
  25.             cout << "\nYou entered an invalid pizza type!" << endl;
  26.         }while(!okay);
  27.         if (pizzaType == 'r')
  28. /* referencing getData like this just gives you a function pointer.
  29.    You need to call the function by calling it as getData(diameter, price);
  30.    Obviously you will need to declare the variables diameter and price first.*/
  31.         double roundPrice = getData;
  32. /* You don't have any handling here for the square type pizza. */
  33.         cout << "The price per square inch for this pizza is " << roundPrice << endl;
  34.     return 0;    
  35. }
  36.  
  37. double getData(int& length, int& width, int& price)
  38.     {
  39. /* You should indicate what units you need the input in.
  40.    "Enter in the length of the pizza in inches: " */
  41.         cout << "Enter in the length of the pizza: " << endl;
  42.         cin >> length;
  43.         cout << "Enter in the width of the pizza: " << endl;
  44.         cin >> width;
  45.         cout << "Enter in the price of the pizza: " << endl;
  46.         cin >> price;
  47.  
  48. /* I think you probably should have called and returned the value 
  49.    return value of computeUnitCostSize here.  What ever you do
  50.    this function is defined as returning a double and so must return 
  51.    a value*/
  52.     }
  53. double getData(int& diameter, int& price)
  54.     {
  55. /* You should indicate what units you need the input in.
  56.    "Enter in the length of the pizza in inches: " */
  57.         cout << "Enter in the diameter of the pizza: " << endl;
  58.         cin >> diameter;
  59.         cout << "Enter in the the price of the pizza: " << endl;
  60.         cin >> price;
  61.  
  62. /* I think you probably should have called and returned the value 
  63.    return value of computeUnitCostSize here.  What ever you do
  64.    this function is defined as returning a double and so must return 
  65.    a value*/
  66.     }
  67.  
  68. double computeUnitCost(int diameter, int price)
  69.     {
  70. /* The price per square inch of a pizza is not the area / cost, it is
  71.    the cost / area.  The diameter of a circle is not it's area, the area 
  72.    of a circle is PI * radius squared. */
  73.         int roundCost = diameter / price;
  74.     return roundCost;
  75. /* You return before this cout statement so it is never executed */
  76.     cout << "The price per square inch for this pizza is: " << roundCost << endl;
  77. }
  78. double computeUnitCost(int length, int width, int price)
  79.     {
  80. /* The price per square inch of a pizza is not the area / cost, it is
  81.    the cost / area.  Area / Cost gives square inches per $. */
  82.         squareCost = ((lenth * width))/ price;
  83.     return squareCost;
  84. /* You return before this cout statement so it is never executed */
  85.     cout << "The price per square inch for this pizza is: " << squareCost << endl;
  86.     }
Oct 16 '06 #2
matrim
9
Thanks for the help so far! My real problem is knowing how to call and pass parameters correctly. Are the other functions right?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char pizzaType;
  6. void getData(int length, int width, int price);
  7. void getData(int diamter, int price);
  8. double computeUnitCost(int diameter, int price);
  9. double computeUnitCost(int length, int width, int price);
  10.  
  11. int main()
  12. {
  13.         bool okay = true;
  14.         do {
  15.         cout << "Please enter in the type of pizza: " << endl;
  16.         cout << "r - a round pizza" << endl;
  17.         cout << "s - a square pizza" << endl;
  18.         cin >> pizzaType;
  19.  
  20.         cin.ignore(80, '\n');
  21.  
  22.         okay = (pizzaType == 'r' || pizzaType == 's');
  23.  
  24.             cout << "\nYou entered an invalid pizza type!" << endl;
  25.         }while(!okay);
  26.         if (pizzaType == 'r')
  27.  
  28.         int roundPrice = getData(int& diameter, int& price);
  29.         cout << "The price per square inch for this pizza is " << roundPrice << endl;
  30.     //return 0;
  31.         if (pizzaType == 's')
  32.  
  33.         int squarePrice = getData(int& diameter, int& price);
  34.         cout << "The price per square inch for this pizza is " << squarePrice << endl;
  35. }
  36.  
  37. void getData(int& length, int& width, int& price)
  38.     {
  39.  
  40.         cout << "Enter in the length of the pizza: " << endl;
  41.         cin >> length;
  42.         cout << "Enter in the width of the pizza: " << endl;
  43.         cin >> width;
  44.         cout << "Enter in the price of the pizza: " << endl;
  45.         cin >> price;
  46.  
  47.     }
  48. void getData(int& diameter, int& price)
  49.     {
  50.         cout << "Enter in the diameter of the pizza in: " << endl;
  51.         cin >> diameter;
  52.         cout << "Enter in the the price of the pizza in: " << endl;
  53.         cin >> price;
  54.     }
  55.  
  56. double computeUnitCost(int diameter, int price)
  57.     {
  58.         int roundCost = diameter / price;
  59.     cout << "The price per square inch for this pizza is: " << roundCost << endl;
  60.     return roundCost; // not sure if i need to return this or if its the right way
  61. }
  62.  
  63. double computeUnitCost(int length, int width, int price)
  64.     {
  65.         int squareCost = ((length * width))/ price;
  66.     cout << "The price per square inch for this pizza is: " << squareCost << endl;
  67.         return squareCost; // not sure if this is the right way to return it
  68.  
  69.     }
  70.  
Oct 17 '06 #3
Banfa
9,065 Expert Mod 8TB
Function Parameters

function parameters can be passed either by value or by reference, in C++ if passing by reference you can use either references or pointers, in C only pointers are available.

To pass data to a function the data has to exist, here is a simple example that I hope will help

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. static int fnPassByValue(int x);
  6. static int fnPassByReferencePointer(int *x);
  7. static int fnPassByReferenceReference(int &x);
  8.  
  9.  
  10. int main()
  11. {
  12.     int x = 0;
  13.     int old;
  14.  
  15.     cout << "main                      : Initial value x = " << x << endl;
  16.  
  17.     old = fnPassByValue(x);
  18.  
  19.     cout << "main                      : After calling fnPassByValue x = " << x << endl;
  20.     cout << "main                      :   it's previous value was " << old << endl;
  21.  
  22.     old = fnPassByReferencePointer(&x);
  23.  
  24.     cout << "main                      : After calling fnPassByReferencePointer x = " << x << endl;
  25.     cout << "main                      :   it's previous value was " << old << endl;
  26.  
  27.     old = fnPassByReferenceReference(x);
  28.  
  29.     cout << "main                      : After calling fnPassByReferenceReference x = " << x << endl;
  30.     cout << "main                      :   it's previous value was " << old << endl;
  31.  
  32.     return 0;
  33. }
  34.  
  35. int fnPassByValue(int x)
  36. {
  37.     int old = x;
  38.  
  39.     cout << "fnPassByValue             : Parameter Input " << x << endl;
  40.  
  41.     x += 10;
  42.  
  43.     cout << "fnPassByValue             : Parameter Now " << x << endl;
  44.  
  45.     return old;
  46. }
  47.  
  48. int fnPassByReferencePointer(int *x)
  49. {
  50.     int old = *x;
  51.  
  52.     cout << "fnPassByReferencePointer  : Parameter Input " << *x << endl;
  53.  
  54.     *x += 100;
  55.  
  56.     cout << "fnPassByReferencePointer  : Parameter Now " << *x << endl;
  57.  
  58.     return old;
  59. }
  60.  
  61. int fnPassByReferenceReference(int &x)
  62. {
  63.     int old = x;
  64.  
  65.     cout << "fnPassByReferenceReference: Parameter Input " << x << endl;
  66.  
  67.     x += 1000;
  68.  
  69.     cout << "fnPassByReferenceReference: Parameter Now " << x << endl;
  70.  
  71.     return old;
  72. }
  73.  
Oct 17 '06 #4
matrim
9
Thanks for that. However, we haven't even gone through pointers yet, so I need a way to do that referencing without having pointers in it? :)

Here's what I need:

Your program should include

1. A function named getData( )that gets all of the user input for a round pizza. For a round pizza it will ask for the diameter of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global variables! The return type of the function is void.
2. Another function named getData( )that gets all of the user input for a square pizza. For a square pizza it will ask for the length and width of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global data. The return type of the function is void.
3. A function named computeUnitCost( ) that takes the diameter of a round pizza and its price as parameters, and returns the cost of 1 square inch of that pizza.
4. Another function named computeUnitCost( ) that takes the length and width of a rectangular pizza and its price, and returns the cost of 1 square inch of that pizza. Note that the function computeUnitCost( ) is overloaded.

And what I have coded so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void getData(int& length, int& width, int& price)
  7. void getData(int& diameter, int& price);
  8. double computeUnitCost(int diameter, int price);
  9. double computeUnitCost(int length, int width, int price);
  10.  
  11. int main()
  12. {
  13.         char pizzaType;
  14.         bool okay = true;
  15.         do {
  16.         cout << "Please enter in the type of pizza: " << endl;
  17.         cout << "r - a round pizza" << endl;
  18.         cout << "s - a square pizza" << endl;
  19.         cin >> pizzaType;
  20.  
  21.         cin.ignore(80, '\n');
  22.  
  23.         okay = (pizzaType == 'r' || pizzaType == 's');
  24.  
  25.             cout << "\nYou entered an invalid pizza type!" << endl;
  26.         }while(!okay);
  27.         int price = 0;
  28.  
  29.         if (pizzaType == 'r')
  30.         {
  31.         int diameter = 0;    
  32.         getData(diameter, price);
  33.         cout << "The price per square inch for this pizza is " << computeUnitCost(diameter, price) << endl;
  34.         }
  35.         if (pizzaType == 's')
  36.         {
  37.         int length = 0;
  38.         int width = 0;
  39.  
  40.         getData(length, width, price);
  41.         cout << "The price per square inch for this pizza is " << computeUnitCost(length, width, price) << endl;
  42.         }
  43.         int length = 0;
  44.         int width = 0;
  45.  
  46.         computeUnitCost(length, width, price);
  47.  
  48.         system("PAUSE");
  49. }
  50.  
  51. void getData(int& length, int& width, int& price)
  52.     {
  53.  
  54.         cout << "Enter in the length of the pizza: " << endl;
  55.         cin >> length;
  56.         cout << "Enter in the width of the pizza: " << endl;
  57.         cin >> width;
  58.         cout << "Enter in the price of the pizza: " << endl;
  59.         cin >> price;
  60.     }
  61.  
  62. void getData(int& diameter, int& price)
  63.     {
  64.         cout << "Enter in the diameter of the pizza in: " << endl;
  65.         cin >> diameter;
  66.         cout << "Enter in the the price of the pizza in: " << endl;
  67.         cin >> price;
  68.     }
  69.  
  70. double computeUnitCost(int diameter, int price)
  71.     {
  72.         int roundCost = diameter / price;
  73.     cout << "The price per square inch for this pizza is: " << roundCost << endl;
  74.     return roundCost; // not sure if i need to return this or if its the right way
  75. }
  76.  
  77. double computeUnitCost(int length, int width, int price)
  78.     {
  79.         int squareCost = ((length * width))/ price;
  80.     cout << "The price per square inch for this pizza is: " << squareCost << endl;
  81.         return squareCost; // not sure if this is the right way to return it
  82.  
  83.     }
Oct 18 '06 #5
Banfa
9,065 Expert Mod 8TB
And that code has all the basic elements needed. You still have not corrected the maths in the computeUnitCost functions that I pointed out originally.

Please ask a specific question or problem about this code, what are you stuck on?
Oct 18 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: administrata | last post by:
Is it possible? I tried... I = "John" print \ """ I used to love pizza"""
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
7
by: Craig Parsons | last post by:
Folks, I have a scanned image which is circular, and contains a trace in the middle of it (for those of you familar with trucks it is a tachograph chart!). I want to "flatten" it out, into a...
0
by: trev85852 | last post by:
Im a bit of a VB noob but hey we all gotta start as a noob right? I need a VB script for Outlook that reads in a file and then changes the ntfs file attribute "Title" to the same as the file name....
2
by: Link360 | last post by:
Im a complete noob and im proud of it. I am excited in learning everything about the C++ language. Right now im trying to make tic-tac-toe. Go ahead laugh. here is what i have so far ...
5
by: Hydrogenfussion | last post by:
Hello and thank you for reading this. I have just started to learn C++ from www.cprogramming.com,its a great site! But i do not understand some of the terms. I am using Dev-C++. Can you tell me...
3
by: gham | last post by:
I am a complete noob to linux and shell scripting please help this is what I am trying to do 1. Create a script that takes 1 argument being a file, read the inputted file, and look for...
19
by: Ganesh J. Acharya | last post by:
Hi there, I want to redesign my website and make that look professional. I made this about 6 years ago with very little knowledge of internet. Today I am getting about 4000 visitors a day for...
2
by: pinman | last post by:
hi. i'm pretty much a noob to c# and visual studio and am trying to create a simple login method. i have created a users table in the database and can add users by inputing there md5 encrypted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.