-1

the task for checking the priority string is not working i have included string header file of cstring but still. i want to check whether the input inserted by user is in the case which i have provided or else it should ask again to enter the priority. but when i am running it is not doing in the same way i want it to be.

 void add_task(){
                bool check=true;
                system("color 4F");
                cout<<"Enter the task: ";
                getline(cin,task);
                do{
                cout<<endl<<"Priority(High/Medium/Low) : ";
                gets(priority);
                **code to check whether the priority order is correct??**
                if (strcmpi(priority,"High") == true) {
                    check=true;
                    break;
    //comparing text 
                }
                else if(strcmpi(priority,"Medium") == true ){
                    check=true;
                    break;
                }
                else if(strcmpi(priority,"Low") == true){
                    check=true;
                    break;
                }
                else {
                    check=false;
                    cout<<endl<<"Please enter Correctly";
                }
            }
            while (check==false);
                cout<<endl<<"Due in days: ";
                cin>>due;
            }
  • 1
    Any reason why you seem to be using C strings (hence the C functions) in C++? And by the way, [`gets()` is unsafe and evil](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Also, you don't show where you define `priority` or `task`. Anyway, I think the issue is that `strcmp` and friends return 0 when equal, so comparing it with `true` will not be correct. – mediocrevegetable1 Apr 14 '21 at 03:46
  • Please improve code indent, too. – U. Windl Apr 14 '21 at 07:58

1 Answers1

0

you write

while (check == false);

it means when check is false process stuck there for life unless another thread or process change it you can use this code

void add_task()
{
    bool check = false;
    system("color 4F");
    cout<<"Enter the task: ";
    getline(cin,task);
    while (check == false) {
    cout<<endl<<"Priority(High/Medium/Low) : ";
    gets(priority);
    if (strcmpi(priority,"High") == true) {
        check = true;
        break;
     
    } else if (strcmpi(priority,"Medium") == true ) {
          check = true;
          break;
    } else if (strcmpi(priority,"Low") == true) {
          check = true;
          break;
    } else {
          check = false;
          cout<<endl<<"Please enter Correctly";
    }
        cout<<endl<<"Due in days: ";
        cin>>due;
    }
}

so when check is false while repeat and when check is true while will be finished

Pouya Imani
  • 182
  • 8