Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2006, 2:32 AM   #1
j0nathan
Newbie
 
j0nathan's Avatar
 
Join Date: Mar 2006
Location: Tj, Mx
Posts: 7
Rep Power: 0 j0nathan is on a distinguished road
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!!!
j0nathan is offline   Reply With Quote
Old Apr 5th, 2006, 3:44 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
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";
You have an extra cout in there, which is putting out the value of cout in hex.
Change this to either
	cout << areaOfPizza <<
	" square inches, then at " << price<< " cents per square\n";
or
	cout << areaOfPizza;
	cout << " square inches, then at " << price<< " cents per square\n";
this happens several time in the code.

2. This calculation:
double getAreaOfPizza( double P_I, int size )
{
	return (P_I*(size/2)*(size/2));
}
will perform the size/2 as integer calculations first (because of the brackets), this will lead to a wrong result when you use an odd number.
Change this to
double getAreaOfPizza( double P_I, int size )
{
	return (P_I*size/2*size/2);
}
or
double getAreaOfPizza( double P_I, int size )
{
	return (P_I*(size/2.0)*(size/2.0));
}
As a side note, I wouldn't expect an area calulation to have PI passed in as a variable. It aint gonna change much, so you could probably have it as a global constant or a local constant inside the area function.
The Dark is offline   Reply With Quote
Old Apr 5th, 2006, 3:44 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Apr 5th, 2006, 3:50 AM   #4
j0nathan
Newbie
 
j0nathan's Avatar
 
Join Date: Mar 2006
Location: Tj, Mx
Posts: 7
Rep Power: 0 j0nathan is on a distinguished road
Thanks guys I really apreciate it.
j0nathan is offline   Reply With Quote
Old Apr 5th, 2006, 3:53 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC