Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop and do while loop only running once in a calculator program. C++

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);
    
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In C++ bools are stored as a 0 or 1. 0 is false and 1 is true. If you’re putting in "true" for the cin statement it won’t work. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading