Click to See Complete Forum and Search --> : if statement prolem
scheols
June 13th, 2006, 06:11 PM
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?
Code_Nerd
June 13th, 2006, 06:58 PM
if(y != 'h')
{
//Do stuff
}
else cout<<"To early to go home.."<<endl;
I think that is all you are after?
scheols
June 13th, 2006, 08:08 PM
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:
cout<<"Where Would you like to go?\nHome\nJob\nStore";
until they have picked the right one. with out adding a million if statements
Code_Nerd
June 13th, 2006, 08:13 PM
Yes you could make it a while loop..
while(y != 'h')
{
cout<<"Where Would you like to go?\nHome\nJob\nStore"<<endl;
cin>>y;
}
Something like that will work for you..
Notsosuperhero
June 13th, 2006, 08:15 PM
while loop?
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
}
scheols
June 13th, 2006, 08:18 PM
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.
Code_Nerd
June 13th, 2006, 08:42 PM
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 ;)
Notsosuperhero
June 13th, 2006, 09:40 PM
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).
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:
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.
scheols
June 13th, 2006, 09:46 PM
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.
Notsosuperhero
June 13th, 2006, 09:54 PM
Take my example up there and add to it:
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.:
// If q is pressed, break out of loop
if (y == 'q')
{
break;
}
Is that what you wanted?
humptydumpty
June 14th, 2006, 12:50 AM
Take my example up there and add to it:
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.:
// 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
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
Notsosuperhero
June 14th, 2006, 10:31 AM
Oh oops, didin't even realize that :blush:
Sorry for the mis info.
humptydumpty
June 14th, 2006, 10:37 AM
it's okay dude. But keep in Mind all of these things.
Thanx
scheols
June 14th, 2006, 04:22 PM
alright how do you make your own functions
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.