Code:
/*
The next three lines simply include all
the necessary packages
*/
#include <iostream>
#include <string>
using namespace std;
/*
This declares a constant. Although the
two words are completely opposite, think
of a constant as a variable that never
changes
*/
const int numnames = 5;
int main()
{
/*
names is an array of strings, length is
numnames, the constant value of 5
*/
string names[numnames];
/*
for (starting values;
condition (if true, loop executes);
what to increment variables by)
{
code to execute;
}
If you have multiple starting values,
etc., you can separate them with commas.
If you have just one line in the loop,
you can omit the {}'s. Make sure you
don't have a ; after the for statement.
*/
/*
Reads in data to sort
*/
for (int c=0; c < numnames; c++)
{
cout<<"Enter Name # "<<c<<": ";
cin>>names[c];
}
/*
Main logic loop
- Compares each string
- Depending on results, will swap
strings to eventually smooth
everything into correct order
*/
for (int c1=0; c1 < numnames-1; c1++)
{
int min = c1;
/*
A nested loop - a loop inside a loop
*/
for (int c2=c1+1; c2 < numnames; c2++)
if ( names[min] > names[c2] )
min = c2;
if ( c1 != min )
names[c1].swap(names[min]);
}
/*
This loop reads out the sorted strings
*/
for ( c=0; c<numnames; c++)
cout<<names[c]<<endl;
}
Just any half-decent tutorial from Google will do. If you have a book, use that.