|
-
June 13th, 2006, 06:11 PM
#1
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?
-
June 13th, 2006, 06:58 PM
#2
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?
-
June 13th, 2006, 08:08 PM
#3
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
-
June 13th, 2006, 08:13 PM
#4
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..
-
June 13th, 2006, 08:15 PM
#5
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
}
-
June 13th, 2006, 08:18 PM
#6
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.
-
June 13th, 2006, 08:42 PM
#7
Re: if statement prolem
 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
-
June 13th, 2006, 09:40 PM
#8
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.
-
June 13th, 2006, 09:46 PM
#9
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.
-
June 13th, 2006, 09:54 PM
#10
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?
-
June 14th, 2006, 12:50 AM
#11
Re: if statement prolem
 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
-
June 14th, 2006, 10:31 AM
#12
Re: if statement prolem
Oh oops, didin't even realize that
Sorry for the mis info.
-
June 14th, 2006, 10:37 AM
#13
Re: if statement prolem
it's okay dude. But keep in Mind all of these things.
Thanx
-
June 14th, 2006, 04:22 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|