ok i have a few questions about this code. it is supposed to display the first name alphabetically and the last name alphabetically from the list.
1. i don't think the strcpy function has been covered yet in the book so i was wondering if there was a different way to assign the current char to the first and last variables.
2. when i used the strcpy function i got a warning saying i should use strcpy_s instead. i have no idea what the difference is could someone explain.
3. when i read in the file it prints to the screen just right but the "last" student is displayed as what i initilized it as which is "aaaaa". how come the name after that alphabetically is not being stored in the "last" variable?
here is my code so far:
Code:
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("LineUp.dat");
if (!infile)
{
cout << "Corrupt File " << endl;
}
int numStudents = 0;
char first[21] = "zzzzzz";
char last[21] = "aaaaaaa";
char name[21];
while (infile >> name)
{
if (strcmp(name,first) < 0)
{
strcpy_s(first, name);
}
if (strcmp(name,last) > 0)
{
strcpy_s(last, name);
}
cout << name << endl;
numStudents++;
}
cout << "The first student is " << first << endl;
cout << "The last student is " << last << endl;
cout << "There are " << numStudents << " students. " << endl;
return 0;
}