Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code.
A teacher has asked all her students to line up according to
their first name. For example, in one class Amy will be at the
front of the line, and Yolanda will be at the end. Write a program
that prompts the user to enter the number of students in the class,
then loops to read that many names. Once all the names have been
read, it reports which student would be at the front of the line
and which one would be at the end of the line. You may assume that
no two students have the same name.
Input Validation: Do not accept a number less than 1 or greater
than 25 for the number of students
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
// declaring interger type values
ifstream inputFile;
string names[20];
int i, n = 0;
// opening a file
inputFile.open("lineUp.txt");
if (!inputFile)// check for file
cout << "error in opening
file\n";
else
{
while (inputFile >>
names[n])
{
n++;
}
n++;
//sorting names
int temp;
// loop to iterate all names
for (int i = 0;i <= n -
1;i++)
{
temp = i;
//for string
each string
for (int j = i +
1;j
if (names[j]
temp = j;
names[i].swap(names[temp]);
}
inputFile.close();
}
//end close
//display name at front
cout << "Front line:" <<names[1] <<
endl;
//display name at end
cout << "end line:"
<<names[n - 1] << endl;
//pause system for a while
system("pause");
return 0;
}
}
//C++ program
#include <iostream>
using namespace std;
int main()
{
// declaring interger type values
int no_of_students;
string front,end;
while(1){
cout<<"Enter the number
of students in the class : ";
cin>>no_of_students;
if(no_of_students>=1
&& no_of_students<=25)break;
cout<<"Number of
students should between 1 and 25\n";
}
cin.get();
string name[no_of_students];
for(int i=0;i<no_of_students;i++){
cout<<"Enter name of
student-"<<i+1<<" : ";
getline(cin,name[i]);
if(i==0){
front =
name[0];
end =
name[0];
}
else{
if(name[i]<front)front = name[i];
if(name[i]>end) end = name[i];
}
}
cout << "Front line:" <<front << endl;
//display name at end
cout << "end line:" <<end << endl;
//pause system for a while
system("pause");
return 0;
}
//sample output
Get Answers For Free
Most questions answered within 1 hours.