Class HW problem

Hello, I am making a program that simulates a slot machine. I think I have the idea right, but I am getting errors mainly on my function prototypes.

I have it divided into 3 files.

Main .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************

.
.
.
.

Main Algorithim:

	#include..
	namespace..
	Class
		Public
			Constuctor 
		         Initilize 12 coins
			# OF COIN Function 
			 Get number of coins left in machine
			PLAY Function
			 Insert coin, random number of winning/losing
		Private
			Function
				Add a coin from user
			Function
				Get Random Number
			Pay out coin if, win/loose

int main

Initilize # of coins in the machine

while coin is > than 0

Call PLAY function
{ INSIDE THE FUCNTION
	Ask user to enter one coin to play y/n
	IF y, continue
	IF n, display coins won, quit
	
	CALL ADD A COIN FUNCTION
	CALL RANDOM FUNCTION
		
	If int returned is >=1 && <=7 then the player wins one coin
	Return true/false	
}

if bool == true
  minus a coin from the machine
  tell user he won 


end main

FUNCTIONS
********************************************/


#include <iostream>
#include "A3BJ.h"
#include <iomanip>
using namespace std;

int main()
{

srand(time(0)); //Seeds random number generator

slotmachine sm;

int randomNumber =0;
int numOfCoins =0;
bool winLose = false;
char yesNo;

while(numOfCoins > 0)
{
 cout << "Enter one coin to play? y/n" << endl;
 cin >> yesNo;
 while( yesNo == 'y' || yesNo == 'Y')
 {
  winLose = slotmachine::play(numOfCoins, randomNumber);
 }

 if(winLose == true)
    {
     numOfCoins = numOfCoins - 1;
     cout << "You've won one coin" << endl;
    }
	else
	{
	 cout << "You lost one coin" << endl;
	}
}
 cout << "You've won all of the coins in the machine!" << endl;
 return 0;
}
// End Main 

ERROR:"A3BJ.cpp", line 80: Error: Use "." or "->" to call slotmachine::play(int, int).


Class .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "A3BJ.h"
#include <iostream>
#include <iomanip>


slotmachine::slotmachine();
{
 numOfCoins = 12;
 randomNumber = 0;
}


bool slotmachine::play(int numOfCoins, int randomNumber);
{
 numOfCoins = addCoin();
 randomNumber = ranNum();

  if(randomNumber >= 1 && randomNumber <=7)
  {
   return true;
  }
     else
     {
      return false;
     }
}


int slotmachine::addCoin()
{
 numOfCoins = numOfCoins + 1;
 return numOfCoins;
}

int slotmachine::ranNum()
{
 int randomNum = rand()%10+1;
 return randomNum; 
}

ERRORS:
"A3BJfunc.cpp", line 6: Error: The class member slotmachine() cannot be declared outside the class.
"A3BJfunc.cpp", line 7: Error: A declaration was expected instead of "{".
"A3BJfunc.cpp", line 10: Error: A declaration was expected instead of "}".
"A3BJfunc.cpp", line 13: Error: The class member play(int, int) cannot be declared outside the class.
"A3BJfunc.cpp", line 14: Error: A declaration was expected instead of "{".
"A3BJfunc.cpp", line 15: Error: Multiple declaration for numOfCoins.
"A3BJfunc.cpp", line 15: Error: The function "addCoin" must have a prototype.
"A3BJfunc.cpp", line 16: Error: Multiple declaration for randomNumber.
"A3BJfunc.cpp", line 16: Error: The function "ranNum" must have a prototype.
"A3BJfunc.cpp", line 18: Error: A declaration was expected instead of "if".
"A3BJfunc.cpp", line 18: Error: ")" expected instead of ">=".
11 Error(s) detected.


header.h
//Contains Class and function prototypes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
using namespace std;

class slotmachine
{
	public:
		slotmachine();
		int numCoin(int numOfCoins); //Number of coins in the machine
		bool play(int numOfCoins, int randomNumber);
	private:
		int addCoin(); //Adds coins to the machine
		int ranNum(); //Finds random Number
};
main line 80 should refer to an instance of slotmachine:
 
winLose = sm.play(numOfCoins, randomNumber);

class.cpp line 6 and 12 should not have semicolons.
class.cpp lines 8 and 9 appear to be setting member variables in slotmachine, but no such variables exist.
class.cpp lines 15 and 16, you setting arguments that were passed in by values. These values will be lost when the function exits.


Okay Guys, I fixed almost everything except my function file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "A3BJ.h"
#include <iostream>
#include <iomanip>


slotmachine::slotmachine();
{
 numOfCoins = 12;
 randomNumber = 0;
}


bool slotmachine::play(int numOfCoins, int randomNumber)
{
 numOfCoins = addCoin();
 randomNumber = ranNum();

  if(randomNumber >= 1 && randomNumber <=7)
  {
   return true;
  }
     else
     {
      return false;
     }
}


int slotmachine::addCoin()
{
 numOfCoins = numOfCoins + 1;
 return numOfCoins;
}

int slotmachine::ranNum()
{
 int randomNum = rand()%10+1;
 return randomNum; 
}


ERRORS:
"A3BJfunc.cpp", line 6: Error: The class member slotmachine() cannot be declared outside the class.
"A3BJfunc.cpp", line 7: Error: A declaration was expected instead of "{".
"A3BJfunc.cpp", line 10: Error: A declaration was expected instead of "}".
The :: operator is called the scope resolution operator. if I had something that resided within the scope of the slotmachine class, i would use this to call it. a very good example of this is the day to day usage of the namespace std:

using namespace std;

If i didn't make this statement, i would need to use the scope resolution operator every time i called a function or class that was within the scope of namespace:

1
2
3
4
5
6
7
//instead of 

cout<<"stuff"<<endl;

//it would be 

std::cout<<"stuff"<<std::endl;


In your code above, you are specifying a function that resides within the global namespace, but is a MEMBER of the slotmachine class. Since it is a member, it gets called differently:
1
2
slotmachine sm;
sm.play(...);


And since the object exists within the local store, and not on the free store, you don't need the -> operator to resolve the pointer.
Okay, I fixed some more issues now, everything is apparently not defined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "A3BJ.h"
#include <iostream>
#include <iomanip>


slotmachine::slotmachine();
{
 numOfCoins = 12;
 randomNumber = 0;
}


bool slotmachine::play(int numOfCoins, int randomNumber)
{
 numOfCoins = addCoin();
 randomNumber = ranNum();

  if(randomNumber >= 1 && randomNumber <=7)
  {
   return true;
  }
     else
     {
      return false;
     }
}


int slotmachine::addCoin()
{
 numOfCoins = numOfCoins + 1;
 return numOfCoins;
}

int slotmachine::ranNum()
{
 int randomNum = rand()%10+1;
 return randomNum; 
}


"A3BJfunc.cpp", line 8: Error: numOfCoins is not defined.
"A3BJfunc.cpp", line 9: Error: randomNumber is not defined.
"A3BJfunc.cpp", line 31: Error: numOfCoins is not defined.
"A3BJfunc.cpp", line 31: Error: numOfCoins is not defined.
"A3BJfunc.cpp", line 32: Error: numOfCoins is not defined.

But I did define it, so do I need to add in an int somewhere?
these variables are defined within the scope of the main(), and not within the class slotmachine, where you expect to use them.
Topic archived. No new replies allowed.