Home > Blockchain >  How do I make the if statement return to the previous step?
How do I make the if statement return to the previous step?

Time:11-11

so I want to return to 'Enter a new email address: " once the user enters an incorrect email address rather than it move on to the next step.

cout<<"\n\t Enter a new email address: ";
                     cin>>str;
                     if(Email_check(str))
                     {
                    cout<<""; //i dont know how to check it without it giving any output, so i did ""
                     }
                        else{
                    cout<<"\n\t Email Address is not valid."<<endl;
                    //HERE I WANT IT TO RETURN TO "ENTER A NEW EMAIL ADDRESS"
                    cout<<"\n\t Enter a new ID number: ";
                    cin>>newid;

I could only manage to return 0; which ends the program but thats not what i want.

CodePudding user response:

Whenever you want to do something repeatedly you need some kind of loop. Since you want to repeat until an email address is valid a do ... while loop seems appropriate.

Here's one way to do it

bool email_valid = false; // track whether email is valid
do
{
    cout << "\n\t Enter a new email address: ";
    cin >> str;
    if (Email_check(str))
    {
        email_valid = true; // email is valid
    }
    else
    {
        cout << "\n\t Email Address is not valid." << endl;
    }
}
while (!email_valid); // repeat if email not valid

cout << "\n\t Enter a new ID number: ";
cin >> newid;
  • Related