C++ PROGRAM A teacher has asked all her students to line up single line accordin
ID: 659765 • Letter: C
Question
C++ PROGRAM
A teacher has asked all her students to line up single line according to their first name, considering only for the two students at the front and the end of line. That meant the student at the front and at the end of the line will be set alphabetically according to their first name.
Write a C++ program that prompts the teacher to enter the total number of students in a class, and then loops to read in that many full names. All the read-in names must be recorded in the order that they were entered onto an external text file. Once all the names have been read in, the program reports which student should be at the front of the line, and which one should be at the end of the line, in alphabetical order of their names. Also, the list of the students
Explanation / Answer
Answer:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main()
{
char *students;
int numStudents;
cout << "How many students do you have? ";
cin >> numStudents;
if (numStudents < 5 || numStudents > 50)
{
cout << "Cannot have less than 5 student or more than 50 students..." << endl;
cout << "Exiting..." << endl;
}
ofstream outputFile("Names.txt");
for (int i = 1; i <= numStudents; i++)
{
cout << "Enter the name of your students: ";
cin >> students;
outputFile << students << endl;
}
outputFile.close();
ifstream inFile("Names.txt");
for ( i = 1; i <= numStudents; i++)
{
inFile >> students;
//strcmp(students);
cout << students << " ";
}
inFile.close();
char string[128],temp;
int n,j;
cout<<"alphabetical order";
n=strlen(string);
for(j=0;j<n-1;j++)
{
{
if(string[numStudents]>string[numStudents])
{
temp=string[numStudents];
string[numStudents]=string[j];
string[j]=temp;
}
}
}
cout<<" "<<string<<numStudents;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.