Hey guys I overall got this problem solved it has to allow the user to input numbers to do artithmitic functions to fractions. And the whole mathmatical works fine. But the professor wants it to loop, and i cant figure out a good way to loop it. The way the functions are. Any good ideas?

#include <iostream> 
#include <string> 
#include <iomanip>
using namespace std; 
void variable(int& a, int& b, int& c, int& d); 
void addFrac(int&,int&,int&,int&,int&,int&);
void subFrac(int&,int&,int&,int&,int&,int&);
void divFrac(int&,int&,int&,int&,int&,int&);
void multiplyFrac(int&,int&,int&,int&,int&,int&);
void menu();

int main()
{

menu();

}


void variable(int& a, int&  b, int& c, int& d)
{

	cout << "Please enter in the first fraction numerator" << endl;
	cin >> a;
	cout << "Please enter in the first fraction denomator" << endl;
	cin >> b;
	cout << "Please enter in the second fraction numerator " << endl; 
	cin >> c;
	cout << "Please enter in the second fraction denomator" << endl;
	cin >> d; 
}


void addFrac(int& a, int& b, int& c, int& d, int& num,int& den)
{
	

	
num= (a*d + b*c);
den= (b*d); 



}

void subFrac(int& a, int& b, int& c, int& d, int& num,int& den)
{
		

	num=((a*d)-(b*c));
	den=(b*d);



}

void divFrac(int& a, int& b, int& c, int& d, int& num,int& den)
{
	num= (a*d);
    den = (b*c); 





}

void multiplyFrac(int& a, int& b, int& c, int& d,int& num,int& den)
{
	
	

	num=a*c;
	den=b*d;




}

void menu()	
{
int a;
int b;
int c;
int d; 
int num;
int den;	
int choice;
cout << "The Fraction Program" <<  endl;
cout << "Please Choose from the following possible mathmatical operations" << endl;
cout << " 1 : Will add two fractions together" << endl;
cout << " 2 : Will subtract two fractions" << endl; 
cout << " 3 : Will divide two fractions" << endl;
cout << " 4 : Will multiply two fractions" << endl; 
cin >> choice;
	switch (choice)
{
case 1 :

     variable(a,b,c,d);
	addFrac(a,b,c,d,num,den);
	 break;
case 2 :

	variable(a,b,c,d);
	subFrac(a,b,c,d,num,den);
	break;

case 3:
    variable(a,b,c,d);
	divFrac(a,b,c,d,num,den);
	break;

case 4: 
	variable(a,b,c,d);
	multiplyFrac(a,b,c,d,num,den);
	break;

default:
	cout << "Sorry thats an invalid choice" << endl;
}


	
}

Dani AI

Generated

Solid follow-up to — adding an explicit "exit" choice (as suggested) and wrapping the menu in an outer loop (as hinted) is the right structural fix. A few additional, concrete improvements turn the program from "works once" into robust, maintainable code:

  • Prefer small, single-purpose functions that return a result instead of mutating many reference parameters (return a Fraction object or a pair<int,int> for the result). This avoids uninitialized num/den problems.
  • Validate input immediately (check for bad stream state and zero denominators) and normalize results (move any negative sign to the numerator and reduce by the GCD).
  • Print the computed result inside the loop so each operation actually shows output to the user.
  • Add simple error handling for divide-by-zero (when dividing by a zero-valued fraction) and consider integer overflow for big inputs (use long long if needed).

A compact redesign pattern (illustrative) — Fraction type, operations return a Fraction, main loop with an exit sentinel, and automatic reduction:

#include <iostream>
#include <numeric>
#include <stdexcept>

struct Fraction {
  int n=0, d=1;
  Fraction(int nn=0,int dd=1):n(nn),d(dd){
    if(dd==0) throw std::runtime_error("zero denominator");
    if(d<0){ n=-n; d=-d; }
    int g = std::gcd(std::abs(n), std::abs(d));
    if(g){ n/=g; d/=g; }
  }
};
Fraction add(const Fraction& a,const Fraction& b){ return Fraction(a.n*b.d + b.n*a.d, a.d*b.d); }
// sub, mul, div similar; div should check b.n!=0

int main(){
  while(true){
    std::cout<<"0:Exit 1:Add 2:Sub 3:Mul 4:Div > ";
    int ch; if(!(std::cin>>ch)) break; if(ch==0) break;
    Fraction A,B; std::cin>>A.n>>A.d>>B.n>>B.d;
    try{ Fraction R = (ch==1? add(A,B) : /* ... */ add(A,B)); std::cout<<R.n<<'/'<<R.d<<"\n"; }
    catch(const std::exception& e){ std::cout<<"Error: "<<e.what()<<"\n"; }
  }
}

Quick checklist for robustness: loop + exit option, validate denominators and input, reduce/normalize results, print after each operation, and test edge cases (0 denominators, zero numerator, negatives, large values).

Recommended Answers

All 3 Replies

A couple suggestions (hint: they're all interrelated):
1. Add a bool variable (perhaps called "quit").
2. Convert menu() to return something other than void
3. Add a switch case
4. Figure out how to keep calling menu() as long as the user wants to

Those should start you in the right direction, the rest is up to you.

Remember, whenever you want a program to run again, without closing after its done, the majority of the program's structure must be contained within an outer loop. (This does not mean flooding main() with statements better placed elsewhere in the program.)

Add another choice to menu() that lets the user exit the program, and place the whole function into an appropriate loop. The loop should check to see that the 'exit' choice has/hasn't been selected.

Add another choice to menu() that lets the user exit the program, and place the whole function into an appropriate loop. The loop should check to see that the 'exit' choice has/hasn't been selected.

I went with your idea of adding an extra choice in the menu function, it was so simple cant beleive i didnt think of it X_X. Just hope my maths right..although not to sure, teacher gave us the formulas, hope shes right knowing her though she gave us the wrong ones XD
Thanks agian guys.

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.