Ok i'll show u my problem, i have two cpp files and two header files.
definitions.h :
Code:
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#define OK 1
#define FAILED 0
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/*Prototypes*/
void moveAtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void moveAtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void moveBtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void moveBtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void moveCtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void moveCtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
class CPeg{
public:
CPeg(vector<int> pStack,const string& itsName):
pegStack(pStack),name(itsName)
{}
int addRing(int rSize){
if(pegStack.size() > 0){
if(rSize < pegStack.back()){
pegStack.push_back(rSize);
return OK;
}else
return FAILED;
}else{
pegStack.push_back(rSize);
return OK;
}
}
int getTop(){ return pegStack.back(); }
int stackSize(){ return pegStack.size(); }
void removeRing(){ pegStack.pop_back(); }
void showStack(){
int i;
for(i = pegStack.size() - 1; i >= 0; i--)
printf("%5d\n",pegStack[i]);
}
private:
vector<int> pegStack;
string name;
};
class CDifficulty{
public:
CDifficulty(const char* itsSetting = "",const int nRings = 3):
setting(itsSetting),numRings(nRings)
{}
CDifficulty(const string& itsSetting,int nRings):
setting(itsSetting),numRings(nRings)
{}
int setDifficulty(const string itsSetting){
setting = itsSetting;
if(setting == "easy"){
numRings = 3;
return OK;
}else if(setting == "medium"){
numRings = 5;
return OK;
}else if(setting == "hard"){
numRings = 7;
return OK;
}else
return FAILED;
}
string getDifficulty(){ return setting; }
private:
string setting;
int numRings;
};
struct gameStruct{
int noMoves;
int minMoves;
CPeg* A;
CPeg* B;
CPeg* C;
CDifficulty* diff;
};
#endif
interface.h :
Code:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <stdlib.h>
#include <iostream>
using namespace std;
COption* mainMenu();
COption* diffMenu();
COption* playMenu();
void createRings(struct gameStruct hanoiGame);
void showPegs(struct gameStruct hanoiGame);
void mainSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void diffSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void playSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
void clearPegs(struct gameStruct hanoiGame);
class COption{
public:
COption(const char* itsName = "",const int optNumber = 0):
name(itsName),number(optNumber)
{}
COption(const string& itsName,const int optNumber):
name(itsName),number(optNumber)
{}
string getName(){ return name;}
int getNumber(){ return number; }
private:
int number;
string name;
};
class CMenu{
public:
CMenu(const char* itsName = ""):
mName(itsName),noOptions(0)
{}
CMenu(const string& itsName,COption* optList,const int numOptions):
mName(itsName),o_array(optList),noOptions(numOptions)
{}
void showMenu(){
cout << "#### " << mName << " ####" << endl;
for(int i = 0; i<noOptions; i++)
cout << o_array[i].getNumber() << ". "
<< o_array[i].getName() << endl;
cout << "\nPlease choose an option: ";
}
private:
string mName;
int noOptions;
COption* o_array;
};
struct menuStruct{
CMenu* mainMenu;
CMenu* diffMenu;
CMenu* playMenu;
};
#endif
hanoi.cpp :
Code:
#include "definitions.h"
#include "interface.h"
int main(){
vector<int> pStack;
menuStruct hanoiMenus;
gameStruct hanoiGame;
/*Create the pegs*/
hanoiGame.A = new CPeg(pStack,"A");
hanoiGame.B = new CPeg(pStack,"B");
hanoiGame.C = new CPeg(pStack,"C");
/*Set up the move count*/
hanoiGame.noMoves = 0;
hanoiGame.minMoves = 7;
/*Create the difficulty*/
hanoiGame.diff = new CDifficulty("easy",3);
/*Create menu options*/
COption* mainOpts = mainMenu();
COption* diffOpts = diffMenu();
COption* playOpts = playMenu();
/*Create Menus*/
hanoiMenus.mainMenu = new CMenu("MAIN MENU",mainOpts,3);
hanoiMenus.diffMenu = new CMenu("DIFFICULTY MENU",diffOpts,4);
hanoiMenus.playMenu = new CMenu("PLAY MENU",playOpts,7);
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
delete[] mainOpts;
delete[] diffOpts;
delete[] playOpts;
return 0;
}
void moveAtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.A->getTop();
if(hanoiGame.B->stackSize() == 0){
hanoiGame.A->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.B->getTop()){
hanoiGame.A->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.B->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
void moveAtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.A->getTop();
if(hanoiGame.C->stackSize() == 0){
hanoiGame.A->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.C->getTop()){
hanoiGame.A->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.C->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
void moveBtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.B->getTop();
if(hanoiGame.A->stackSize() == 0){
hanoiGame.B->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.A->getTop()){
hanoiGame.B->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.A->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
void moveBtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.B->getTop();
if(hanoiGame.C->stackSize() == 0){
hanoiGame.B->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.C->getTop()){
hanoiGame.B->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.C->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
void moveCtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.C->getTop();
if(hanoiGame.A->stackSize() == 0){
hanoiGame.C->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.A->getTop()){
hanoiGame.C->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.A->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
void moveCtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int temp;
temp = hanoiGame.C->getTop();
if(hanoiGame.B->stackSize() == 0){
hanoiGame.C->removeRing();
hanoiGame.noMoves++;
}
else if(temp < hanoiGame.B->getTop()){
hanoiGame.C->removeRing();
hanoiGame.noMoves++;
}
hanoiGame.B->addRing(temp);
system("CLS");
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
}
interface.cpp
Code:
#include "interface.h"
COption* mainMenu(){
COption* o_array = new COption[3];
COption num1("Choose Difficulty",1);
COption num2("Play game",2);
COption num3("Exit",3);
o_array[0] = num1;
o_array[1] = num2;
o_array[2] = num3;
return o_array;
}
COption* diffMenu(){
COption* o_array = new COption[4];
COption num1("Easy",1);
COption num2("Medium",2);
COption num3("Hard",3);
COption num4("Main menu",4);
o_array[0] = num1;
o_array[1] = num2;
o_array[2] = num3;
o_array[3] = num4;
return o_array;
}
COption* playMenu(){
COption* o_array = new COption[7];
COption num1("Move From A to B",1);
COption num2("Move From A to C",2);
COption num3("Move From B to A",3);
COption num4("Move From B to C",4);
COption num5("Move From C to A",5);
COption num6("Move From C to B",6);
COption num7("Main Menu",7);
o_array[0] = num1;
o_array[1] = num2;
o_array[2] = num3;
o_array[3] = num4;
o_array[4] = num5;
o_array[5] = num6;
o_array[6] = num7;
return o_array;
}
void createRings(struct gameStruct hanoiGame){
int i;
if(hanoiGame.diff->getDifficulty() == "easy"){
for(i = 3; i>0; i--)
hanoiGame.A->addRing(i);
}else if(hanoiGame.diff->getDifficulty() == "medium"){
for(i = 5; i>0; i--)
hanoiGame.A->addRing(i);
}else if(hanoiGame.diff->getDifficulty() == "hard"){
for(i = 7; i>0; i--)
hanoiGame.A->addRing(i);
}
}
void showPegs(struct gameStruct hanoiGame){
cout << "PEG A\n";
hanoiGame.A->showStack();
cout << "\nPEG B\n";
hanoiGame.B->showStack();
cout << "\nPEG C\n";
hanoiGame.C->showStack();
cout << "\n\nNumber of moves: " << hanoiGame.noMoves;
cout << "\nMinimum no. moves: " << hanoiGame.minMoves;
cout << "\n\n";
}
void mainSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int choice = 0;
cin >> choice;
switch(choice){
case 1:
system("CLS");
hanoiMenus.diffMenu->showMenu();
diffSelect(hanoiMenus,hanoiGame);
break;
case 2:
system("CLS");
createRings(hanoiGame);
showPegs(hanoiGame);
hanoiMenus.playMenu->showMenu();
playSelect(hanoiMenus,hanoiGame);
break;
case 3:
exit(0);
break;
default:
cout << "Invalid";
break;
}
}
void diffSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int choice = 0;
cin >> choice;
switch(choice){
case 1:
hanoiGame.minMoves = 7;
if((hanoiGame.diff->setDifficulty("easy")) != OK){
cout << "WE MAY HAVE A PROBLEM";
break;
}
system("CLS");
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
break;
case 2:
hanoiGame.minMoves = 31;
if((hanoiGame.diff->setDifficulty("medium")) != OK){
cout << "WE MAY HAVE A PROBLEM";
break;
}
system("CLS");
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
break;
case 3:
hanoiGame.minMoves = 127;
if((hanoiGame.diff->setDifficulty("hard")) != OK){
cout << "WE MAY HAVE A PROBLEM";
break;
}
system("CLS");
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
break;
case 4:
system("CLS");
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
break;
default:
cout << "Invalid";
break;
}
}
void playSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
int choice = 0;
cin >> choice;
switch(choice){
case 1:
moveAtoB(hanoiMenus,hanoiGame);
break;
case 2:
moveAtoC(hanoiMenus,hanoiGame);
break;
case 3:
moveBtoA(hanoiMenus,hanoiGame);
break;
case 4:
moveBtoC(hanoiMenus,hanoiGame);
break;
case 5:
moveCtoA(hanoiMenus,hanoiGame);
break;
case 6:
moveCtoB(hanoiMenus,hanoiGame);
break;
case 7:
system("CLS");
clearPegs(hanoiGame);
hanoiGame.noMoves = 0;
hanoiMenus.mainMenu->showMenu();
mainSelect(hanoiMenus,hanoiGame);
break;
default:
cout << "Invalid";
break;
}
}
void clearPegs(struct gameStruct hanoiGame){
int temp;
int i;
/*Clear A*/
temp = hanoiGame.A->stackSize();
if(temp > 0){
for(i = temp; i>0; i--)
hanoiGame.A->removeRing();
}
/*Clear B*/
temp = hanoiGame.B->stackSize();
if(temp > 0){
for(i = temp; i>0; i--)
hanoiGame.B->removeRing();
}
/*Clear C*/
temp = hanoiGame.C->stackSize();
if(temp > 0){
for(i = temp; i>0; i--)
hanoiGame.C->removeRing();
}
}
I get like 127 errors now that i've divided the code up, heres the first few.
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2143: syntax error : missing ';' before '*'
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2501: 'COption' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2501: 'mainMenu' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2143: syntax error : missing ';' before '*'
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2501: 'COption' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2086: 'COption' : redefinition
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2501: 'diffMenu' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2143: syntax error : missing ';' before '*'
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2501: 'COption' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2086: 'COption' : redefinition
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2501: 'playMenu' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(38) : error C2629: unexpected 'class CMenu ('
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(38) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2143: syntax error : missing ';' before '*'
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2501: 'COption' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2501: 'o_array' : missing storage-class or type specifiers
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(22) : error C2065: 'mainOpts' : undeclared identifier
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(22) : error C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(23) : error C2065: 'diffOpts' : undeclared identifier
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(23) : error C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast