![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Location: Tj, Mx
Posts: 7
Rep Power: 0
![]() |
double functions (getting weird numbers)
Hi, I was wondering if someone could check my C++ code to see if can figure out why when I run my program I get sme weird numbers(3.123445ADD. I know that something is wrong with my functions but can't figure out what is it, I would really appreciate any help, here is what I have.
#include <iostream>
#include <string> //Needed for string variables
#include <windows.h> //To have Colors
#include <cmath> //Needed for ceil() & floor()
#include <iomanip> //Needed for setw()
using namespace std;
void welcome();
//A message to greet user
void hitEnter();
//Prompt user to hit ENTER key in order to continue
void logo();
//My logo JB will display to id my work
string getUserName();
//Prompt user for name and return the name
void order();
void echoInfo( string userName );
void obtainSizeAndPrice( string userName, int& size, int& price );
void echoInfo( string userName, int size, int price );
double getAreaOfPizza( double P_I, int size );
double calculatePizzaPrice( double areaOfPizza, int price );
double round( double pizzaCost );
void niceFormat();
int main()
{
//User defined function calls
welcome();
logo();
order();
return(0);
}
/**********************************************************
***********************************************************
FUNCTION DECLARATIONS
***********************************************************
**********************************************************/
/**********************************************************
***********************************************************
hitEnter prompt user to hit enter in order to continue
***********************************************************
**********************************************************/
void hitEnter()
{
cout << "\n\n\t\t\t*** Hit ENTER to continue ****\n";
cin.ignore();
return;
}
/**********************************************************
***********************************************************
Order
***********************************************************
**********************************************************/
void order()
{
const double P_I = 3.14159;
string userName;
int size,
price;
double areaOfPizza,
pizzaCost,
roundFloor,
roundPizza;
userName = getUserName();
echoInfo ( userName );
obtainSizeAndPrice( userName, size, price );
echoInfo( userName, size, price );
cout << "\n\n\nSince a " <<size << "-inch pizza covers ";
areaOfPizza = getAreaOfPizza( P_I, size );
cout << areaOfPizza <<
cout << " square inches, then at " << price<< " cents per square\n";
cout << "inch, the cost is $";
niceFormat();
pizzaCost = calculatePizzaPrice( areaOfPizza, price );
cout << pizzaCost<<
cout << ". This rounds to $";
roundPizza = round( pizzaCost );
cout << roundPizza<<
cout <<"\n\n";
cin.ignore();
}
/**********************************************************
***********************************************************
GetUserName ask for username and stores it
***********************************************************
**********************************************************/
string getUserName()
{
string name;
cout << "\n\nPlease enter your first name and hit ENTER: ";
cin >> name;
return name;
}
/**********************************************************
***********************************************************
echoInfo returns the name of user.
***********************************************************
**********************************************************/
void echoInfo( string userName )
{
cout << "\n\n\t Ah, " <<userName << ", so nice to have you joining us";
cout << " today for some pizza!!\n\n\n";
hitEnter();
cin.ignore();
system( "cls" );
return;
}
/**********************************************************
***********************************************************
obtainSizeAndPrice ask user for size and price
**********************************************************
**********************************************************/
void obtainSizeAndPrice( string userName, int& size, int& price )
{
cout <<"\n\n\n "<<userName << ", please enter the pizza size (5 - 25 inches in diameter),";
cout << " and price \n";
cout << "of ingredients (10 - 15 cents per square inch of pizza), like 5 10: ";
cin >> size >> price;
return;
}
/**********************************************************
***********************************************************
echoInfo display info given by user
***********************************************************
**********************************************************/
void echoInfo( string userName, int size, int price )
{
cout << "\n\n\nThanks, " <<userName << ", for selecting a " <<size << "-inch pizza at ";
cout <<price << " cents per square inch.\n";
hitEnter();
cin.ignore();
system( "cls" );
return;
}
void niceFormat()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
return;
}
double getAreaOfPizza( double P_I, int size )
{
return (P_I*(size/2)*(size/2));
}
double calculatePizzaPrice( double areaOfPizza, int price )
{
return ((areaOfPizza*price)/100);
}
double round( double pizzaCost )
{
return (floor(pizzaCost + 0.5));
}What's wrong ?? I have been here for a couple of hours and nothing. help please!!! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Two problems I can see:
1. The one that is causing your output problem is this code: cout << areaOfPizza << cout << " square inches, then at " << price<< " cents per square\n"; Change this to either cout << areaOfPizza << " square inches, then at " << price<< " cents per square\n"; cout << areaOfPizza; cout << " square inches, then at " << price<< " cents per square\n"; 2. This calculation: double getAreaOfPizza( double P_I, int size )
{
return (P_I*(size/2)*(size/2));
}Change this to double getAreaOfPizza( double P_I, int size )
{
return (P_I*size/2*size/2);
}double getAreaOfPizza( double P_I, int size )
{
return (P_I*(size/2.0)*(size/2.0));
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Works fine for me, you are not using your variable roundfloor btw
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
void hitEnter();
void order();
string getUserName();
void echoInfo( string userName );
void obtainSizeAndPrice( string userName, int& size, int& price );
void echoInfo( string userName, int size, int price );
void niceFormat();
double getAreaOfPizza( double P_I, int size );
double calculatePizzaPrice( double areaOfPizza, int price );
double round( double pizzaCost );
int main()
{
order();
return(0);
}
void hitEnter()
{
cout << "\n\n\t\t\t*** Hit ENTER to continue ****\n";
cin.ignore();
return;
}
void order()
{
const double P_I = 3.14159;
string userName;
int size,
price;
double areaOfPizza,
pizzaCost,
roundFloor,
roundPizza;
userName = getUserName();
echoInfo ( userName );
obtainSizeAndPrice( userName, size, price );
echoInfo( userName, size, price );
cout << "\n\n\nSince a " <<size << "-inch pizza covers ";
areaOfPizza = getAreaOfPizza( P_I, size );
cout << areaOfPizza << " square inches, then at "
<< price << " cents per square\n" << "inch, the cost is $";
niceFormat();
pizzaCost = calculatePizzaPrice( areaOfPizza, price );
cout << pizzaCost << ". This rounds to $";
roundPizza = round( pizzaCost );
cout << roundPizza << "\n" << endl;
cin.ignore();
}
string getUserName()
{
string name;
cout << "\n\nPlease enter your first name and hit ENTER: ";
cin >> name;
return name;
}
void echoInfo( string userName )
{
cout << "\n\n\t Ah, " <<userName << ", so nice to have you joining us";
cout << " today for some pizza!!\n\n\n";
hitEnter();
cin.ignore();
system( "cls" );
return;
}
void obtainSizeAndPrice( string userName, int& size, int& price )
{
cout <<"\n\n\n"<<userName << ", please enter the pizza size (5 - 25 inches in diameter), and price \n" of ingredients (10 - 15 cents per square inch of pizza), like 5 10: ";
cin >> size >> price;
return;
}
void echoInfo( string userName, int size, int price )
{
cout << "\n\n\nThanks, " <<userName << ", for selecting a " <<size << "-inch pizza at ";
cout <<price << " cents per square inch.\n";
hitEnter();
cin.ignore();
system( "cls" );
return;
}
void niceFormat()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
return;
}
double getAreaOfPizza( double P_I, int size )
{
return (P_I*(size/2.0)*(size/2.0));
}
double calculatePizzaPrice( double areaOfPizza, int price )
{
return ((areaOfPizza*price)/100.0);
}
double round( double pizzaCost )
{
return (floor(pizzaCost + 0.5));
}EDIT: Nevermind, The Dark answered your question, I automatically removed the couts without having a look at it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Location: Tj, Mx
Posts: 7
Rep Power: 0
![]() |
Thanks guys I really apreciate it.
![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
My suggestion would be to calculate all the values and only after that print them to screen. Not printing something then calculate then printing again. This will clear up what you are doing.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|