Home > Software design >  While loop and do while loop only running once in a calculator program. C
While loop and do while loop only running once in a calculator program. C

Time:12-13

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

CodePudding user response:

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

}
  • Related