Jump to content
  • Advertisement
Sign in to follow this  
Lenny1986

C++ console game

This topic is 917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys i am learning c++ out of hobby. I`ve started making small console games to get to know the language better. I made this game where i get a starnge behaviuor that i can`t explain nor can i overcome. Seems that my variable 'amount' is geting overriden by the last object of the array 'slots' when i run the 'roll' function after getting the value for amount using cin. I also tried using getline and stringstream stoping getline on '\n' but still the same result :

 

#?include? <iostream>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
void roll(int slots[], int size)
{
    srand(time(NULL));
    for (int i=0;i<size;i++)
    {
        slots=rand()%9;
    }
}
int main()
{
    int money=100;
    int amount;
    int slots[2];
    char play='y';
    cout<<"\t\t**** Welcome to Lenny Slots ****"<<endl<<endl;
    cout<<endl;
    cout<<"\t* * *"<<endl;
    cout<<"\t\tDo you wanna play?";
    cin>>play;
    cin.get();
 
    while(play=='y')
    {
        cout<<"\nYou have "<<money<<" dollars!"<<endl;
        cout<<"How much do you wanna bet?"<<endl;
        cin>>amount;
        cout<<"Rolling..."<<endl;
        roll(slots,3);
        cout<<"\t"<<slots[0]<<" "<<slots[1]<<" "<<slots[2]<<endl;
        if ((slots[0]==slots[1]) && (slots[1]==slots[2]))
        {
            money+=amount;
            cout<<"Congrats!!! ***winner winner chicken dinner***"<<endl;
        }
        else
        {
            money-=amount;
            cout<<"Sorry, better luck next time"<<endl;
        }
        cout<<"Your bank roll is "<<money<<endl;
        cout<<endl;
        cout<<"Play again?(y/n): ";
        cin>>play;
        cin.get();
    }

}

Share this post


Link to post
Share on other sites
Advertisement

Thanks a lot that did it. I`m not sure why cin.get() is there , tried lots of stuff and probably i forgot to delete that line. Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!