CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2006
    Posts
    20

    if statement prolem

    See Im Tryna Make My Text Based RPG in Console Application.


    when using if statement i want it to repeat its self if they dont enter the right character on keyboard.


    Say i wanted them to enter their home from a list.

    cout<<"Where Would you like to go?\nHome\nJob\nStore";
    cin>>y;
    if(!strcmp(home,"h")){
    //Make it go to the home and do some stuff then if it isnt ensertered right it will display a message and say "Its To Early to go here Because Your Broke";


    how could i work this out?

  2. #2
    Join Date
    Aug 2004
    Posts
    148

    Re: if statement prolem

    Code:
    if(y != 'h')
    {
        //Do stuff
    }
    else cout<<"To early to go home.."<<endl;
    I think that is all you are after?

  3. #3
    Join Date
    Jun 2006
    Posts
    20

    Re: if statement prolem

    Alright great im going to try and ask this as detailed as possible

    say we do what you told me to do above how can i make this inifinite and go back to this message:
    Code:
    cout<<"Where Would you like to go?\nHome\nJob\nStore";
    until they have picked the right one. with out adding a million if statements

  4. #4
    Join Date
    Aug 2004
    Posts
    148

    Re: if statement prolem

    Yes you could make it a while loop..
    Code:
    while(y != 'h')
    {
         cout<<"Where Would you like to go?\nHome\nJob\nStore"<<endl;
         cin>>y;
    }
    Something like that will work for you..

  5. #5
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: if statement prolem

    while loop?

    Code:
    while (1) // infinite loop
    {
        cout << "Your text" << endl;
        cin >> y;
    
        if (y == 'h')
            // do what you want because they chose home
            break; // exit the loop
    }
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  6. #6
    Join Date
    Jun 2006
    Posts
    20

    Re: if statement prolem

    can u both expplain both of them so i get a better understanding of it

    Yep Im a Noob and C++ is one of the best i see so far.

  7. #7
    Join Date
    Aug 2004
    Posts
    148

    Re: if statement prolem

    Quote Originally Posted by scheols
    can u both expplain both of them so i get a better understanding of it

    Yep Im a Noob and C++ is one of the best i see so far.
    My while loop will run until 'h' is entered at the prompt..
    Of course you must ensure that y != 'h' before you enter the loop

  8. #8
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: if statement prolem

    my loop will loop forever, only when you enter the correct option it will 'break' or exit oout of the while loop.

    The while statement will loop until the condition supplied is false (or 0).

    Code:
    int i = 0;
    
    while (i != 10)
    {
        cout << "something";
        i++;
    }
    that will loop 10 times, printing "something" 10 times. After the print, i gets incremented by i, so after the first time i will now equal 1 and so on. Once i gets to ten, the app will get out of the loop.

    To exit out of a loop you can also use the break keyword.
    Now in my while loop, it is while(1), which is basically while(true), so it will never exit, inside it I test if the character inputted was 'h', if it was then I break out of the loop.

    You may also want to test if they input a capital h by modifying the if statement as:
    Code:
    if (y == 'h' || y == 'H')
    the '||' is a logical OR operator, which basically reads out as if y is equal to h OR y is equal to H.

    Hope that explains it to you.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  9. #9
    Join Date
    Jun 2006
    Posts
    20

    Re: if statement prolem

    Okay Is it possible to do more then one input say i did what i did and i used while statement

    how can i make it so if they pick one they do somthen else so it wouldnt matter what character they push.

    like i say go to cout<<"Go to Home \nor\n Out Side\nor\nStore";

    how can i make it do the while loop and do either the first two and not the third one.

  10. #10
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: if statement prolem

    Take my example up there and add to it:

    Code:
    while (1) // keep looping until we get something
    {
         cout << "Where Would you like to go?\nHome\nJob\nStore\n";
    
         if (y == 'h')
         {
              HomeFunction(); // user defined function for Home
              break;
         }
    
         else if (y == 'j')
         {
              JobFunction(); // user defined function for picking Job
              break;
         }
         else
         {
              cout << "Not a valid character.\n";
         }
    }
    Now if you select 'j' it will take you to Job, 'h' will take you home, any other character entered will print "Not a valid character." and continue the while loop.

    Also if you want it to loop back to the start after you handle the selected function, remove the breaks, and don't forget to put in an exit condition, i.e.:

    Code:
    // If q is pressed, break out of loop
    if (y == 'q')
    {
        break;
    }
    Is that what you wanted?
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  11. #11
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: if statement prolem

    Quote Originally Posted by Notsosuperhero
    Take my example up there and add to it:

    Code:
    while (1) // keep looping until we get something
    {
         cout << "Where Would you like to go?\nHome\nJob\nStore\n";
    
         if (y == 'h')
         {
              HomeFunction(); // user defined function for Home
              break;
         }
    
         else if (y == 'j')
         {
              JobFunction(); // user defined function for picking Job
              break;
         }
         else
         {
              cout << "Not a valid character.\n";
         }
    }
    Now if you select 'j' it will take you to Job, 'h' will take you home, any other character entered will print "Not a valid character." and continue the while loop.

    Also if you want it to loop back to the start after you handle the selected function, remove the breaks, and don't forget to put in an exit condition, i.e.:

    Code:
    // If q is pressed, break out of loop
    if (y == 'q')
    {
        break;
    }
    Is that what you wanted?
    Did u know in Case of Not a valid Character you are making a Infinite loop. Have a look again back to your code.Here is One Didn't tested .But hpefully work Proper

    Code:
    	char ch,ch1='y';
    	cout<<"Enter Where you want to go \n h for Home  \n j for job \n s for Store\n";
    	cin>>ch;
    	do
    	{
    		switch(ch)
    		{
    			case 'j':
    			case 'J':
    				{
    					JobFunction();
    					ch =NULL;
    					break;
    				}
    			case 'h' :
    			case 'H':
    				{
    					HomeFunction();
    					ch =NULL;
    					break;
    				}
    			case 's' :
    			case 'S':
    				{
    					StoreFunction();
    					ch =NULL;
    					break;
    				}
    			default:
    				{
    					cout<<" Not a valid Choice Do you want to try it again for yes Press Y \n";
    					cin>>ch1;									
    				}
    			if(ch1=='y' || ch1=='Y')
    			{
    				cin.ignore();
    				cin.clear();
    				cout<<"Enter Where you want to go \n h for Home  \n j for job \n s for Store\n";
    				cin>>ch1;
    			}
    		    else
    				break;
    		}
    		
    	}while(ch=='h' ||ch =='H' ||ch =='j' ||ch=='J' || ch=='s' ||ch=='S');
    	cout<<"Come out";
    Thanx

  12. #12
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: if statement prolem

    Oh oops, didin't even realize that

    Sorry for the mis info.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  13. #13
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: if statement prolem

    it's okay dude. But keep in Mind all of these things.
    Thanx

  14. #14
    Join Date
    Jun 2006
    Posts
    20

    Re: if statement prolem

    alright how do you make your own functions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured