Not sure why it's not letting me choose the operation I want where I put "Enter an operation symbol..", I'm supposed to use switch statements to compute functions.

#include <iostream> 
#include<cmath>
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
    int type;
    int a, b, c, d;
    char op;

    cout << fixed << showpoint << setprecision(2); 

    //select operation
    cout << "Enter a fraction: "; 
    cin >> a; cin.ignore(); cin >> b;
    cout << "Enter another fraction: ";
    cin >> c; cin.ignore(); cin >> d; 
    cout << "\tEnter an operation symbol (+,-, *, /): "; 
    cin >> op; cin.ignore();

    //operate function
    switch(op)
    {
    case '+' : cout << a << "/" << b << "+" << c << "/" << d << "=" << (a*d)+(c*b) << "/" << (d*b) << endl;
    break;
    case '-' : cout << a << "/" << b << "+" << c << "/" << d << "=" << (a*d)-(c*b) << "/" << (d*b) << endl;
    break;
    case '*' : cout << a << "/" << b << "+" << c << "/" << d << "=" << (a*c) << "/" << (d*b) << endl; 
    break;
    case '/' : cout << a << "/" << b << "+" << c << "/" << d << "=" << (a*d) << "/" << (c*b) << endl; 
    break;
    }


    //end program
    system ("pause");
    return 0; 
}

Dani AI

Generated

Short explanation and how to fix the skipping: the operator prompt is being satisfied from leftover input. If you type the whole expression on one line (for example 12/5+2.1/7.5) the stream extraction that reads the first fraction will leave the + in the buffer, so the later cin >> op reads it immediately. was right to flag that the program expects separate extractions; switching to a parsing pattern that explicitly consumes the slash and validates input avoids the surprise.

Robust parsing pattern (accepts either separate entries or a single-line a/b?c/d form). Use floating-point types if you want decimals and always validate the separator and denominators:

double a,b,c,d;
char sep1, sep2, op;
if (cin >> a >> sep1 >> b) {
  if (sep1 != '/') /* handle bad input */;
  // try to parse operator and second fraction from same line
  if (!(cin >> op >> c >> sep2 >> d)) {
    // operator not in buffer: clear remainder and read them separately
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin >> op >> c >> sep2 >> d;
  }
  if (sep2 != '/' || b == 0.0 || d == 0.0) /* handle errors */;
}

Formatting and other tips: use double (or float) to avoid integer division and print with fixed << setprecision(2) to get 12.00/5.00. For multiplication 12/5 * 2.1/7.5 the product is 25.2/37.5 (shown as 25.20/37.50 with two decimals). Replace system("pause") with a portable input pause (e.g., cin.get() after clearing the buffer). If input still behaves oddly, clear the stream with cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); before prompting again.

Recommended Answers

All 6 Replies

In what way does it "not let you"? Does the keyboard refuse to accept any entry when you get to the operator selection?

It comes out like this but when it asks for you to enter an operation symbol, it just automatically does it without letting me choose which one I want.

Enter a fraction: 12/5
Enter another fraction: 2.1/7.5
        Enter an operation symbol (+,-, *, /): 12/5+2/1=12/10
Press any key to continue . . .

When it asks you to enter a fraction, you're supposed to just type number <enter> number<enter>. Not the / sign.

Enter a fraction: 2
3
Enter another fraction: 4
5
    Enter an operation symbol (+,-, *, /): +
2/3+4/5=22/15

You're typing strings made of number/number when it's expecting a single int value.

Ok, so if I want to make the output look like this: How would I make it work?

Enter a fraction: 12/5
Enter another fraction: 2.1/7.5
        Enter an operation symbol (+,-, *, /): *
            (12.00/5.00)+(2.10/7.50) = 2.52/37.5
Press any key to continue . . .
Enter a fraction: 12
5
Enter another fraction: 2.1
7.5
   Enter an operation symbol (+,-, *, /): *
        (12.00/5.00)+(2.10/7.50)=25.20/37.50
Press any key to continue . . .

I want it to look like this though:

Enter a fraction: 12/5
Enter another fraction: 2.1/7.5
        Enter an operation symbol (+,-, *, /): *
            (12.00/5.00)+(2.10/7.50) = 2.52/37.5
Press any key to continue . . .

I think I got it nevermind.

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.