Return To Top of Program ? [Solved]

Posts 1–7 of 7 · Page 1 of 1
Return To Top of Program ? [Solved]
So, in my code, if you enter something you're not supposed to, I wanted it to go back to the top, and repeat it the process until you choose one of the allowed choices.
Code:
#include <iostream>





using namespace std;



{


    string gender;
    cout << "\nBoy or Girl\n";
    cin >> gender;
    if(gender == "boy")
    {
        gender = "Boy";
    }
    cout << "You are a ";
    cout << gender;
    cout << "\n";
    if(gender == "Boy")
    {
        #include "Boy.h"
    }
    else if(gender == "Girl")
    {
        #include "Girl.h"
    }
    if(gender != "Boy","Girl")
    {
        cout << "Please, pick Boy or Girl!";





    }

}
i'm no c++ expert but here is my take on your program... i also made it return to the top of the main function if it does not equal boy or girl by putting the whole thing inside a loop.

Code:
#include <iostream>
using namespace std;

int main()

{
    string gender;
    while(1)
    {
     cout << "Boy or Girl?\n";
     cin >> gender;
    
     if(gender == "boy")
     {
      gender = "Boy";
      cout << "\nYou are a " << gender << endl << endl;
      #include "Boy.h"
      cout << "Press [ENTER] to exit...";
      cin.ignore();
      cin.get();
      break;
     }
     
     if(gender == "girl")
     {
      gender = "Girl";
      cout << "You are a " << gender << endl;
      #include "Girl.h"
      cout << "Press [ENTER] to exit...";
      cin.ignore();
      cin.get();
      break;
     }
     
     else if (gender != "Boy" | gender != "Girl")
     {
         cout << "Please, pick Boy or girl!\n\n";
     }
    
    }
    return 0;
}
You can put this:

Code:
 string gender;
    while(1)
    {
     cout << "Boy or Girl?\n";
     cin >> gender;
    
     if(gender == "boy")
     {
      gender = "Boy";
      cout << "\nYou are a " << gender << endl << endl;
      #include "Boy.h"
      cout << "Press [ENTER] to exit...";
      cin.ignore();
      cin.get();
      break;
     }
     
     if(gender == "girl")
     {
      gender = "Girl";
      cout << "You are a " << gender << endl;
      #include "Girl.h"
      cout << "Press [ENTER] to exit...";
      cin.ignore();
      cin.get();
      break;
     }
     
     else if (gender != "Boy" | gender != "Girl")
     {
         cout << "Please, pick Boy or girl!\n\n";
     }
    
    }
Make that a function, then from your main function call that function.
wowow, calm down

The guy doesn't even know what a Loop is (neither knows what a function is), and yet you tell him to use an infinite loop (thats never good for someone who is learning, well, never is but w/e)? ... Why?

Also, if he doesn't know what a loop is.. I doubt that he will know what a function is, so.. telling him to call a function ain't a good solution.
^ Nig spoke D:

If you are going to spoon-feed him, at least give some explanation.
/Request close

I just made it a While loop. :P
string gender = "";
while( gender.compare( "Boy" ) != 0 && gender.compare( "Girl" ) != 0)
{
cout << endl << "Boy or Girl?";
cin >> gender;
Alright.
/Closed on Request.
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us