Sort List of Names (Sort, Array, Strings)

Hello All!

So task is two-old. I must ask the user for the number of students, which can be from 1-25. I must then ask for their first names, and sort the list alphabetically

I have created the cases first, that is, if the students are Zero, a single student and then more than one, but less than 25, and above 25.

I understand I could eliminate the single student case, but it's a unique situation, so I wanted to make a case for it.

After I figure out the cases, I will try to get an array setup to sort. But one thing at a time for me.

So right now, I don't even know how to properly initialize string of loneStudent. The following errors out. So here are a couple question to start.

1. How do I properly initialize the string of "loneStudent" for the single-student case?

2. How I do I go back to a specific line of code. I only want to write a single array sorting function, and have it referenced where appropriate.

3. Do you need anything else wrong? Am I going about this the wrong way?

Thank you!




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
// This program asks for a number of students in a class, then asks for their first names, then sorts the list alpahbetically  

#include <iostream>
#include <string.h>

using std::cin;
using std::cout;

int main()
{

    int    countStudent = 0;                                             // define variables
    


    cout << "Please enter number of students.""\n";   // We first ask how many students.
    
    cin >> countStudent;
        
    if (countStudent == 0) {
        
        cout << "You have no students!" << "\n";
        cin.ignore();
       
        return 0;
        
    }
    
    else if (countStudent == 1) {
        
        cout << "What is the name of the student?" "\n";
        cin << loneStudent
        cout << "You only have a single student, nothing to sort, I will just repeat the name back!" << "\n";
        cout << " <loneStudent>" "\n";
        cin.ignore();
       
        return 0;
        
    }
    else if (countStudent > 1 && countStudent < 25) {
        
        cout << "What is the name of the student?" "\n";
        cin << loneStudent
        cout << "You only have a single student, nothing to sort, I will just repeat the name back!" << "\n";
        cout << " <loneStudent>" "\n";
        cin.ignore();
       
        return 0;
        
    }

    else (countStudent >= 25){
        
        cout << "Too many students, try again!" "\n";
        
        cin.ignore();
       
        return 0;
        
    }
        
    }
    
// Added Pause before ending program to allow output to display

cin.ignore();

return 0;
}
Simply do cin >> loneStudent to put the value inside.

For the multiple students case, I dont see an array, so how do you plan on saving the student names?
The compiler says I need to either declare of initialize the value. How do I do this?

1. How do I properly initialize or decalre the string of "loneStudent" for the single-student case?

I haven't gotten as far as the array yet. Gotta get this right first, then I can setup the array.
Last edited on
Declare: string loneStudent

Initialize with input from user: cin >> loneStudent
Last edited on
Thank you for advice on \n". I always saw it in the book as "\n". Ok, I got the cases down I think, now on the array. I removed the single student part, as I realized it's pointless.

How do I assign the cin to the array, do I just push it and stack it until the array is full?

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
// This program asks for a number of students in a class, then asks for their first names, then sorts the list alphabetically    

#include <iostream>
#include <string.h>

using std::cin;
using std::cout;
using std::string;

int main()
{

    int    countStudent = 0;                                             // define variables
    string loneStudent;


    cout << "Please enter number of students.\n";   // We first ask how many students.
    
    cin >> countStudent;
    
    int *classArray = new int[countStudent];
    
    if ( countStudent > 0 && < 26) {
        
        cout << "You have no students!\n";
        cin.ignore();
       
        return 0;
        
    }
        
    else if ( countStudent == 0 ) {
        
        cout << "You have no students!\n";
        cin.ignore();
       
        return 0;
        
    }
    
    else if ( countStudent > 25 ) {
        
        cout << "Too many students, try again!\n";
        
        cin.ignore();
       
        return 0;
        
    }
        
}
Last edited on
Line 43: change else to else if

By the way, you don't need to put the newline character separately like this:
cout << "Please enter number of students.""\n";.

You can have it directly after the text, within the first pair of quotes, like this:
cout << "Please enter number of students.\n";
(6)
Thank you for advice on \n". I always saw it in the book as "\n". Ok, I got the cases down I think, now on the array. I removed the single student part, as I realized it's pointless.

How do I assign the cin to the array, do I just push it and stack it until the array is full?
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
// This program asks for a number of students in a class, then asks for their first names, then sorts the list alphabetically    

#include <iostream>
#include <string.h>

using std::cin;
using std::cout;
using std::string;

int main()
{

    int    countStudent = 0;                                             // define variables
    string loneStudent;


    cout << "Please enter number of students.\n";   // We first ask how many students.
    
    cin >> countStudent;
    
    int *classArray = new int[countStudent];
    
    if ( countStudent > 0 && < 26) {
        
        cout << "You have no students!\n";
        cin.ignore();
       
        return 0;
        
    }
        
    else if ( countStudent == 0 ) {
        
        cout << "You have no students!\n";
        cin.ignore();
       
        return 0;
        
    }
    
    else if ( countStudent > 25 ) {
        
        cout << "Too many students, try again!\n";
        
        cin.ignore();
       
        return 0;
        
    }
        
}
Line 23: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Your cout at line 25 doesn't match your condition. Presumably, 1-25 students is the normal class size and you would want to prompt for multiple students.
Topic archived. No new replies allowed.