assignment - Alphabetical Order - Char


I have a program that gets first names from the user they are stored as char.
We have to then determine of the names entered which name would come first and which would come last alphabetically.

This should be accomplished with loops and if statements, we have not discussed any library functions that can accomplish this.

I am having problems with the alphabetical part of the program. Can I test 2 char variables to determine alphabetical order? Or is this best done with strings? I'm lost at this part I threw something together but it does not do what I expect/think it should do. I realize my current code would not work anyway to test the whole file, but just for trying to figure this out it just expects 2 names in the file (there can be more, but the testing won't accomplish as intended).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>
#include <fstream>


using namespace std;

int main(int argc, char *argv[])
{
    ofstream outputFile;
    outputFile.open("students.txt");
    
    char nameInput[40];
    int studentCount;
    char name1[40];
    char name2[40];
    char first[40];
    char last[40];
    
    cout << "How many students are there? ";
    cin >> studentCount;
    
    if(studentCount >= 1 && studentCount <= 25) //check for proper student count 1-25 allowed
    {
        for (int count = 1; count <= studentCount; count++) //ask for student names equal to the studentCount
        {             
           cout << "Enter name: ";        //Begin file output for n names
           cin >> nameInput;
           outputFile << nameInput << endl;
        }
    } 
    else
    {
        cout << "Invalid Input" << endl;
        return 0;
    }
 
    outputFile.close();   
    
    
    ifstream inputFile;
    
    inputFile.open("students.txt");
    //inputFile >> name1;   for testing make sure the names were being read correctly
    //inputFile >> name2;
    
    
    
    
    for (int count = 1; count <= studentCount; count++) // test as many times as there are students
    {
        inputFile >> name1; // read in 2 names from the file
        inputFile >> name2;
        if(name1[0] > name2[0]) // test the first characters of each ??
        {
          first[40] = name1[40]; // if true name1 is first name2 is second alphabetically
          last[40] = name2[40];
        } else
          {
          first[40] = name2[40]; // if false name2 is first name1 is second alphabetically
          last[40] = name1[40];
          }
    }
    
    cout << first[40];
    cout << last[40];    
    
    system("PAUSE");
    return 0;
}


The output for this is seemingly 2 random characters (assuming the data is not being saved to the variables).

Any insight on how to accomplish this would be appreciated, thanks.
I have a program that gets first names from the user they are stored as char.
No, it's an array of char which is a major different.

you need a loop to compare all the char in that array (for the first char you're detecting a difference you need to break that loop). On line 54 you're only checking the first char.

Since an array starts with 0 your loop should look like this:

for (int count = 0; count < studentCount; count++)
Topic archived. No new replies allowed.