-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); } 
8
  • 2
    Not 100% match, but the issue is the same: 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; Commented Dec 12, 2021 at 23:37
  • @Yksisarvinen I have "using namespace std" at the top so do I need to include the std:: ? Commented Dec 12, 2021 at 23:39
  • 2
    Why is "using namespace std;" considered bad practice? Commented Dec 12, 2021 at 23:40
  • @Yksisarvinen sorry for the double reply. Just tried it without the std:: and it worked. Thanks alot Commented Dec 12, 2021 at 23:41
  • 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. Commented Dec 12, 2021 at 23:47

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.