| View previous topic :: View next topic |
| Author |
Message |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 8:48 pm Post subject: [C++]looping |
|
|
sorry for the excessive amount of threads that ive been bringing into this section but im a new C++ programmer and am quite inquisitive...
| Code: | cin >> response;
if (response = 1)
goto loop;
if (response = 2)
{
cout << "Press the Enter key to exit";
cin.ignore();
cin.get();
} |
thats a snippet of code from a simple program that im making. after obtaining the value for "response", if the value is 1, then it should return to the loop at the beginning of the program. if the value is 2, then it should prompt the user to press the enter key to exit, then exit when that is done. It's obviously not correct because it just keeps looping, even when the value is changed to 2. what error have i made? keep in mind that this is my first try at loops.
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 28, 2008 8:54 pm Post subject: |
|
|
| Code: | bool bLoop = false;
cin >> responce;
if (responce == 1)
bLoop = true;
else
{
cout << "Press the enter key to exit";
cin.ignore();
cin.sync();
return 0;
}
while ( bLoop )
{
// loop your coding yea!
}
|
_________________
|
|
| Back to top |
|
 |
Spawnfestis GO Moderator
Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Mon Jan 28, 2008 9:17 pm Post subject: |
|
|
Oh yeah was just about to type the while()
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 28, 2008 9:20 pm Post subject: |
|
|
do NOT use GOTOS, DONOTDONOTDNOT
_________________
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 9:22 pm Post subject: |
|
|
| blankrider wrote: | | do NOT use GOTOS, DONOTDONOTDNOT |
why? i fuckin suck at this so far...
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 28, 2008 9:25 pm Post subject: |
|
|
it's slow and bad practice. While is much better.
while(response == 1)
{
//whatever
}
_________________
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 9:29 pm Post subject: |
|
|
ok thanks. would it be better to define my own function so i only have 1 line after while?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jan 28, 2008 9:42 pm Post subject: |
|
|
Depends. (Note that this is my opinion, and may not be correct)
If it's a big project with a lot of functions already, it'd probably be best to just go ahead with the code all in the while loop.
If this is just something small, with maybe 1 or 2 different functions (excluding "int main()"), then go ahead and create a new function.
You could even go as far as
| Code: |
while(!MyLoop(MyParams))
{
Sleep(10); //don't rape the processor
}
|
This is assuming that MyLoop is a method which returns a bool, and you want it to eventually return true. It also assumes that it takes the type of parameter that MyParams is. You use "Sleep(10)" as not to rape the computer's processor.
You should Google to find which library the Sleep() method is included in.
_________________
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jan 28, 2008 9:45 pm Post subject: |
|
|
| samuri25404 wrote: | You could even go as far as
| Code: |
while(!MyLoop(MyParams))
{
Sleep(10); //don't rape the processor
}
|
|
| Code: | | for(;!MyLoop(MyParams);Sleep(10)); |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jan 28, 2008 9:48 pm Post subject: |
|
|
Haha, even better.
Is the semi-colon needed at the end of the for-loop?
| Code: |
for(;!MyLoop(MyParams);Sleep(10));
|
_________________
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Mon Jan 28, 2008 9:51 pm Post subject: |
|
|
You could also use a switch. Here is a simple example
| Code: |
while(looping == true)
{
// Gets input
std::cout << "Choose what you whant to do.\n" << "0 to quit, "
<< "1 to whatever ";
std::cin >> c;
switch(c) // The switch
{
case 0: //Says if c = 0 exit the loop
looping = false; // Will stop the loop
std::cout << "~crys~ Will i ever see you again?\n";
break;
case 1: // same as above
break;
default:
std::cout << " Bad input" << std::endl;
break;
}
}
}
|
Uses 2 loops and a bool.
Keeps the code going and is easy. This is bare if you put in something like k it will screw it up.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 9:56 pm Post subject: |
|
|
you've lost me.
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 28, 2008 10:00 pm Post subject: |
|
|
| samuri25404 wrote: | Haha, even better.
Is the semi-colon needed at the end of the for-loop?
| Code: |
for(;!MyLoop(MyParams);Sleep(10));
|
|
nope
| Code: | for ( ;!MyLoop(MyParams);Sleep(10))
{
//do my stuff
} |
but for now i think u should just stick with the nice while loop. simple and efficiant.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jan 28, 2008 10:05 pm Post subject: |
|
|
To be simple, just put this code:
| Code: |
for(;!MyLoop();Sleep(10));
|
and define a method
| Code: |
bool MyLoop()
{
cout << "Enter 0 to quit.\n";
cout << "Enter 1 to whatever.\n";
cin >> temp;
switch(temp)
{
case 0: return true; //done looping
case 1: //do whatever
return false; //not done
default:
cout << "Bad input.\n".
return false; //once again, not done looping
}
}
|
_________________
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 10:10 pm Post subject: |
|
|
| Quote: | | but for now i think u should just stick with the nice while loop. simple and efficiant. |
agreed. all this stuff is confusing me. ill keep it as simple as possible.
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
|