issue compiling

So im trying to do this simple program that gets 10 students and 5 grades for each. I' am pretty sure I got the main core of it down. The only thing is when I go to compile, it's giving me a compiling error yet no indication of which line is bad. Any help would be much 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
#include <iostream>
#include <string>

using namespace std;

const int AMOUNT = 9;
void showIntro ();
string getNames ();
int getGrades (string);

int main ()
{ 
    int weekendNachos = 0;
    string theNames = " ";
   
    showIntro();
    getNames();
    getGrades(theNames);
    
    cin.ignore();
    cin.get ();
    
}

void showIntro()
{
     
     cout << "Hello there user, simply input the name of your student.";
     cout<< "\n";
     cout<< "\n";
     cout<< "Once finished go ahead and input the 5 grades you have for the student.\n \n";
     cout<< "The program will get the average and letter grade of the student, \n";
     cout<< "of course dropping the lowest grade from the students average."; 
     cout<< "\n";
     cout<< "Just remember, you only have 10 slots for the students.\n \n";

}

string getNames ()
{
       int weekendBeer = 0;
       
       string myNames [AMOUNT];
       
       while ( weekendBeer < 10)
       {
       weekendBeer++;      
       cout << "Enter the students name ";
       cin >> myNames [weekendBeer];
       }
}

int getGrades (string myNames[AMOUNT])
{
    
    int theGrades [AMOUNT];
    int weekendBoobs = 0;
    while (weekendBoobs < 10)
    {
    weekendBoobs++;
    cout<< "Now please enter the 5 grades for " << myNames[weekendBoobs];
    cin >> theGrades [weekendBoobs];
    }
}
Well what's the error?
[Linker error] undefined reference to `getGrades(std::string)'

Something wrong with my getGrades function.
It's not a compiler error. It compiles fine. It's a linker error. There is no function getGrades that accepts a single string, but the code tries to call such a function here:

getGrades(theNames);
Best variable names ever.....

Anyways here are a couple of pointers: 0xf938e3fd, 0xe2561f24a, 0x14e35ab1;

Nah just kidding. Try the following:

1. Replace line 17 with: theNames = getNames(); Pass the string through to a variable.
2. Replace line 43 with: string myNames; No array nessesary
3. Replace line 53 with: int getGrades(string myNames) No array nessesary
4. Replace line 56 with: int theGrades[10]; 9 Will not work because you call theGrades[9] later. (undeclared memory, this will fail at runtime).
5. Add after line63: return theGrades[0] An int function must return an int variable.
Last edited on
Thanks for the help guys. So I've made the adjusments, but now I' am getting a compiler error that says that I have too few arguments in the int getGrades function on line 18. What I' am trying to understand is why I dont put a string variable in the arguments for getGrades so I can use each students name.

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
#include <iostream>
#include <string>

using namespace std;

const int AMOUNT = 9;
void showIntro ();
string getNames ();
int getGrades (string);

int main ()
{ 
    int weekendNachos = 0;
    string theNames = " ";
   
    showIntro();
    theNames = getNames();
    getGrades();
    

    
    cin.ignore();
    cin.get ();
    
}


void showIntro()
{
     
     cout << "Hello there user, simply input the name of your student.";
     cout<< "\n";
     cout<< "\n";
     cout<< "Once finished go ahead and input the 5 grades you have for the student.\n \n";
     cout<< "The program will get the average and letter grade of the student, \n";
     cout<< "of course dropping the lowest grade from the students average."; 
     cout<< "\n";
     cout<< "Just remember, you only have 10 slots for the students.\n \n";

}

string getNames ()
{
       int weekendBeer = 0;
       
       string myNames;
       
       while ( weekendBeer < 10)
       {
       weekendBeer++;      
       cout << "Enter the students name ";
       cin >> myNames [weekendBeer];
       }
}

int getGrades (string myNames)
{
    
    int theGrades [10];
    int weekendBoobs = 0;
    while (weekendBoobs < 10)
    {
    weekendBoobs++;
    cout<< "Now please enter the 5 grades for " << myNames[weekendBoobs];
    cin >> theGrades [weekendBoobs];
    return theGrades [0];
    }
}
Your function getGrades takes a string.

int getGrades (string myNames)

See. One input parameter. A string.

This means that when you call it, you must give it one parameter. A string.


This attempt to call it passes no parameters.
getGrades();
What Moschops is saying is replace line 18 with the following:
getGrades(theNames);
Your code is all very confused. You try to treat myNames as an array, but your string is just a string, not an array of strings. I think you need to stop, step back, and think about what you want to do rather tan just hack at the code.
Last edited on
Thanks for the help guys and yea Moschops I was just in the middle of editing everything so there were a couple things missing, like declaring the arrays properly.

I' am just having a slight problem with this while loop function that repeats twice, any ideas?
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
int main ()
{ 
    int weekendNachos = 0;
    string theNames[10] = " ";
   
    showIntro();
    getNames();
    getGrades(theNames[10]);
    
    cin.ignore();
    cin.get ();
    
}
void showIntro()
{
     
     cout << "Hello there user, simply input the name of your student.";
     cout<< "\n";
     cout<< "\n";
     cout<< "Once finished go ahead and input the 5 grades you have for the student.\n \n";
     cout<< "The program will get the average and letter grade of the student, \n";
     cout<< "of course dropping the lowest grade from the students average."; 
     cout<< "\n";
     cout<< "Just remember, you only have 10 slots for the students.\n \n";

}
string getNames ()
{
       int weekendBeer = 0;
       
       string myNames[10];
       
       while ( weekendBeer <= 10)
       {
       cout << "Enter the students name ";
       cin >> myNames [weekendBeer];
       cout << "\n";
       weekendBeer++;   
       }
}
It doesn't repeat twice, it repeats eleven times. The final time it attempts to write to myNames[10] which does not exist and causes a segFault (if you're lucky - if you're unlucky it trashes data and you have no way of knowing).
Last edited on
But, what do you mean it doesn't exist, I have the variable there with the number of arrays I want. It simply just repeats the question "Enter the students name" twice.
Array index begins with zero.
So in your example your theNames variable has indexes from 0 to 9 (sum 10 items).

Try to make a for-loop.

The line "cout << "\n";" is unnecessary, because the user enters a line feed after typing in the name.

Here is a sample output.

j@j-desktop:~/badCode$ ./a.out 
Hello there user, simply input the name of your student.

Once finished go ahead and input the 5 grades you have for the student.
 
The program will get the average and letter grade of the student, 
of course dropping the lowest grade from the students average.
Just remember, you only have 10 slots for the students.
 
Enter the students name a

Enter the students name b

Enter the students name c

Enter the students name d

Enter the students name e

Enter the students name f

Enter the students name g

Enter the students name h

Enter the students name i

Enter the students name j

Enter the students name k
Segmentation fault


using the following code:

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
#include <iostream>
#include <string>

using namespace std;
void showIntro()
{
     
     cout << "Hello there user, simply input the name of your student.";
     cout<< "\n";
     cout<< "\n";
     cout<< "Once finished go ahead and input the 5 grades you have for the student.\n \n";
     cout<< "The program will get the average and letter grade of the student, \n";
     cout<< "of course dropping the lowest grade from the students average."; 
     cout<< "\n";
     cout<< "Just remember, you only have 10 slots for the students.\n \n";

}
string getNames ()
{
       int weekendBeer = 0;
       
       string myNames[10];
       
       while ( weekendBeer <= 10)
       {
       cout << "Enter the students name ";
       cin >> myNames [weekendBeer];
       cout << "\n";
       weekendBeer++;   
       }
}

int main ()
{ 
    int weekendNachos = 0;
    string theNames[10] = " ";
   
    showIntro();
    getNames();
    //   getGrades(theNames[10]);
    
    cin.ignore();
    cin.get ();
    
}
Last edited on
Ok so I' am trying to wrap my head around this, what memory is the program trying to access that isn't available. I have declared the variables and arrays correctly.
You declare an array like this:

string myNames[10];

This creates an array big enough for ten string objects. Here is a list of all those elements:

1
2
3
4
5
6
7
8
9
10
myNames[0];
myNames[1];
myNames[2];
myNames[3];
myNames[4];
myNames[5];
myNames[6];
myNames[7];
myNames[8];
myNames[9];


As you can see, there are ten of them.

You then try to write into the array like this:
cin >> myNames [weekendBeer];

where weekendBeer takes the values 0,1,2,3,4,5,6,7,8,9,10

Let's write those elements out:

1
2
3
4
5
6
7
8
9
10
11
myNames[0];
myNames[1];
myNames[2];
myNames[3];
myNames[4];
myNames[5];
myNames[6];
myNames[7];
myNames[8];
myNames[9];
myNames[10];


There are eleven of them. But there is only space in the array for ten. Oh no. You are trying to write to myNames[10], which is off the end of your array. It is memory that does not belong to you.
Moschops thank you so much for taking the time to help me understand the issue. You were much help.
Topic archived. No new replies allowed.