### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````/******************************************* . . . . 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 #include "A3BJ.h" #include 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
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include "A3BJ.h" #include #include 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.

//Contains Class and function prototypes
 ``1234567891011121314`` ``````#include #include 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include "A3BJ.h" #include #include 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:

 ``1234567`` ``````//instead of cout<<"stuff"<

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:
 ``12`` ``````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.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include "A3BJ.h" #include #include 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.