The problem I'm having is when i try to set some of my class variables within an array of 3 class objects, I get an error stating that the variables required by the function are not declared within the scope of the main function.
I'm still new to classes and separating my interface from my implementation and I do find it to be confusing at times.
I have been looking at some other examples on this site and others and I can't figure out exactly what my issue is and how to fix it.
interface.h file:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std; // I know this is a programming sin to include inside a header file but my textbook wants me to do it this way
class student
{
public:
student(); // class constructor
void setstudent(string studentName, string ID, string studentNumber, string diploma); // Function im using to set objects in array object
private:
string studentName;
string ID;
string studentNumber; // class variables
string diploma;
int averageMark;
string codes [5];
int marks [5];
};
#endif // INTERFACE_H
implementation.cpp file:
#include <iostream>
#include "interface.h"
using namespace std;
student::student()
{
studentName = "";
ID = "";
studentNumber = ""; // constructor to initialise all class variables
diploma = "";
averageMark = 0;
codes [5] = {"", "", "", "", ""};
marks [5] = {0, 0, 0, 0, 0};
}
void student::setstudent(string studentName, string ID, string studentNumber, string diploma) // function to set class variables
{
cout << "Please enter a student name" << endl;
cin.getline(studentName);
cout << "Please enter a unique student ID" << endl;
cin.getline(ID);
cout << "Please enter a unique student number" << endl;
cin.getline(studentNumber);
cout << "Please enter diploma name" << endl;
cin.getline(diploma)
if (diploma == "Garden Design")
{
codes[5] = {"G1","G2","G3","G4","G5"};
}
else if (diploma == "Gourmet Cooking")
{
codes[5] = {"C1","C2","C3","C4","C5"};
}
}
main.cpp file:
#include <iostream>
#include "interface.h"
using namespace std;
int main()
{
student studentDetails[3]; // Creation of class array object to store 3 objects / Maybe ive declared my array to hold 3 class objects incorrectly?
for (int i = 0; i < 3; i++)
{
studentDetails[i].setstudent(studentName, ID, studentNumber, diploma); // function call to set class variables for the objects
}
return 0;
}
If anyone can please point out what I've done wrong and point me in the right direction would be very appreciated.
Error by CodeBlocks:
||=== Build: Debug in student (compiler: GNU GCC Compiler) ===|
C:\Users\nicbe\Desktop\Student\student\main.cpp||In function 'int main()':|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentName' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'ID' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentNumber' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'diploma' was not declared in this scope|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|