I am a new C++ programmer, and I am using Dev C++ as an IDE. I wrote a simple first exercise to make sure compilation and execution would go as expected, but of course they didn't. The goal is to average some grades. Here is the linking error I am getting:
c:\documents and settings\jdbutl1\desktop\code\grade average\driver.o: In function 'main':
//c/documents and settings/jdbutl1/desktop/grades/driver.cpp: undefined reference to 'Grades::average(void)'
Here is the driver.cpp that contains the main function:
#include <iostream.h>
#include "grade.h"
int getGrade()
{
int input;
cout << "Please enter a grade: ";
cin >> input;
if((input < 0 )||(input > 100))
{
cout << "Sorry, but that is not a valid score!";
getGrade();
}
return input;
}
void average()
{
int total = 0;
int counter = 0;
int totalAverage;
while(counter<=20)
{
total += getGrade();
counter++;
}
totalAverage = total/counter;
cout << "Average is: " + totalAverage;
}
Thanks for the help guys, and I'm really looking forward to getting away from Java and into C++.
1. Need to change your preprocessor directives and add namespace using.
#include <iostream>
using namespace std;
2. In your main function line 8, you're creating an object of class Grades called myGrades, but you have not defined a Grades class in grade.h.
3. In line 9, you have a function call to your object myGrades member function: average(). Since the Grades class doesn't exist this won't work.
grades.cpp ( did you mean grades.h? If not this cpp file neads to be a header file and if it is the header file grades.h you should not be #including the header file within the header file.
Take a look at some of this code, this accomplishes what you want with 10 grades. It is also not obvious to the user how many grades to enter, it just seems like a loop in your code since there are 20 entries.
// grades.h
#include <iostream>
usingnamespace std;
class Grades
{
public:
// function to prompt for 10 grades using while loop
void getGrade()
{
int total = 0;
int grade;
int counter = 1;
int average = total / 10;
while ( counter <= 10 )
{
cout << "Enter 10 grades to determine the average: ";
cin >> grade;
if (( grade < 0 ) || ( grade > 100 ))
cout << "Invalid score\n";
else
{
total += grade;
counter++;
}
}
cout << "\nThe average grade is: " << total / 10 << "\n";
}
}; // end class
// driver.cpp
#include <iostream>
#include "grades.h"
usingnamespace std;
int main()
{
Grades myGrades; // create an object of class Grades
myGrades.getGrade(); // call the member function of object myGrades
system("pause");
return 0;
}
Hope this helps.
return 0;