-2

I am programming a sum and subtraction only calculator in c++. I'm using 2 void functions, one for the user interface whereas the user can choose wether to use the sum or subtraction functions (or exit the program as well). The other one is made up of 3 conditionals, the first 2 to the algebraic operation the calculator has to do in order to deliver the result of the arithmetical operation. The third one is for when the user didn't choose a valid option, and redirects the user to where it started.

The problem here is that the second conditional if being ignored. For instance, if I input "2" or "2.", the program says outputs "Invalid choice", as if the third conditional was met, but it is not for sure. How can I solve this? Here's the code:

#include <iostream>
#include <thread>

using namespace std;

float userInterfaceChoice;
float num1;
float num2;

void conditionals();

void userInterface()
{
    cout<<"CALCULATOR"<<endl
        <<""<<endl
        <<"Please choose an option"<<endl
        <<"1. Sum"<<endl
        <<"2. Subtraction"<<endl
        <<"3. Exit"<<endl;
    cin>>userInterfaceChoice;
    
    this_thread::sleep_for(1s);
    conditionals();
}


void conditionals()
{
    // Sum
    
    if(userInterfaceChoice==("1"||"1."))
    {
        // Input of numbers
        cout<<""<<endl
            <<"SUM"<<endl
            <<"Enter the first number"<<endl;
        cin >>num1;
        cout<<""<<endl
            <<"Enter the second number"<<endl;
        cin >>num2;
        
        // Operation & Output
        
        cout<<"The sum equals "<<num1+num2<<endl;
        
        // Wait 2 seconds, then come back to the GUI
        
        this_thread::sleep_for(2s);
        userInterface();
    }
    
    // Subtracion
    
    else if(userInterfaceChoice==("2"||"2."))
    {
        // Numbers input
        cout<<""<<endl
            <<"SUM"<<endl
            <<"Enter the first number"<<endl;
        cin >>num1;
        cout<<""<<endl
            <<"Enter the second number"<<endl;
        cin >>num2;
        
        // Operation & Output
        
        cout<<"The subtraction equals"<<num1-num2<<endl;
        
        // Wait 2 seconds, come back to the GUI
        
        this_thread::sleep_for(2s);
        userInterface();
    }
    
    //Invalid choice
    
     else if(userInterfaceChoice!=("1"&&"1."&&"2"&&"2."))
    {
        cout<<""<<endl
            <<"Invalid choice"<<endl;
        // Wait 2 seconds, come back to the GUI
        
        this_thread::sleep_for(2s);
        userInterface();
    }
}

// Execute the void functions

int main()
{
    userInterface();
    conditionals();
}

I tried to solve this problem by deleting the third conditional, but the program exits with exit code 0, which meeans that still the conditional is't met or is straightaway ignored.

Chris
  • 26,361
  • 5
  • 21
  • 42
Elrichiz
  • 13
  • 3
  • 6
    `if(userInterfaceChoice==("2"||"2."))` doesn't do what you think. – Raymond Chen Apr 10 '23 at 02:55
  • 1
    `if(userInterfaceChoice==("1"||"1."))` -- If you look through the C++ book you are using, where do you see `if` statements structured in this way? You won't find any, because this is not how to put together an `if` statement that needs to check for both values. If you are guessing how C++ works, this is the danger of doing this -- you will get code to compile, but what is compiled does something totally different than what you expected. – PaulMcKenzie Apr 10 '23 at 02:59
  • Side note: `userInterface` and `conditionals` calling each other more or less unconditionally will end in the disaster from which this site gets its name.. – user4581301 Apr 10 '23 at 03:28
  • Quick lesson on constructing a [mre]: demonstrate the error; do **not** demonstrate "a sum and subtraction only calculator". You have an `if` statement that is misbehaving for a certain value. Demonstrate that. Just that. Strip away everything else, especially user input (unless that makes the error go away). `int main() { float userInterfaceChoice = 2; if(userInterfaceChoice==("2"||"2.")) { std::cout << "Expected result\n"; } else { std::cout << "This should not happen!\n"; } }` (After your question and code, you can describe your project for background, which might explain the conditional.) – JaMiT Apr 10 '23 at 06:18
  • `if(userInterfaceChoice==("1"||"1."))` should be `if(userInterfaceChoice==1)`. And `float userInterfaceChoice;` shoulld be `int userInterfaceChoice;`. – john Apr 10 '23 at 06:30
  • @JaMiT Nice example, but unfortunately the chances of a beginner actually doing that are zero. – john Apr 10 '23 at 06:33
  • @john *"the chances of a beginner actually doing that are zero"* -- Hence "lesson", not "requirement for posting". My comment provides directions and an example for isolating a problem. (Also, I wouldn't go quite as far as "zero".) – JaMiT Apr 10 '23 at 07:07
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Apr 10 '23 at 10:49
  • @john Actually, I inderstood the code, and I am a beginner. – Elrichiz Apr 10 '23 at 14:27

1 Answers1

3

Booleans don't work this way. You are comparing float values to booleans which always evaluate to true, but those comparisons don't do what you expect.

userInterfaceChoice==("1"||"1.")

Becomes:

userInterfaceChoice == 1

But beware of == on floats, since they can be imprecise.

Likewise:

userInterfaceChoice!=("1"&&"1."&&"2"&&"2.")

Is likely meant to be:

userInterfaceChoice != 1 && userInterfaceChoice != 2)

It is furthermore strongly recommended that you avoid global variables. Data should be passed to and from functions via their arguments and return values.

Chris
  • 26,361
  • 5
  • 21
  • 42