Array & Functions for Student Gradebook

I realize I have a few problems with my code, but my main problem is in line 23. I want to say, "If the user inputs anything but "KEY", run this code". The complaint Xcode is giving me is "Comparison between pointer and integer ('int' and 'const char')".

Another problem that I'm having is that it's currently only allowing one name and one grade, followed by one name & one grade. I need one name, followed by up to 18 grades, then the option to input more names & grades.
I'd originally included ifstream & iomanip so I could input a text file containing students & format the output nicely, but I haven't gotten that far yet.

Any help would be greatly appreciated!

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
71
72
73
#include <iostream>
#include <ifstream>
#include <iomanip>
#include <string.h>
using namespace std;

const unsigned int numberOfStude = 30; //Max # of students
const unsigned int studeNameLength = 80; //Max # of characters in name
const unsigned int studeGradeLength = 18; //Max # of student grades
string studeName[numberOfStude][studeNameLength];
float studeGrade[numberOfStude][studeGradeLength];
int currentName=0;//Student currently being input
int gradeCount=0; //Count # of grades put in for single student

void getNameGrade(char studeName[numberOfStude][studeNameLength],float studeGrade[numberOfStude][studeGradeLength])

{
    for(int gradeCount=0; gradeCount<studeGradeLength;gradeCount++)
    {
        cout << "Please enter a student name in the following format first_last: ";
        char studeName;
        cin >> studeName;
        if (studeName != "KEY"){
            //studeName[numberOfStude];
            currentName++;
            float studeGrade;
            cout << "Please enter a grade: ";
            cin >> studeGrade;
            if(gradeCount<studeGradeLength)
            {
                //studeGrade[studeGradeLength];
                gradeCount++;
            }
            
            else
            {
                break;
            }
        }
        
        else
        {
            break;
        }
        
        
    }
}


void printNameGrade()
{
    cout<<endl<<"Grades"<<endl;
    
    for(int i=0;i<currentName;i++)
    {
        cout<<studeName[i]<<": "<<studeGrade[i]<<endl;
    }
}


int main() {
    
    
    getNameGrade(studeName,studeGrade);
    printNameGrade();
    
    return 0;
    
    

}
Last edited on
This #include <ifstream> Doesnt exist. Its #include <fstream>


1
2
3
 char studeName;
 cin >> studeName;
 if (studeName != "KEY")


Why is studeName a char? char is used for single characters. A,B,C,D etc, single characters. It should be string if you want a name right? You're getting the error becuase,

studeName != "KEY"

You're comparing a character and a string, you cant do that, cant compare two things that are not of the same type.
AhHA! Thank you so much! That solved one of my problems.
The other one is that my main function is claiming that my getNameGrade function doesn't exist. What did I do wrong there?
Problems on lines 4, 10, 15, 22, 23, 65.

In line 10 you create a global variable that is a 2D array of strings.

In line 4, the library you're using is string.h, AKA cstring, which do not have strings.

In line 15, you provide a studeName parameter which is of type char**.

In line 22, you create a local (to the loop) studeName variable which is char.

In line 23, you try to compare a char* to a char.

In line 65, you are trying to call function getNameGrade(string**, float**) which is not defined.
Ok, I understood some of that (forgive me! this is my first programming class and I'm old!)
I see part of the problem- I wanted my studeName array to be the input in the first function, but I re-defined studeName as a string in the function. So, I need to figure out how to get the student's name into the array and still be able to tell it to terminate when the user types "KEY".

In line 23, do I need to convert the char* to a string?

I'm still not understanding the problem with line 65, though. Why is that function not defined? What am I missing?
OK, I'll try to explain the problems in more detail.

On line 10, you create a global variable: string studeName[numberOfStude][studeNameLength];

This variable is a 2D array of type string (an array of string arrays).

On line 4, you are using the library <string.h> (AKA <cstring>). This is not the library for C++ strings, but for C strings (character arrays).

That means that the code on line 10 is illegal, because string is an undefined type. (It's defined in the library <string>.)

On line 15, you have void getNameGrade(char studeName[numberOfStude][studeNameLength],float studeGrade[numberOfStude][studeGradeLength]).

That means your getNameGrade function is supposed to accept 2 parameters: a 2D array of type char (char**) and a 2D array of type float (float**). So your function is getNameGrade(char**, float**).

On line 22, you create a new char variable named studeName. This is not illegal, because of scope. I'll let you look that up if you're curious.

On line 23, you have if (studeName != "KEY"){. The "KEY" is of type char*. In this case, studeName is the studeName you declared on line 22 and not the parameter on line 15, because local scope has priority.

That means you are trying to compare a char to a char*. (The compiler gives the error that you are trying to compare an int to a char* because the internal representation of a char is basically the same as an int.)

On line 65, you have getNameGrade(studeName,studeGrade);. That is in your main function, where you haven't declared any variables. So that means studeName in this line is referring to the global string** variable studeName.

That also means you are trying to call the function getNameGrade(string**, float**). Remember that the function on line 15 is getNameGrade(char**, float**). These are two different functions. The function getNameGrade(string**, float**) is not defined.
Topic archived. No new replies allowed.