Input six names from text file and use if (s1>s2) then swap to sort 5 names (use string library)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string names[6];
/*you have to create an
input.txt file and write 6
names there before
running the program*/
ifstream fin("input.txt");
if(!fin)
cout<<"File not found!!!\n";
//read names form file input.txt
for(int i=0;i<6;i++)
{
fin>>names[i];
}
//sort 5 names use s1>s2 buble sort
for(int i=0;i<5;i++)
{
for(int j=i+1;j<6;j++)
{
if(names[j]>names[i])
{
swap(names[j],names[i]);
}
}
}
for(int i=0;i<6;i++)
cout<<names[i]<<" ";
return 0;
}
Comments
Leave a comment