Hello there! So I have created a project for school. I have this program running without errors. The problem is it doesn't perform the math. Any ideas or suggestions? Here is the output when ran...and the code as well.

Please choose your operator :
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
Operation to perform: +

Enter the numerator for the first fraction: 2
Enter the denominator for the first fraction: 4
Enter the numerator for the second fraction: 1
Enter the denominator for the second fraction: 4

Would you like to go again?

//	********************
	#include <stdafx.h>
	#include "iostream"
	#include <cmath>
//	********************
	using namespace std;
//	*******************

//	*******************
//	Function Prototypes
//	********
	void addFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den);
	void subtractFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den);
	void multiplyFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den);
	void divideFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den);
	void menu(char operationType);
//	**End**Funtion**Prototypes****
//	******************************

//	*********************
//	Variable Declarations
//	********
	int n1 = 0,
		d1 = 0,
		n2 = 0,
		d2 = 0;
	int num;
	int	den;

	char operationType;
	char answer;
	bool finished = true;
//	*********************

//	***********
	int main( )
	{


//		*************
//		User Loop
//		****
		do
		{
		menu(operationType);
			
		cout << "Enter the numerator for the first fraction: ";
		cin >> n1;

		cout << "Enter the denominator for the first fraction: ";
		cin >> d1;

		cout << "Enter the numerator for the second fraction: ";
		cin >> n2;

		cout << "Enter the denominator for the second fraction: ";
		cin >> d2;

	switch (operationType){

	case '+': 
		addFractions(n1,d1,n2,d2,num,den);
		cout << "\nThe answer is " << num << "/" << den << endl;
		break;

	case '-':
		subtractFractions(n1,d1,n2,d2,num,den);
		cout << "\nThe answer is " << num << "/" << den << endl;
		break;

	case '*':
		multiplyFractions(n1,d1,n2,d2,num,den);
		cout << "\nThe answer is " << num << "/" << den << endl;
		break;

	case '/':
		divideFractions(n1,d1,n2,d2,num,den);
		cout << "\nThe answer is " << num << "/" << den << endl;
		break;
	}
			
  
//		***********************
//		Obtain wishes from user
//		********
		cout << endl;
		cout << "Would you like to go again?" << endl;
		cin >> answer;
		if (answer == 'n')
			finished = true;
		else (answer == 'y');
			cout << "Welcome Back!" << endl;

		} while (!finished);//userLoop
//		******************************

	return 0;
	}// Function main
//	*****************



//	*************
//	Menu Function
//	****
	void menu(char operationType) 
	{
	cout << endl;
	cout << "Please choose your operator :"<< endl;
    cout << "		+ (addition)\n"
		 << "		- (subtraction)\n"
		 << "		* (multiplication)\n"
		 << "		/ (division)\n"
		 << "Operation to perform: ";
	
    cin >> operationType;
    cout << endl;
	}//**endFunction**Menu**
//	************************

//	************
//	Add Function
//	***
	void addFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den)
	{
		num = (n1*d2 + d1*n2);
		den = (d1*d2);
	}//**end**Function**Add**
//	*************************

//	*****************
//	Subtract Function
//	********
	void subtractFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den)
	{
		num = ((n1*d2) - (d1*n2));
		den = (d1*d2);
}//**end**Function**Subtract**
//	**************************

//	*****************
//	Multiply Function
//	******** 
	void multiplyFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den)
	{	
		num = (n1*n2);
		den = (d1*d2);
	}//**end**Function**Multiply**
//	******************************

//	*****************
//	Division Function
//	********
	void divideFractions(int& n1, int& d1, int& n2,int& d2, int& num, int& den)
	{
		num = (n1*d2);
		den = (n2*d1);
	}//**end**Function**Divide**
//	****************************

Dani AI

Generated

Short diagnosis and concrete fixes based on the code and the replies from and :

The immediate reason your program "does nothing" is that menu is using a copy of operationType. The value you read inside menu never gets back into main, so the switch in main sees an uninitialized/unchanged char and none of the cases run. is right to suggest making the parameter a reference; is also correct that returning a char from menu is a cleaner design. Either fix will make the operator chosen in the menu visible to main.

Other things to check while you fix that:

  • Make sure the functions that produce the result actually write it back to main. If your add/subtract/multiply/divide functions accept num and den by value, the caller will not see the computed values. Either pass num and den by reference, or return a pair/struct containing numerator and denominator.
  • Verify that your function prototypes match their definitions exactly. A mismatch can compile in some cases but produce unexpected behavior.
  • Fix the loop/answer logic. The current else (answer == 'y'); line is a no-op and the following cout will run unconditionally. Use explicit assignments or braces so finished is set correctly (for example set finished = (answer == 'n')).

Polish and safety checks to add:

  • Validate denominators (do not allow zero).
  • Normalize and reduce the result (compute gcd and divide numerator/denominator; std::gcd is available in C++17).
  • Keep denominator positive (move sign to numerator) and consider using a small struct Fraction { int num, den; } to make passing/returning cleaner.

After making the menu and result-return fixes, re-run with simple inputs and compile with warnings enabled (e.g., -Wall or MSVC warning level) to catch uninitialized-variable and logic issues quickly.

Recommended Answers

All 3 Replies

You need to declare the operationType in menu as a reference.
86 the asterisks.
Remove one tab from the beginning of every line.

Ok will give it a try. The format is as my teacher likes. He uses = instead of asterisks. But I like them. Sorry if it's an eye sore to you!

If menu() returns a value, then return the value. It should be a char , not void , function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.