how to put addition,sub,multiply, and division

Hello,

I need help adding, subtracting, multiplying, and dividing two fractions.
It should show up like this:

Enter your operation: 1/2 + 3/4

but the user can either put: +, - , *, /.

I just do not know how to put +, -, *, /.

is there a special character for it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <oistream>

using namespace std;

  int main()
{
	int n1, n2, d1, d2;
	char slash;

        cout << "Please enter your operation:";
	cin >> n1 >> slash >> d1 >> ?? >> n2 >> slash >> d2;


	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int n1, n2, d1, d2;
	char slash;
	char oper;

	cout << "Please enter your operation:";
	cin >> n1 >> slash >> d1 >> oper >> n2 >> slash >> d2;

	switch( oper )
	{
		case '+':
			break;
		case '-':
			break;
		// ... etc ...

	}
You seem to be misunderstanding something. Think of a char just like you would for your ints, just only a single character instead. So, though it is easiest to have a temporary just for the slashes, just have another one for the operation. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
    int n1, d1, n2, d2;
    char op, temp;

    std::cout << "Please enter you operation: ";
    std::cin >> n1 >> temp >> d1 >> op >> n2 >> temp >> d2;

    // now, you have two fractions n1/d1 and n2/d2, as well as the
    // operation to perform with them in op (e.g. '+', '-', etc...)

    // ...

    return 0;
}


EDIT:
Ninja'd by @Esslercuffi...
Last edited on
First just input those character from user, for the equation you can use switch case statement based on that character
1
2
3
4
5
6
7
8
9
10
11
12
13
char x;
cin>>x;
switch(x){
   case '+':
      //your code
      break;
   case '-':
     //another code
     break;
    ...
   default:
      //if user input wrong character
}
Last edited on
closed account (48T7M4Gy)
For a start:

1. try 'iostream' instead of 'oistream'
2. slash = '\\';
3. you can declare another char called operation to cin the operation selection required
4. use separate cin's for the numbers and operation
5. float or double would be better than int unless you want integer division truncation - eg 2/3 with integers equals 0!

<oistream>, that's the library my Jewish uncle used when he was frustrated.
closed account (48T7M4Gy)
<oistream>, that's the library my Jewish uncle used when he was frustrated.

Yeah I know Essler. <oistream> has a method called vey - it lways make me laugh when I call oi.vey() but there you go. Tell your uncle that life is too short so try Java if the challenge of C++ is getting him down.
I need help adding, subtracting, multiplying, and dividing two fractions.


From your description, I assume you want to manipulate them as fractions. It would be worthwhile, then, to define a type to make things easier:

1
2
3
4
5
struct fraction
{
    int num;
    int den;
};


And to use code that operates on (instances of) that type:

1
2
3
4
5
6
7
8
9
10
11
fraction add(fraction a, fraction b)
{
    result.den = a.den * b.den ;
    result.num = b.num*a.denom + a.num * b.denom ;
    return result ;
}

std::ostream& operator<< (std::ostream& os, fraction f)
{
    return os << f.num << '/' << f.den;
}


And then main might look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    std::cout << "Enter your operation\n> ";

    fraction a, b;
    char dummy, operation;

    std::cin >> a.num >> dummy >> a.den >> operation >> b.num >> dummy >> b.den;

    switch (operation)
    {
    case '+': std::cout << a << " + " << b << " = " << add(a, b) << '\n'; break;
    case '-': std::cout << "Subtraction unimplemented\n"; break;
    case '*': std::cout << "Multiplication unimplemented\n"; break;
    case '/': std::cout << "Division unimplemented\n"; break;
    default: std::cout << "Unexpected operator \"" << operation << "\" encountered\n"; break;
    }
}


Put together: http://ideone.com/XTqQ6H

The addition operation is intentionally naive and will likely require improvement.



Last edited on
Topic archived. No new replies allowed.