Thread seems a little dusty, so I might add a thing or 2 to this thread.
Haven't posted in here in years so lets talk a basics again.
I personally use Visual Studio Professional 2005, makes it easier because it already has the Windows SDK built in. If you want to, you can always get the Visual Studio Express Edition free from Micro$oft and download the Windows SDK separate and have them link, but thats another discussion xD..
Well, I guess I'll talk about loop functions.
<Just as a sidenote, if you're using Visual Studio, all my examples are going to be done in the 'Win32 Console Application' template in mind>
Today I'll do Spunky a favor and talk about an easy loop function called the for/next loop. This is a basic conditional looping function. I'm going to assume that you have a basic grasp of variables and what they are, for instance the int, double, string(if you have it included in the header), and others out there.
For/Next Loops are mainly used as a counter loop, so, for example, if you have a user put in number to signify how many times they lick a lollipop, it should look something like this.
// Example For/Next Loop
// Your Name Here <----
#include "stdafx.h"
#include <iostream> //Your basic user calls
using namespace std; //A lazy way to include all the methods above
int main()
{
int licks;
cout << "How many times would you like to lick the lolipop? ";
cin >> licks;
for (int i = 0; i < licks; i++)
{
cout << "Lick " << i+1 << ": Yum" << endl;
}
return 0;
}
If you're lost, thats fine, we'll go from the top. This is actually fairly simple once you see whats going on. Ignore the header for now and lets just look into main().
The first line you see is:
int licks;
Basically what is happening there is that I'm making a variable of type "int", which of course stands for integer, named licks. Make sure you end your statements with a ';', I don't know how many errors I've gotten from that dumb mistake.
The next line after that you see:
cout << "How many times would you like to lick the lolipop? ";
The "cout" is a method of the header <iostream> that basically prints, if you will, a message out to the console.
The "<<" operator is the line insertion operator. Basically what it will do for you is add to a message in this case. So when you do:
"cout <<" you are adding the capability to display something to the console.
In this case I'm adding "How many times would you like to lick the lollipop? ". You can have that say whatever you want, I always put an extra space at the end of it so it looks nice when you see it in the console, and as always, end with a ';'.
The next line as follows is:
cin >> licks;
The "cin" is also a method of the <iostream> class that allows you, in this case, to take the user input and place it into a variable.
The ">>" is the extraction operator which allows you to take that value and plop it into a variable, which in this case is licks.
Yay, and the next line you've all been waiting for is:
for (int i = 0; i < licks; i++)
What the hell is going on there you say? Lets go from the top.
The "for" commands asks for 3 things for you to pass, a variable that you're using, a comparison event, and an aftermath event. So basically what you're seeing is: for( variable ; event of sort ; aftermath ).
Specifically lets use the said line and break it down.
int i = 0. I'm having the for loop create an integer of variable name "i" and giving it a value of zero. The variable must be initialized to be used in a loop statement sometime before it occurs if it is not assigned a value already.
In the above statement, "int licks;" I did not initialize it, therefore it would have given me an error message IF I had not assigned it a value since it's creation.
OK on to the next portion, the event of sort.
I'm declaring that as long as the variable I created "i" is less than "<" my variable of licks (which has the value from the "cin"). As long as this is true we can do our loop.
And to the end, we have the aftermath. In order for this to be a good counter loop, "i" needs to at some point meet my event's needs.
I have "i++" declared at the end of it. The ++ basically means to add a value of 1 to my variable in question, in this case "i". This also holds true with the subtraction sign "--".
If this is not thorough enough, please comment this with a question, I'll see if I can give you a better response than I'm showing here.
After that we have this segment going on:
{
cout << "Lick " << i+1 << ": Yum" << endl;
}
When you are making a loop: for/next, do/do while statement, you need to encompass your statements in the brackets {,} and it needs to start with the { first.
After that we see our response entangled between our brackets. You can have it do whatever your hearts desire in between these, I simply had it display the message
Lick (your current number + 1 because it starts at 0 and it looks bad as lick 0) and then Yum.
The endl; at the end is basically an easy way to tell it to end the line and start the next.
OK, so at this point, if you compile the said code and run it, it should ask you to put a value in for the licks.
The output should look something like this if your value is 3.
How many times would you like to lick the lollipop? 3
Lick 1: Yum
Lick 2: Yum
Lick 3: Yum
Press any key to continue . . .
And yay! There you have it, if you have more questions please ask, I probably should have covered if statements before I did this, but I'll see if I can do a rewind for you guys later. Until then, happy coding, again if you have questions ask.