I have been trying to pass a struct, which hold all my vars, to multiple functions, which are saved in a separate class. I know the error has to do with some sort of syntax error, most likely, but I do not see what I have done wrong.
The main.ccp is:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "running.h"
using namespace std;
int main()
{
//------Class Objects---------
running runObj;
//----------Vars--------------
char saveGame = 'N';
struct gameVar
{
int correctGuesses; // These vars need to be reset for each new game.
int Lives;
int rowCorrect;
int highScore;
char anotherGame;
} values;
values.highScore = 12;
values.anotherGame = 'Y';
//--------Game Loop-----------
// int highScore2 = runObj.readHighScore();
while (values.anotherGame = 'Y')
{
struct gameVar = runObj.processGame(gameVar);
struct gameVar = runObj.afterText(gameVar);
gameVar values;
values.anotherGame;
}
cout << endl << "-------------------------------------------------------" << endl;
cout << "Would you like to save your high score? Y/N" << endl;
cin >> saveGame;
if(saveGame == 'Y')
{
runObj.saveHighScore(gameVar);
}
return 0;
}
My header file is:
#ifndef RUNNING_H
#define RUNNING_H
class running
{
public:
struct gameVar processGame(struct gameVar);
void saveHighScore(struct hs);
int readHighScore();
struct gameVar afterText(struct gameVar);
};
#endif // RUNNING_H
First of all, a simple issue: you are using
=in yourwhileloop condition, which will assign the value'Y'togameVar.anotherGame. What you actually want is==, to test for equality.Take a look at this line:
What are you trying to do here?
gameVaris the name of your struct, not an object ofgameVartype. Your object is actually calledvalues. Perhaps you were wanting to do something like:Ditto for the next line too.
It seems like the reason you have this confusion is because you’re defining your
structat the same time as creating an object of that type. ThestructcalledgameVaris just a blueprint for objects and you create an object that matches that blueprint calledvalues:You might be less confused if you define the
structoutside themainfunction as:And then create the instance of it in
mainwith:It is this
valuesobject that you must pass to a function – you can’t pass a type, which is whatgameVaris.I’m not sure what you were then attempting to do with:
This would redefine the
valuesobject within thewhileloop and it will be destroyed at the end of the loop. You then access the data memberanotherGamebut don’t do anything with it. Maybe you’re looking for:It’s worth noting that in C++, you do not need to put
structbefore every use of thegameVartype. The type name is justgameVar. That is, you could change your declaration ofprocessGameto:gameVar processGame(gameVar);