Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>

using namespace std;

int main()
{
    int a, b;
    char o;
    cout << "Enter the equation: ";
    cin >> a >> o >> b;
    if ((cin >> a) && (cin >> b)) {
        switch (o) {
            case '+': 
                cout << a << o << b << '=' << double(a) + double(b);
                break;
            case '-': cout << a << o << b << '=' << double(a) - double(b) << endl;
                break;    
            case '*': cout << a << o << b << '=' << double(a) * double(b) << endl;
                break;
            case '/': cout << a << o << b << '=' << double(a) / double(b) << endl;
                break;
            case '<':
                if (a < b) {
                    cout << a << o << b << " = T" << endl;
                }
                else {
                    cout << a << o << b << " = F" << endl;
                }
                break;
            case '>':
                if (a > b) {
                    cout << a << o << b << " = T" << endl;
                }
                else {
                    cout << a << o << b << " = F" << endl;
                }
                break;
            case '=':
                if (a == b) {
                    cout << '(' << a << " == " << b << ')' << " = T" << endl;
                }
                else {
                    cout << '(' << a << " == " << b << ')' << " = F" << endl;
                }
                break;
            default:
                cout << "Invalid operation." << endl;
                break;
        }
    }
    else {
        cout << "Invalid input." << endl;
    }

    return 0;
}


What I have tried:

I have tried removing endl, putting cout in the same row as the case but nothing seems to work the console still does not output anything when the operation and input are valid
Posted
Updated 11-May-23 22:22pm
Comments
Member 15627495 12-May-23 3:54am    
you have to Cast from "number" to "String" Type.

cout handles String only.
// add library :
#include <string>
// and use :
double result = double(a) - double(b);
cout << to_str(result) << endl ;

// it's a Type Mismatch causing trouble.
Richard MacCutchan 12-May-23 4:07am    
That is not correct. The iostream overloads can handle any expression.

Quote:
I have tried removing endl, putting cout in the same row as the case but nothing seems to work the console still does not output anything when the operation and input are valid

Stop guessing and make sure, use the debugger and see what is going on.
C++
cin >> a >> o >> b;  // This reads 3 values
if ((cin >> a) && (cin >> b)) {
                   ^ this reads b again
     ^ this read a again

-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
The first thing to notice is that you try to read both a and b twice:
Once here:
char o;
cout << "Enter the equation: ";
cin >> a >> o >> b;
And immediately afterwards here:
if ((cin >> a) && (cin >> b)) {
So unless you actually type 1, the operator, b, and then a and b again, you program will sit waiting for the second set of inputs ... I suspect you are trying to check the values for validity, but I don't know why you are doing that!

Remove the if ... else and it might start to work.
 
Share this answer
 
Comments
CPallini 12-May-23 4:40am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900