A teacher has asked all her students to line up single file according to their f
ID: 3552825 • Letter: A
Question
A teacher has asked all her students to line up single file 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 in that many names. once all the names have been read in it reports which students 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
I have this so far, the program works, however it is not showing the name of the student who is first in line nor the name of the student who is last in line. Please help.
// 3/17/14
// Lab 7
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
int students;
int count;
char names[25];
char first [25];
char last [25];
cout << "Enter in the number of students in the class: ";
cin >> students;
cin.ignore(25,' ');
while (students < 1 || students > 25)
{
cout << "Enter in a number that is no less than 1 and no more than 25: ";
cin >> students;
}
for (int count = 1; count <= students; count++)
{
if (count == 1) {
strcpy(first, names);
strcpy(last, names);
}
cout << "Enter in name " << count << ": ";
cin.getline(names, 25);
if (strcmp(names,first) < 1)
{
strcpy(first, names);
}
if (strcmp(names,last) > 5)
{
strcpy(last, names);
}
}
cout <<endl << first << "The first student is. ";
cout <<endl << last << "The last student is. ";
system("PAUSE");
return 0;
}
Explanation / Answer
#include <iostream> #include <iomanip> #include <cstring> using namespace std; int main(int argc, char *argv[]) { int students; int count; char names[25]; char first [25]; char last [25]; cout << "Enter in the number of students in the class: "; cin >> students; cin.ignore(25,' '); while (students < 1 || students > 25) { cout << "Enter in a number that is no less than 1 and no more than 25: "; cin >> students; } for (int count = 1; count <= students; count++) { cout << "Enter in name " << count << ": "; cin.getline(names, 25); if (count == 1) { strcpy(first, names); strcpy(last, names); } if (strcmp(names,first) < 0) { strcpy(first, names); } if (strcmp(names,last) > 0) { strcpy(last, names); }} cout <<endl << first << "The first student is. "; cout <<endl << last << "The last student is. "; return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.