So I'm trying to convert my code into 3 files, the main.cpp, a header, and the classbody.cpp. This was my original code:
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
class colorPicker {
private:
string colorArray[7];
public:
colorPicker() {
colorArray[0] = "Red";
colorArray[1] = "Green";
colorArray[2] = "Purple";
colorArray[3] = "Yellow";
colorArray[4] = "Orange";
colorArray[5] = "Indigo";
colorArray[6] = "Pink";
}
void printAllColors() {
for (int i = 0; i < 7; i++)
{
cout << colorArray[i] << endl;
}
}
string randomColor() {
srand((unsigned)time(0));
int i = 0;
i = rand() % 7;
return colorArray[i];
}
};
int main()
{
colorPicker P;
P.printAllColors();
cout << "Random Color: " << P.randomColor();
system("pause");
return 0;
}
If ran it'll list the colors in random order, now I'm trying to practice making header files. and these are my code:
Main.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include "ColorPicker.h"
int main()
{
ColorPicker P;
P.printAllColors();
cout << "Random Color: " << P.randomColor();
system("pause");
return 0;
}
ColorPicker.h:
#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
#ifndef "COLORPICKER_H"
#endif "COLORPICKER_H"
class colorPicker {
private:
public:
ColorPicker();
};
And Finally, ColorPicker.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <stdio.h>
#include "ColorPicker.h"
ColorPicker::ColorPicker() {
class colorPicker {
private:
string colorArray[7];
public:
colorPicker() {
colorArray[0] = "Red";
colorArray[1] = "Green";
colorArray[2] = "Purple";
colorArray[3] = "Yellow";
colorArray[4] = "Orange";
colorArray[5] = "Indigo";
colorArray[6] = "Pink";
}
void printAllColors() {
for (int i = 0; i < 7; i++)
{
cout << colorArray[i] << endl;
}
}
string randomColor() {
srand((unsigned)time(0));
int i = 0;
i = rand() % 7;
return colorArray[i];
}
};
};
I've decided to do it in this fashion because I watched "Buckys C++ Programming tutorials" and this was on his Header episode. However, my version is not valid, what am I doing wrong? and thank you in advanced!