Quantcast

Maximum PC

It is currently Sat Sep 20, 2014 10:39 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: c++ slot machine
PostPosted: Mon May 26, 2008 5:42 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i an thinking of making a slot machine program in c++
here is what it should do. roll slots, randomly generates the number. for everytime you win, you get 4 dollars, every play costs 1 dollar. the random number should be somewhere between 1 to 5. if the random number is all fives the player gets 100 dollars. Here is what i have so far, but its not working. Could you please tell me what to do.

Code:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Slotstart ();
void Slotplay ();

int money;
int maxrand;
int result;
int rannum;
int winnum;

int main()
{
  char name[50];
 
  cout<<"Please enter your name: ";
  cin.getline ( name, 50 );
  cout<<"Welcome to Virtual Slot machine, "<< name  <<" \n";
  cout<<"Press enter to continue\n";
  cin.get();
  Slotstart();
}

void
Slotstart ()
{

     money = 2;
     maxrand = 2;
     
cout<<"Press enter to pull the slot machine.\n";
cout<<"Press ""leave"" anytime to stop playing.\n";

cin.get();
srand( (unsigned)time( NULL ) );
rannum = rand() % maxrand;
Slotplay ();

}

void
Slotplay ()
{
     


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 7:23 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
... um ... finish writing your code?

Actually, before you do that, I strongly recommend designing the entire program in pseudocode .. THEN flesh it out with proper C++ code. That way, you can think your way through the problem before you try to translate it into a language with which you are largely unfamiliar.


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 1:44 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
here it is, but it won't compile, it says something about SlotRestart() can't be used as a funtion or void.

Code:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>

using namespace std;

void SlotRestart;



int slota;
int slotb;
int slotc;


int main()
{
  char name[50];
 
  cout<<"Please enter your name: ";
  cin.getline ( name, 50 );
  cout<<"Welcome to Virtual Slot machine, "<< name  <<" \n";
  cout<<"Press enter to continue\n";
  cin.get();


     
cout<<"Press enter to pull the slot machine.\n";
cout<<"Press ""leave"" anytime to stop playing.\n";
cin.get();

srand(time(0));
    slota=rand()%3;
    slotb=rand()%3;
    slotc=rand()%3;
   
    if(slota==slotb && slotb==slotc && slotc==slota)
    cout<<"You win\n";
   
    else
    cout<<"You lose\n";
    cin.get();
    SlotRestart();
}

void
SlotResult ()
{
        cout<<"Press enter to pull the slot machine.\n";
cout<<"Press ""leave"" anytime to stop playing.\n";
cin.get();

srand(time(0));
    slota=rand()%3;
    slotb=rand()%3;
    slotc=rand()%3;
   
    if(slota==slotb && slotb==slotc && slotc==slota)
    cout<<"You win\n";
    SlotRestart();
   
    else
    cout<<"You lose\n";
    cin.get();
    SlotRestart();
}             




Top
  Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 4:24 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
change void SlotRestart; to void SlotRestart(); line 8

change void SlotResult (){ to void SlotRestart(){ line 47

add curly brackets to the last if block (in SlotRestart()) lines 58 to 66


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 4:48 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i changed it to what mag said, but now it highlights "else" in slot 64. It says:

In funtion 'void SlotRestart()'
expected primary-function before 'else'
expected ';' before 'else'

thanks


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 6:41 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
These are simply syntax errors .. you need to learn to debug these if you intend to do any programming.

What book are you using to learn? Is this for a course?


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 11:44 am 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i am doing this by myself, not for a course, i use the guide on cprogramming.org.


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 11:49 am 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i fixed it by deleting void SlotRestart in line 62, but now if you run it, it will do you lose for about 20 times in a row, and then you win for 5 times in a row, and you lose 20 times in a row, and so on.


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 2:31 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
look over their guide on if structures again

hint, you want
Code:
cout<<"You win\n";
SlotRestart();


to execute if they win

you want
Code:
cout<<"You lose\n";
cin.get();
SlotRestart();

to execute if they lose


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 4:17 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
i want SlotRestart to start no matter what, so they don't have to reopen the program to play the slot machine again.


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 7:02 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Hint: Always use braces with if statements. It makes your life a lot easier and avoids problems like you have currently


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 7:47 pm 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
smartcat99s wrote:
Hint: Always use braces with if statements. It makes your life a lot easier and avoids problems like you have currently


Good advice, at least until you get better with the syntax...


Top
  Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 6:21 pm 
8086
8086

Joined: Thu Aug 09, 2007 2:30 pm
Posts: 53
You know, this would be a good situation for a do...while statement.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group