C++ Beginner help

I need help with this particular programming assignment. Here goes.

Ask the user how many students are in a class. Using a loop, the application should read in all of the student names. When all the names have been read in the application should report how many students fall into each range A-F, G-K, L-Q, R-V, W-Z. The output should be properly formatted into rows and columns. For example if there are four students, Abraham, Deidra, Billy, and Wyatt. The first column, A-F would have 3 students, column G-K would have 0, column L-Q would have 0, column R-V would have 0, and column W-Z would have 1 student. You can assume the first names will all start with a capital letter and no students will have the same name.

Input Validation: The number of students can not be below 1 or greater than 25.


Here is the code I have written so far. I've made it to the input validation but I don't understand how I group the user input into columns for output.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int studentAmount;
string studentName, firstStudent, lastStudent;

cout << "How many students are in the class?" << endl;

cin >> studentAmount;

while(studentAmount < 1 || studentAmount > 25)
{
cout << "Error. The number of students cannot be below 1 or greater than 25" << endl << "Enter a valid amount."
<< endl;
cin >> studentAmount;
}




return 0;}
When all the names have been read in the application should report how many students fall into each range A-F, G-K, L-Q, R-V, W-Z.


From that statement it appears you only need to count the number of students in each group. It does not say that you have to print a table of the student names.

Perhaps the easiest is to create 5 groups.
1
2
3
4
5
6
 
  int groupAF = 0;
  int groupGK = 0;
  int groupLQ = 0;
  int groupRV = 0;
  int groupWZ = 0;

When you read in a name, determine from the first letter which group to increment.

The first column, A-F would have 3 students,

Now, that seems to imply you are to print the names in a column. That's a different problem.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Thanks...after further dissection I've determined I'm not printing the names in the columns, just the number count. Any advice on how to do this..."When you read in a name, determine from the first letter which group to increment."

Thanks for the help!
Just use a series of if statements to determine the group.

1
2
3
4
5
6
7
  char c = studentName[0];  // Get first character of name
  if (c >= 'A' && c <= 'F')
    groupAF++;
  else if (c >= 'G' && c <= 'K')
    groupGK++;
  else if ...
// keep going for the remaining groups 

Last edited on
Thanks. I'm getting closer. What am I doing wrong?





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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int studentAmount;
    string studentName;
    char value = studentName[0]; // gets first character of student name;
    int AFcount = 0, GKcount = 0, LQcount = 0, RVcount = 0, WZcount = 0; // keeps count of the groups
    
    cout << "How many students are in the class?" << endl;
    
    cin >> studentAmount;
    cin.ignore();
    
    while(studentAmount < 1 || studentAmount > 25)
    {
        cout << "Error. The number of students cannot be below 1 or greater than 25" << endl << "Enter a valid amount."
        << endl;
        cin.ignore();
        cin >> studentAmount;
        
    }
    
    for (int counter = 1; counter <= studentAmount; counter++)
    
    {
        cout << "Enter the name of student " << counter << "." << endl;
    
        getline(cin, studentName);
        
        
    if (value >= 'A' && value <= 'F')
        
        AFcount++;
        
        
    else if (value >= 'G' && value <= 'K')
        
        GKcount++;
        
    else if (value >= 'L' && value <= 'Q')
        
        LQcount++;
        
    else if (value >= 'R' && value <= 'V')
        RVcount++;
    
    else if (value >= 'W' && value <= 'Z')
        
        WZcount++;
    
    }
    
    cout << "Range AF" << endl << AFcount;
    
    
    return 0;}


Whenever I do a test and enter names like Amber, Ashton, etc....it still returns a 0 for Range AF. Please advise.
Last edited on
You never set "value" to anything
char value = studentName[0];

Is that not what that did?
What is the value of studentName[0] at that point? On line 9. Which is before you have entered the student name.
Ahhhhh I see!
Thanks for all the help guys. I truly appreciate it. Here is the final result.


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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    int studentAmount;
    string studentName;
    int AFcount = 0, GKcount = 0, LQcount = 0, RVcount = 0, WZcount = 0; // keeps count of the groups
    
    cout << "How many students are in the class?" << endl;
    
    cin >> studentAmount;
    cin.ignore();
    
    while(studentAmount < 1 || studentAmount > 25)
    {
        cout << "Error. The number of students cannot be below 1 or greater than 25" << endl << "Enter a valid amount."
        << endl;
        cin >> studentAmount;
        cin.ignore();
    }
    
    for (int counter = 1; counter <= studentAmount; counter++)
    
    {
        cout << "Enter the name of student " << counter << "." << endl;
    
        getline(cin, studentName);
    
        char value = studentName[0]; // gets first character of student name;
        
    if (value >= 'A' && value <= 'F')
        
        AFcount++;
        
        
    else if (value >= 'G' && value <= 'K')
        
        GKcount++;
        
    else if (value >= 'L' && value <= 'Q')
        
        LQcount++;
        
    else if (value >= 'R' && value <= 'V')
        RVcount++;
    
    else if (value >= 'W' && value <= 'Z')
        
        WZcount++;
    
    }
    
    cout << setw(12) << "Range A-F" << setw(12) << "Range G-K" << setw(12) << "Range L-Q" << setw(12)
    << "Range R-V" << setw(12) << "Range W-Z"<< endl << setw(12) << AFcount << setw(12) << GKcount
    << setw(12) << LQcount << setw(12) << RVcount << setw(12) << WZcount;
    
    
    return 0;}
Topic archived. No new replies allowed.