-1

Was trying to run a simple calculator using a while loop and internal class. My issue is that I used a while loop that had the condition that when a boolean called flag is equal to true it would continually run the program over and over. To exit the while loop I included a section to ask the user if it wanted to continue and allowed them to input a value to the flag. No matter how many different versions of the conditions I use the loop only runs with once. I currently have a do while loop that checks if an int called loop is less than 2 which is initialized to have the value of 1. After presenting the value it increments int loop so that it is 2 and doesn't meet the loop requirements. Then asks the user if they want to continue, which if true resets the value to 1. Still hasn't worked and nothing online has shown the same issue or a solution, even in different languages. Thank you to any additions.

Code: (C++)

//  main.cpp
//  Calculator
//
//  Created by yared yohannes on 12/10/21.
//
class calculation{
public:
    calculation(){
        
    }
    int add(int first, int second){
        int result= first+second;
        return result;
    }
    int minus(int first, int second){
        int result= first-second;
        return result;
    }
    int multi(int first, int second){
        int result= first*second;
        return result;
    }
    int divide(int first, int second){
        int result= first/second;
        return result;
    }
};


#include <iostream>
using namespace std;


int main(){
    int first=0,second=0;
    bool flag=true;
    char sign;
    int loop=1;
    calculation calc;
    cout<<"Welcome to the calculator program.\n";
    
    do{
        cout<<"Please enter the first value: ";
        cin>>first;
        cout<<"Please enter the desired operation(+,-,*,/): ";
        cin>>sign;
        cout<<"Please enter the second value: ";
        cin>>second;
        if(sign=='+'){
            cout<<calc.add(first, second)<<"\n";
        }
        else if(sign=='-'){
            cout<<calc.minus(first, second)<<"\n";
        }
        else if(sign=='*'){
            cout<<calc.multi(first, second)<<"\n";
        }
        else if(sign=='/'){
            cout<<calc.divide(first, second)<<"\n";
        }
        
        
        cout<<"Do you want to continue(true or false): ";
        cin >> flag;
        loop++;
        if(flag==true){
            loop=1;
        }
        
    }while(loop<2);
    
}
Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
Yaredyy
  • 5
  • 5
  • 2
    Not 100% match, but the issue is the same: [cin and boolean input](https://stackoverflow.com/questions/26203441/cin-and-boolean-input). In short, reading `bool` from `cin` only accepts `0` or `1` as input. To read `true` or `false` strings, you need `std::cin >> std::boolalpha >> flag;` – Yksisarvinen Dec 12 '21 at 23:37
  • @Yksisarvinen I have "using namespace std" at the top so do I need to include the std:: ? – Yaredyy Dec 12 '21 at 23:39
  • 2
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yksisarvinen Dec 12 '21 at 23:40
  • @Yksisarvinen sorry for the double reply. Just tried it without the std:: and it worked. Thanks alot – Yaredyy Dec 12 '21 at 23:41
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Dec 12 '21 at 23:42
  • @SamVarshavchik If you mean debugger as in the field that shows any errors and the position/links to the error. Then nothing appeared. I tried removing the solution and couldn't find a debugger option so maybe I'm just too inexperienced. – Yaredyy Dec 12 '21 at 23:45
  • 1
    There's a lot more to the debugger than that. The debugger will show you the values of all variables, and by running the program, one line at a time, you see exactly what it's doing and why the program executes each if/while loop, by examining the values of all variables used by each if/while loop you will understand exactly why the if/while loop executes or doesn't execute. – Sam Varshavchik Dec 12 '21 at 23:47
  • @SamVarshavchik Damn I'm gonna have to check this out cause this would relieve so much processing on my head alone trying to figure out what my code does step by step. Thanks so much bro. – Yaredyy Dec 12 '21 at 23:51

1 Answers1

0

In C++ bools are stored as 0 or 1 values. 0 is false and 1 is true. If you're putting in "true" for the cin statement it won't work so loop will always increase. What you have to do is put in 0 or 1 into the console. You could also store the input as a string and use if statements to check if it is "true" or "false" and set the boolean value based on that. Like this:

#include <string>

/*
The code you have
*/

int main() {
   string booleanInput;

   //Code you have

   cin >> booleanInput;
   if(booleanInput == "true") {
      flag = true;
   } else if(booleanInput == "false") {
      flag = false;
   }

   //Other code you have

}
Gold87
  • 38
  • 5