Answer to Question #214592 in C++ for Ali

Question #214592

Input six names from text file and use if (s1>s2) then swap to sort 5 names (use string library)


1
Expert's answer
2021-07-07T01:53:33-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS