Answer to Question #214591 in C++ for thankuexpert

Question #214591

1-   Input six names from text file and use strcomp and strcopy functions to sort 5 names (use cstring libtrary)

 

Where strcmp and strcpy are the followings:

  

strcomp(s1,s2): return 0 if s1 == s2

                         return positive number if s1>s2

                         return negative number if s1<s2

strcpy(s1,s2):   s1=s2


1
Expert's answer
2021-07-07T01:53:39-0400
using namespace std;


#include <stdio.h>
#include <cstring>


#define NO_OF_NAMES	6
main(void)
{
	string s,Names[NO_OF_NAMES],s1,s2;
	fstream infile; 
	int n=0;
	string Name = "./Names.txt";
	infile.open(Name.c_str());
    if (!infile) 
	{
	    cout << "Unable to open file: "<<Name;
        exit(1); // terminate with error
    }
	else
	{
		cout<<"\nAll Names from input file: "<<Name<<endl;
		while(infile >> s)
		{
			if(n<NO_OF_NAMES) Names[n] = s; 	
			cout<<s<<endl;
			n++;
	}
	}
	std::sort(Names, Names + NO_OF_NAMES, std::greater<string>());
	cout<<"\nSorted Names: ";
	for(n=NO_OF_NAMES-1;n>0;n--) cout<<Names[n]<<", ";
}

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