Fraction Calculator - Cin issue

I am writing a fraction calculator for class and I almost have it completed. However, I am running into an error where my program will not allow me to enter in the 2nd cin of my 'getFractions' function. So, the computer inputs it's own input and then when I try to continue to the main menu again, it enters into an infinite loop.

I have been searching for around 2-3 hrs online to try and find a solution and have used many different solutions but none of them have worked.

This all started when I was trying to figure out how to stop the cin from reading any characters after my 'int' entry. Before I started to change the code to stop it, my code worked just fine. But after I realized that using .clear messed up my code, my original code no longer worked.

Please let me know if you have any ideas.

Ideas I have tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
-cin.clear()
-if (!cin) 
	     {
                  cin.clear();
		  cin.ignore(256, '\n'); 
	     }
- if (!cin) 
	     {
                  cin.clear();
		  cin.ignore(numeric_limits<streamsize>::max(),' ');
	     }
(when i use the ignore, it does not recognize what I have entered in the 2nd cin.)
- changed int choice to char choice & tried to use chars for the switches.



function I am having issues with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//user defined function to get the fractions
void getFractions(int& numOne, int& denomOne, int& numTwo, int& denomTwo)
{
	int d; // dummy variable for '/' in fraction entered by user.

	cout << "Fractions should be entered to include the '/'." << endl << endl
		<< "Enter the first fraction: ";
	cin >> numOne >> d >> denomOne;
	cin.clear();
	cout << "Enter the second fraction: ";
	cin >> numTwo >> d >> denomTwo;
	cin.clear();
	cout << endl;
}



entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>

using namespace std;

void functionMenu(); // displays main menu
void getFractions(int& numOne, int& denomOne, int& numTwo, int& denomTwo); // gets fraction inputs from user
void addFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // adds fractions
void subtractFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // subtracts fractions
void multiplyFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // multiplies fractions
void divideFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // divides fractions
void reduceFractions (int& numAns, int& denomAns); // reduces fractions
void displayResult(int choice, int numOne, int denomOne, int numTwo, int denomTwo, int numAns, int denomAns); // displays results of each task

int main()
{
	//Declarations
	int choice; // menu choice

	//variables for fractions entered by user
	int numOne;
	int denomOne;
	int numTwo;
	int denomTwo;

	//variables for answers to fraction calculations
	int numAns = 0;
	int denomAns = 0;

	do
	{
		system("cls");
		functionMenu();
		cin >> choice;
		cin.clear();

		cout << endl << endl;

		switch (choice)
		{
		case 1: // adds two fractions
			system("cls");
			getFractions(numOne, denomOne, numTwo, denomTwo);
			addFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			system("pause");
			break;
		case 2: // subtracts two fractions
			system("cls");
			getFractions(numOne, denomOne, numTwo, denomTwo);
			subtractFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			system("pause");
			break;
		case 3: // multiplies two fractions
			system("cls");
			getFractions(numOne, denomOne, numTwo, denomTwo);
			multiplyFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			system("pause");
			break;
		case 4: // divides two fractions
			system("cls");
			getFractions(numOne, denomOne, numTwo, denomTwo);
			divideFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
			system("pause");
			break;
		case 5: // exits program
			return 1;
		default: // the default result that is displayed if a user enters a number that does not match any of the cases
			cout << "Invalid input, please press any key to try again." << endl << endl;
			system("pause");
			break;
		}
	}
	while (choice != 5);

	system("pause");
	return 0;
}

//user definied function to show the menu screen
void functionMenu() 
{
	cout << "This program allows users to perform arithmetic operations on fractions." << endl
		<< "Fractions are in the form of a/b, and a and b must be integers." << endl
		<< "b should not equal 0." << endl << endl
		<< "Enter the menu number of the operation you would like to perform." << endl << endl
		<< "Menu: " << endl
		<< "\t1.\tAddition (two fractions)" << endl
		<< "\t2.\tSubtraction (two fractions)" << endl
		<< "\t3.\tMultiply (two fractions)" << endl
		<< "\t4.\tDivide (two fractions)" << endl
		<< "\t5.\tExit" << endl << endl
		<< "Your choice: ";
}

//user defined function to get the fractions
void getFractions(int& numOne, int& denomOne, int& numTwo, int& denomTwo)
{
	int d; // dummy variable for '/' in fraction entered by user.

	cout << "Fractions should be entered to include the '/'." << endl << endl
		<< "Enter the first fraction: ";
	cin >> numOne >> d >> denomOne;
	cin.clear();
	cout << "Enter the second fraction: ";
	cin >> numTwo >> d >> denomTwo;
	cin.clear();
	cout << endl;
}

//user defined function to add two fractions
void addFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
	numAns = (numOne * denomTwo) + (denomOne * numTwo);
	denomAns = denomOne * denomTwo;
	reduceFractions(numAns, denomAns);
}

//user defined function to subtract two fractions
void subtractFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
	numAns = (numOne * denomTwo) - (denomOne * numTwo);
	denomAns = denomOne * denomTwo;
	reduceFractions(numAns, denomAns);
}

//user defined function to multiply two fractions
void multiplyFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
	numAns = numOne * numTwo;
	denomAns = denomOne * denomTwo;
	reduceFractions(numAns, denomAns);
}

//user defined function to divide two fractions
void divideFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
	numAns = numOne * denomTwo;
	denomAns = denomOne * numTwo;
	reduceFractions(numAns, denomAns);
}

 //user defined function to reduce fractions. this was not included in the homework, but i wanted to add it to 
// clean up the fractions.
void reduceFractions (int& numAns, int& denomAns)
{
	for (int i = denomAns * numAns; i > 1; i--)
		if ((denomAns % i == 0) && (numAns % i == 0))
		{
			denomAns /= i;
			numAns /= i;
		}
}

//user defined function to display results based on the calculation chosen by user
void displayResult(int choice, int numOne, int denomOne, int numTwo, int denomTwo, int numAns, int denomAns)
{
	switch (choice)
	{
	case 1: // display results if addition
		cout << numOne << "/" << denomOne << " + " << numTwo << "/" << denomTwo
			<< " = " << numAns << "/" << denomAns << endl << endl;
		break;
	case 2: // display results if subtractions
		cout << numOne << "/" << denomOne << " - " << numTwo << "/" << denomTwo
			<< " = " << numAns << "/" << denomAns << endl << endl; 
		break;
	case 3: // dipslay results if multiplication
		cout << numOne << "/" << denomOne << " * " << numTwo << "/" << denomTwo
			<< " = " << numAns << "/" << denomAns << endl << endl;
		break;
	case 4: // dipsplay results if division
		cout << numOne << "/" << denomOne << " / " << numTwo << "/" << denomTwo
			<< " = " << numAns << "/" << denomAns << endl << endl;
		break;
	}
}
Last edited on
function I am having issues with:

Well d really should be a char if you want to try to retrieve an operator (+-/*).

Something more like:
1
2
3
4
5
6
7
8
9
//user defined function to get the fractions
void getFraction(int& numOne, int& denomOne)
{
	char d; // dummy variable for '/' in fraction entered by user.

	cout << "Fractions should be entered to include the '/'." << endl << endl
		<< "Enter the first fraction: ";
	cin >> numOne >> d >> denomOne;
}


Note I also recommend just getting one fraction at a time, if you need multiple fractions then call the function twice with different variables.


I am not trying to retrieve an operator.

I split the getFractions up into two user Def functions.
Still doing the same thing.

The computer enters in it's own values for the 2nd fraction, then when I press a key to continue back to the main menu, it actually has my default at the bottom of the main menu for me to enter a new choice.

Edit:
Well I feel stupid, especially with what I said before this. I've had a long day. You're my hero jlb.
Last edited on
Topic archived. No new replies allowed.