Jump to content
  • Advertisement
Sign in to follow this  
Doom-Box

C++ Loops?

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I dont understand loops very well. Basicly I dont know what to type to use them. I mean I understand what they do and how they work just not what the "code" is to use a loop. For example lets say im doing the number guessing game. And I need to loop this. How would I do it? (I only want to know what the code is) Thanks, DB

Share this post


Link to post
Share on other sites
Advertisement
next while-if loop:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int i = 5;
int input;

cout << "Guess a number: ";

while (cin >> input) {
if (input != i)
cout << "guess again" << endl;
else {
cout << "Good Job" << endl;
break;
}
}
return 0;
}




for loop


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int a = 5;
int chances = 0;

for (int i = 0; i != a; ++chances) {
cout << "Guess a number: ";
cin >> i;
}

cout << "Congradulations, you took " << chances << " guesses to get it right!" << endl;

return 0;
}






easier while loop


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int a = 5;
int input = 0;

while (a != input) {
cout << "Guess a number: ";
cin >> input;
}

cout << "You Win!" << endl;

return 0;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!