The problem I have getting 5 names from the user and displaying them in alphabetical order. This is my code. When debugging the problem seems to be in the sorted function. It is supposed to tell me when the array is fully sorted. Can someone tell me where my problem is. Thanks
#include<iostream>
#include<string>
using namespace std;
void swap(string,string);
bool sorted (string [3]);
void main ()
{
string firstname[3];
string sortfirstname[3];
int orginialindex[3];
string lastname[3];
float gpa[3];
int id[3];
for (int i=0;i<3;i++)
{
cout<<"Enter firstname"<<endl;
cin>>firstname[i];
}
for (int i=0;i<3;i++)
{
sortfirstname[i]=firstname[i];
}
while (!(sorted(sortfirstname)))
{
for (int i=0;i<3;i++) //3-2-1
{
if (sortfirstname[i]>sortfirstname[i+1])
{
swap(sortfirstname[i],sortfirstname[i+1]);
}
}
}
cout<<sortfirstname[0]<<sortfirstname[1]<<sortfirstname[2];
}
void swap (string a, string b)
{
string temp = b;
b = a;
a = temp;
}
bool sorted (string sortfirstname[3])
{
bool sort;
for (int i=0;i<3;i++)
{
if (sortfirstname[i]<sortfirstname[i+1])
sort = true;
else
sort = false;
}
return sort;
}