Krilla13
Krilla13

Reputation: 9

How to make my program loop back to the beginning based on user input?

I am trying to create a repetition structure for when I type 'Y' at the end of the code to rerun the "insurance price check" again.

#include <iostream>
using namespace std;

int main() {

    // Declaration of variables
    char animal, status, continue_;
    int i=0;

    //Begin Loop
    cout<<"Welcome to the Animal Insurance Company! What type of animal would you like to insure today: Enter D for Dog, C for Cat, B for Bird or R for Reptile: "<<endl;
    cin>>animal;

    if(animal=='D' || animal=='d') {
    cout<<"You have selected a dog, has your dog been neutered? Enter Y for Yes or N for NO."<<endl;
    cin>>status;
    if(status=='Y' || status=='y')
    cout<<"The insurance for your dog cost is $50."<<endl;
    else if(status =='N' || status=='n')
    cout<<"The insurance for your dog cost is $80."<<endl;
    else
    cout<<"Invalid Input, please type Y or N"<<endl;
}

else if (animal=='C' || animal=='c') {
    cout<<"You have selected a cat, has your cat been neutered? Enter Y for Yes or N for NO."<<endl;
    cin>>status;
    if(status=='Y' || status=='y')
    cout<<"The insurance for your cat cost is $40."<<endl;
    else if(status =='N' || status=='n')
    cout<<"The insurance for your cat cost is $60."<<endl;
    else
    cout<<"Invalid Input, please type Y or N"<<endl;
}

else if (animal=='B' || animal=='b' || animal=='R' || animal=='r')
cout<<"The insurance cost will be $10"<<endl;
else
cout<<"Invalid Input"<<endl;

cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
cin>>continue_;
if(continue_=='n' || continue_=='N') 
cout<<"Thank you for using Animal Insurance Company"<<endl;


return 0;
}

How do I make the code loop back to the beginning?

Upvotes: 0

Views: 7186

Answers (3)

Richardson Ansong
Richardson Ansong

Reputation: 790

B. Ward is right, you have to use "nested" do-while loops to fully solve your problem. Because there are other conditions inside your code that needs to be met before program can proceed and they will also require the services of the do-while loop. Like this;

#include <iostream>

using namespace std;

int main() {

    // Declaration of variables
    char animal, status, continue_;
    int i=0;

    //Begin Loop
    do {
        cout<<"Welcome to the Animal Insurance Company! What type of animal would you like to insure today: Enter D for Dog, C for Cat, B for Bird or R for Reptile: "<<endl;
        cin >> animal;
        if(animal=='D' || animal=='d') {
            cout<<"You have selected a dog, has your dog been neutered? Enter Y for Yes or N for NO."<<endl;

            //until the required input is entered, program will keep asking for it
            do {
                cin>>status;
                if(status=='Y' || status=='y') {
                    cout<<"The insurance for your dog cost is $50."<<endl;
                    break;
                }
                else if(status =='N' || status=='n') {
                    cout<<"The insurance for your dog cost is $80."<<endl;
                    break;
                }

                else {
                    cout<<"Invalid Input, please type Y or N"<<endl;
                }

            }while(status != 'y' || status != 'Y' || status != 'n' || status != 'N');

        }

        else if (animal=='C' || animal=='c') {
            cout<<"You have selected a cat, has your cat been neutered? Enter Y for Yes or N for NO."<<endl;

            //until the required input is entered, program will keep asking for it
            do {
                cin>>status;
                if(status=='Y' || status=='y') {
                    cout<<"The insurance for your dog cost is $40."<<endl;
                    break;
                }
                else if(status =='N' || status=='n') {
                    cout<<"The insurance for your dog cost is $60."<<endl;
                    break;
                }

                else {
                    cout<<"Invalid Input, please type Y or N"<<endl;
                }

            }while(status != 'y' || status != 'Y' || status != 'n' || status != 'N');

        }

        else if (animal=='B' || animal=='b' || animal=='R' || animal=='r')
            cout<<"The insurance cost will be $10"<<endl;
        else {
            cout<<"Invalid Input"<<endl;
            break;
        }

        cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
        cin>>continue_;
        if(continue_=='n' || continue_=='N')
            cout<<"Thank you for using Animal Insurance Company"<<endl;

    }while(continue_ == 'y' || continue_ == 'Y');

    return 0;
}

Upvotes: 0

B. Wolf
B. Wolf

Reputation: 682

I'd recommend you use a do while loop. https://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm

do
{
// begin loop
...

}while(continue_!='n' && continue_!='N');

Upvotes: 1

Matt
Matt

Reputation: 987

Well for starters you would need a loop...

In this example, probably a while loop (pre-test if you are interested in looking it up)

To achieve what you want, you would need a boolean flag, and would run the loop as long as the flag is set to true.

(Assuming that the rest of your code works fine)

// Declaration of variables
char animal, status, continue_;
int i=0;
bool running = true;  



//Begin Loop
while (running == true) {

    // Rest of program

    cout<<"Do you want to insure another animal? Enter Y for Yes or N for NO."<<endl;
    cin>>continue_;
    if(continue_=='n' || continue_=='N') {
        cout<<"Thank you for using Animal Insurance Company"<<endl;
        running = false;
    }
}
return 0;
}

Upvotes: 0

Related Questions