Home > other >  C do while loop not continue
C do while loop not continue

Time:06-04

After doing a certain task, it is supposed to ask do you want to continue(Y/N), but instead it exits. It was working yesterday, but now it's not functioning.

Here's my code. It's in the lower part:

#include<iostream>
using namespace std;

int main(){
    char yesNo;
    string bookName[50];
    string bookAuthor[50];
    string bookID[50];
    int i=0, choice; //for case 1
    string id; // for case 2
    
    do{
    
    cout << "==========================" << endl;
    cout << "LIBRARY MANAGEMENT SYSTEM" << endl;
    cout << "==========================" << endl;
    cout << "[1] Add Books" << endl;
    cout << "[2] Delete Books" << endl;
    cout << "[3] Search Books" << endl;
    cout << "[4] View Book List" << endl;
    cout << "[5] Close Application" << endl;
    cout<<"Enter a number: ";
    cin>>choice;
    switch(choice){
    case 1: // add book
        cin.ignore();
        cout<<"Enter book name: ";
        getline(cin, bookName[i]);
        cout<<"Enter book author: ";
        getline(cin, bookAuthor[i]);
        cout<<"Enter book ID: ";
        getline(cin, bookID[i]);
        i  ;
        cout<<"Book succesfully added!"<<endl;
        break;
    case 2: //delete a book
        int j;
        
        if(i!=0){
        cout<<"Enter the book ID you want to delete: ";
        cin>>id;
        for(int y=0; y<i;y  ){
            if(id==bookID[y]){
                for(j=y; j<i; j  )
                bookID[j]=bookID[j 1];
                bookName[j]=bookName[j 1];
                bookAuthor[j]=bookAuthor[j 1];
                i--;
                break;
            }}
        }else{
                cout<<"Your library is empty"<<endl;
            }
        break;
    case 3: // search book
        if(i!=0){
        cout<<"Enter the book ID you want to search: ";
        cin>>id;
        for(int y=0; y<i;y  ){
            if(id==bookID[y]){
                cout<<"Book name: "<<bookName[y]<<endl;
                cout<<"Book author: "<<bookAuthor[y]<<endl;
                cout<<"Book ID: "<<bookID[y]<<endl;
                break;
            }}
        }else{
                cout<<"Your library is empty"<<endl;
            }
            break;
        
    case 4: // display all the books
        if(i!=0){
        cout<<"All Books"<<endl;
        for(int x=0; x<i; x  ){
      
          cout<<"Book name:"<< bookName[x] << endl;
          cout<<"Book author: "<< bookAuthor[x] << endl;
          cout<<"Book ID: "<< bookID[x] << endl;
          cout<<endl;
      }  
        }else{
            cout<<"The library is empty"<<endl;
        }
        
    cout<<"Do you want to continue (Y/N)";
    cin>>yesNo;


    }}while (yesNo == 'y' || yesNo == 'Y');
        cout<<"Thank You!";

    return 0;
}

CodePudding user response:

Your "do you want to continue" prompt is in the wrong scope. You need it to be in the loop body. In the above it is in the switch statement body.

CodePudding user response:

If you fix the indentations in your code, you will see that your Do you want to continue prompt is in the wrong place. You put it inside of case 4, so when the user chooses any menu choice other than 4, yesNo remains uninitialized and the while is likely to break (the chance of yesNo randomly containing 'y' or 'Y' without user input is very unlikely).

The prompt should be after the switch instead, eg:

...
do {
    ...
    switch(choice){
        ...
        case 4: // display all the books
            ...
        
            // REMOVED FROM HERE
            //cout<<"Do you want to continue (Y/N)";
            //cin>>yesNo;

            break; // <-- ADD THIS
    }

    // MOVED HERE INSTEAD!!!
    cout<<"Do you want to continue (Y/N)";
    cin>>yesNo;
}
while (yesNo == 'y' || yesNo == 'Y');
...
  •  Tags:  
  • c
  • Related