GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2008, 08:04
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

How to turn if else statements from math....into functions???


Hello,

I got the program to work as follows: but i need to convert everything to functions.
Here is the program working before I tried converting to functions:

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;

int main ()

{

	double x;
	double y;
	char choice;
	double youranswer; 
	double answer; 
	double z;

do
{
	x = (rand() + time(0)) % 10;


	cout << "The 'random number' is: " ; 
		cout << x << endl;
y = (rand() + time(0)) % 10;


	cout << "The 'random number' is: " ; 
		cout << y << endl;

cout << "Please select one of the following options" << endl;
cout << "A - for addition" << endl;
cout << "D - for division" << endl;
cout << "M - for multiplication" << endl;
cout << "S - for subtraction" << endl;
cout << "Q - to quit the program" << endl;

cin>>choice; 


if (choice == 'A')
{
cout << "Add" << x << "+" << y << endl;
answer = x + y;
cin >> youranswer;
if (youranswer == answer)
cout << "your answer is correct"<< endl;
	else 
		cout << "Incorrect Answer: the correct answer is: " << answer << endl; 

}
else
if (choice == 'S')
{
	if ( x >= y )
	{ 
		answer = x - y;
	cout << "Subtract: " << x << "-" << y << endl;
	}
	else 
	{
		answer = y - x;
	cout << "Subtract: " << y << "-" << x << endl;
	}
	
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
else
if (choice == 'M')
{
	cout << "Multiplicate: " << x << "*" << y << endl;
	answer = x * y;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
else 
if (choice == 'D')
{
	z = x * y;
cout << "Divide: " << z << "/" << x << endl;
	answer = z / x;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
}

while (choice != 'Q'); 
cout << "Goodbye" << endl;

return 0; 


}




and here is my attempt at just converting the add function:

CPP / C++ / C Code:

#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;
double add (double x, double y);

int main ()

{

	double x;
	double y;
	char choice;
	double youranswer; 
	double answer; 
	double z;

do
{
	x = ((rand() + time(0)) % 10) + 1; //add 1 so 0 does not come up 


	cout << "The 'random number' is: " ; 
		cout << x << endl;
y = ((rand() + time(0)) % 10) + 1;


	cout << "The 'random number' is: " ; 
		cout << y << endl;

cout << "Please select one of the following options" << endl;
cout << "A - for addition" << endl;
cout << "D - for division" << endl;
cout << "M - for multiplication" << endl;
cout << "S - for subtraction" << endl;
cout << "Q - to quit the program" << endl;

cin>>choice; 


if (choice == 'A')
{
cout << "Add" << x << "+" << y << add(double x, double y) << endl;
answer = x + y;
cin >> youranswer;


}
else
if (choice == 'S')
{
	if ( x >= y )
	{ 
		answer = x - y;
	cout << "Subtract: " << x << "-" << y << endl;
	}
	else 
	{
		answer = y - x;
	cout << "Subtract: " << y << "-" << x << endl;
	}
	
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
else
if (choice == 'M')
{
	cout << "Multiplicate: " << x << "*" << y << endl;
	answer = x * y;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
else 
if (choice == 'D')
{
	z = x * y;
cout << "Divide: " << z << "/" << x << endl;
	answer = z / x;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
}

while (choice != 'Q'); 
cout << "Goodbye" << endl;

return 0; 


}

double add (double x, double y) 
{

if (youranswer == answer)
return cout << "Your answer is correct"<< endl;
	else 
	return cout << "Incorrect Answer: the correct answer is: " << answer << endl; 
}


any help on just getting the add function to work...thank you
  #2  
Old 12-May-2008, 16:58
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Re: How to turn if else statements from math....into functions???


Hello,

Hey I figured out how to get the functions to work... but for some reason i cant get the division to work when the program runs... it just exits out of the program... i believe it has something to do with unitialized z variable... can someone please help explain this....thank you

here is the code:

CPP / C++ / C Code:

#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;
void add (double x, double y);
void subtraction (double x, double y);
void multiplication (double x, double y);
void division (double x, double y, double z);
double answer;
double youranswer;


int main ()

{

	double x;
	double y;
	char choice;
	double youranswer; 
	double answer; 
	double z;

do
{
	x = ((rand() + time(0)) % 10) + 1; //add 1 so 0 does not come up 


	cout << "The 'random number' is: " ; 
		cout << x << endl;
y = ((rand() + time(0)) % 10) + 1;


	cout << "The 'random number' is: " ; 
		cout << y << endl;

cout << "Please select one of the following options" << endl;
cout << "A - for addition" << endl;
cout << "D - for division" << endl;
cout << "M - for multiplication" << endl;
cout << "S - for subtraction" << endl;
cout << "Q - to quit the program" << endl;

cin>>choice; 


if (choice == 'A')
{
add(x,y);



}
else
if (choice == 'S')
{
	
		subtraction (x,y); 
}
else
if (choice == 'M')
{
	multiplication (x,y);
	
}
else 
if (choice == 'D')
{
	division (x,y,z);
}
}

while (choice != 'Q'); 
cout << "Goodbye" << endl;

return 0; 


}

void add (double x, double y) 
{
answer = x + y;
cout << "Add" << x << "+" << y << endl;
cin>>youranswer; 
if (youranswer == answer)
 cout << "Your answer is correct"<< endl;
	else 
	 cout << "Incorrect Answer: the correct answer is: " << answer << endl; 
}

void subtraction ( double x, double y) 
{ 
	if ( x >= y )
	{ 
		answer = x - y;
	cout << "Subtract: " << x << "-" << y << endl;
	}
	else 
	{
		answer = y - x;
	cout << "Subtract: " << y << "-" << x << endl;
	}
	
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}

void multiplication (double x, double y)
{
	cout << "Multiplicate: " << x << "*" << y << endl;
	answer = x * y;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}
void division (double x, double y, double z)
{
	z = x * y;
cout << "Divide: " << z << "/" << x << endl;
	answer = z / x;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "Your answer is correct!!!" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}

  #3  
Old 12-May-2008, 17:02
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Re: How to turn if else statements from math....into functions???


never mind.... can mods delete this thread??
I figured out that i needed to initialize z outside of the function...
  #4  
Old 13-May-2008, 07:23
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How to turn if else statements from math....into functions???


It may help you to validate your input in the functions. For instance, in the division function, check to make sure that the denominator is not zero.
  #5  
Old 13-May-2008, 08:29
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Re: How to turn if else statements from math....into functions???


Quote:
Originally Posted by fakepoo
It may help you to validate your input in the functions. For instance, in the division function, check to make sure that the denominator is not zero.


Yes, I added 1 to the random function so that 0 does not come up as a variable... Thank you..
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined reference to pow pisuke C Programming Language 6 02-Aug-2007 08:39
Help with some functions baka_yaro CPP / C++ Forum 3 14-Mar-2007 05:56
Need help with if else statements and functions mimi78 CPP / C++ Forum 4 07-Mar-2007 19:22
C math functions tnethaji C Programming Language 1 04-Jul-2005 06:03
Pointers to Functions (was Saving some if statements) TekiFreek CPP / C++ Forum 17 17-Jun-2004 11:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.