Help with variable storage and scope (and other beginner things)

I need to write a program that will ask the user if they want to convert a fraction to a decimal, add, or multiple two fractions. i am supposed to create one file with the main file that has all the cout's and stuff, and another file with just the functions to add, multiply, and convert. i am a little confused as to where to go from here, any help? Ill put both files.

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
  #include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{ double num1;
  double num2;
  char answer;
  char choice = 'y';
	
	
  cout <<"Enter a code for the desired operation"<<endl<<endl;
  cout <<" a to add two fractions"<<endl;
  cout <<" c to convert a fraction to a decimal"<<endl;
  cout <<" m to multiply the two fractions"<<endl;
  cin>>answer;

  switch (answer)
	  {case 'a':
		cout<<"Please input two fractions to be added"<<endl<<endl;
		cin>>num1>>num2;

		cout<<setw(8)<<fixed<<setprecision(2)<< "\nThe sum of the fractions entered is " <<num1+num2<<endl<<endl;
		break;

	 case 'c':
		 cout<<"Please input a fraction to be converted to a decimal"<<endl<<endl;
		 cin>>num1;

		 cout<<setw(8)<<fixed<<setprecision(2)<< "\nThe decimal value of  "<<endl<<endl;
		 break;

	 case 'm':
		 cout<<"Please input two fractions to be multiplied"<<endl<<endl;
		 cin>>num1>>num2;

		 cout<<setw(8)<<fixed<<setprecision(2)<<"\nThe product of the two fractions is " <<num1*num2<<endl<<endl;
			  
		 break;

		
			
	 default:
		 cout<<"\nPlease read the instructions"<<endl<<endl;
		
		}			//end of switch


}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
double addfunc (double num1, double num2)
{addfunc = num1+num2
}



double multfunc 
{multfunc = (num1*num2)
}


double convfunc
{
}
Also, i need to use pass by value for the add function and use the 'return' statement. For the conversion function, i must have two arguments and must pass one by reference that will pass the answer back to the main. For the multiplication function, it must receive its arguments through global variables and return it to main using global variables.
Your function syntax is wrong. Read the tutorial on this site:
http://www.cplusplus.com/doc/tutorial/functions/
Am i close? and can you help me with some of my other problems if you have the time?

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
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;
void addfunc(int num1, int num2);
void multfunc(int num1, int num2);
void convfunc(double num1);
int dec;



int main()

{ double num1;
  double num2;
  char answer;
  char choice = 'y';
	
  do
  {
	
  cout <<"Enter a code for the desired operation"<<endl<<endl;
  cout <<" a to add two fractions"<<endl;
  cout <<" c to convert a fraction to a decimal"<<endl;
  cout <<" m to multiply the two fractions"<<endl;
  cin>>answer;

  switch (answer)
	  {case 'a':
		cout<<"Please input two fractions to be added"<<endl<<endl;
		cin>>num1>>num2;
		addfunc(num1,num2);

		cout<<setw(5)<<fixed<<setprecision(3)<< "\nThe sum of the fractions entered is " <<sum<<endl<<endl;
		break;

	 case 'c':
		 cout<<"Please input a fraction to be converted to a decimal"<<endl<<endl;
		 cin>>num1;
		 convfunc(num1);

		 cout<<setw(5)<<fixed<<setprecision(3)<< "\nThe decimal value of  "<<endl<<endl;
		 break;

	 case 'm':
		 cout<<"Please input two fractions to be multiplied"<<endl<<endl;
		 cin>>num1>>num2;
		 multfunc(num1,num2);

		 cout<<setw(5)<<fixed<<setprecision(3)<<"\nThe product of the two fractions is " <<num1*num2<<endl<<endl;
			  
		 break;

		
			
	 default:
		 cout<<"\nPlease read the instructions"<<endl<<endl;
		
		}			//end of switch

  }while(choice == 'y' || choice == 'Y');
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int addfunc (int num1, int num2, int sum)
{
	sum = num1+num2;
	
}



int multfunc (int&num1, int&num2, int&prod)
{
	prod = (num1*num2);
	

}


double convfunc (double num1)
{int dec;
	dec = ;
		return (dec);
}
In your addfunc and multfinc, you need to return the result. Get rid of the third parameter in each of them.
Ok, did that. How do i get it so the user inputs fractions? also, i need to pass the addfunc by value, one of the arguments from the multfunc by reference, and use global variables for the convfunc.
jon smith wrote:
How do i get it so the user inputs fractions?
Could you give an example of what you mean by this?
jon smith wrote:
also, i need to pass the addfunc by value, one of the arguments from the multfunc by reference, and use global variables for the convfunc.
OK. Is some part of this confusing you? If so, be specific ;)
i want the user to input a fraction such as (1/2), but if they literally put in "1/2" won't it convert it to a number? im using integer, but is that correct for fractions? i am also confused as to how i am supposed to convert it, like what the function would be. I have made programs using pass by value and pass by reference only, but never together. and i havent used global varialbles yet and im not sure how to implement all those into the functions and then call them into the main.
The user would have to enter 0.5, but the integral data types like int only hold whole numbers, so you would have to use a floating point type like double.
Is there a way to have the user input the fraction in the form of a fraction? and here is my code so far, any recommendations? not near done...

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
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;
void addfunc(int num1, int num2);
void multfunc(int num1, int num2);
void convfunc(double num1);
int dec;



int main()

{ double num1;
  double num2;
  char answer;
  char choice = 'y';
	
  do
  {
	
  cout <<"Enter a code for the desired operation"<<endl<<endl;
  cout <<" a to add two fractions"<<endl;
  cout <<" c to convert a fraction to a decimal"<<endl;
  cout <<" m to multiply the two fractions"<<endl;
  cin>>answer;

  switch (answer)
	  {case 'a':
		cout<<"Please input two fractions to be added"<<endl<<endl;
		cin>>num1>>num2;
		addfunc(num1,num2);

		cout<<setw(5)<<fixed<<setprecision(3)<< "\nThe sum of the fractions entered is " <<sum<<endl<<endl;
		break;

	 case 'c':
		 cout<<"Please input a fraction to be converted to a decimal"<<endl<<endl;
		 cin>>num1;
		 convfunc(num1);

		 cout<<setw(5)<<fixed<<setprecision(3)<< "\nThe decimal value of  "<<endl<<endl;
		 break;

	 case 'm':
		 cout<<"Please input two fractions to be multiplied"<<endl<<endl;
		 cin>>num1>>num2;
		 multfunc(num1,num2);

		 cout<<setw(5)<<fixed<<setprecision(3)<<"\nThe product of the two fractions is " <<num1*num2<<endl<<endl;
			  
		 break;

		
			
	 default:
		 cout<<"\nPlease read the instructions"<<endl<<endl;
		
		}			//end of switch

  }while(choice == 'y' || choice == 'Y');
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int addfunc (int num1, int num2)
{int sum;
	sum = num1+num2;
	return (sum);
	
}



int multfunc (int&num1, int&num2)
{int prod;
	prod = (num1*num2);
	return (prod);

}


double convfunc (double num1, double num2)
{
		return (num1/num2);
}
jon smith wrote:
Is there a way to have the user input the fraction in the form of a fraction?
Yes, you read in the numerator, then read the slash into a char (which you will not use), then read in the denominator.

Also, there is a much simpler way to write your functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int addfunc(int num1, int num2)
{
	return num1+num2;
}



int multfunc(int&num1, int&num2)
{
	return num1*num2;
}


double convfunc(double num1, double num2)
{
		return num1/num2;
}
Though I believe your convfunc had to use global variables? I wish they would not teach that as it is wrong, but you need to do it for your assignment.
Last edited on
I was taught that global variables is wrong, but he just wants us to know how to use it i guess. but yes i am supposed to use global variables for convfunc.
How would i read in the numerator and denominator?





Also, im pretty sure the output is supposed to be in fractions too.(except the conversion function) i am really confused as to how to do that, especially when adding.
Is anyone free to help me, please?
jon smith wrote:
How would i read in the numerator and denominator?
jon smith wrote:
Also, im pretty sure the output is supposed to be in fractions too.(except the conversion function) i am really confused as to how to do that, especially when adding.
L B wrote:
Yes, you read in the numerator, then read the slash into a char (which you will not use), then read in the denominator.


It's all just basic steps which you already know. You just need to put them together. The numerator and denominator are of type int. The slash is of type char. Just define separate variables for each. As LB has said, you don't use the contents of the slash, but you do need to read it.

If the output is supposed to be in the form of a fraction, then you will need to keep the number in the form of a fraction the whole way through the program. In order to simplify that, you might use a container to group these related values together.

Here I show the two different ways. I definitely recommend the use of the struct, though you can manage without if you wish.
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
#include <iostream>

using namespace std;

struct fraction {
    int numer;
    int denom;
};

int main()
{
    const char slash = '/';
    char dummy;
// either do this
    int numer;
    int denom;
    cout << "enter a fraction a/b: " << endl;
    cin >> numer >> dummy >> denom;
    
// or do this
    fraction A;
    cout << "enter a fraction a/b: " << endl;
    cin >> A.numer >> dummy >> A.denom;
    
// output each
    cout << "first: "  << numer << slash << denom << endl; 
    cout << "second: " << A.numer << slash << A.denom << endl; 

}


As for adding or multiplying fractions, just use the rules which you learned at school. For example
2/3 + 5/7 = (2*7 + 5*3) / (3 * 7)
and so on.

There is one refinement which you might want to add.
1/2 + 3/4 = 5/4 but if you just follow the rules , you would get 10 / 8.
Thus you may want to simplify the result by dividing numerator and denominator by their largest common factor.
Topic archived. No new replies allowed.