I need to make a SIMPLE BASIC program that plays crabs

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

int main()
{
	int money, i, wager, roll, roll1, roll2;
	string name; 
	char bet, R;
	cout <<"Lets Play Craps!" << endl;
	cout << endl;
	cout <<"This program stimulates a session at a craps table!"<< endl;
	cout << endl;
	cout <<"What's your first name , slick? ";
	cin >> name;
	cout <<"How much money do you have? ";
	cin >> money;
	cout << endl;
	cout <<"Round 1";
	cout << endl;
	cout <<"Bet on Pass(P) or Don't Pass(D) ?";
	cin >> bet;
	cout << endl;
	cout << "How much do you wager? ";
	cin >> wager;
	while (wager > money)
	{
		cout <<"Error, enter a smaller amount.";
		cout << endl;
		cout <<"How much do you wager?";
		cin >> wager;
	}
	roll1 = (rand() + time(0)) % 6 + 1;
	roll2 = (rand() + time(0)) % 6 + 1;
	roll = roll1 + roll2;
	cout <<"Enter R to roll: ";
	cin >> R;
	cout << endl;
	cout << "Roll is " << roll;
	cout << roll << " is the point!";
	cout << roll;
	cout << roll;

	
	system("pause");
	return 0;
}

Recommended Answers

All 13 Replies

I never heard of the game crabs, but what is the question/problem with the code you posted?

If you problem is that you are getting the same number every time it is because you are never calling srand()

How would I get the computer to make a roll between 1and 6

before calling rand() you need to use srand(). something like this should help

srand(unsigned(time(NULL)));
//...
number = (rand() % 6) + 1;
#include<iostream>
#include<string>
#include<ctime>
using namespace std;

int main()
{
	int money, i, x,wager, secondroll,firstroll, roll1, roll2, gamenumber, roll;
	string name; 
	char bet, R;
	cout <<"Lets Play Craps!" << endl;
	cout << endl;
	cout <<"This program stimulates a session at a craps table!"<< endl;
	cout << endl;
	cout <<"What's your first name , slick? ";
	cin >> name;
	cout <<"How much money do you have? ";
	cin >> money;
	cout << endl;
	cout <<"Round 1";
	cout << endl;
	cout <<"Bet on Pass(P) or Don't Pass(D) ?";
	cin >> bet;
	cout << endl;
	cout << "How much do you wager? ";
	cin >> wager;
	while (wager > money)
	{
		cout <<"Error, enter a smaller amount.";
		cout << endl;
		cout <<"How much do you wager?";
		cin >> wager;
	}

	srand(unsigned(time(NULL)));
	x=rand();							
	roll1=x % 6 + 1;
	srand(unsigned(time(NULL)));
	x=rand();							
	roll2=x % 6 + 1;
	roll = (roll1 + roll2);
	
	cout <<"Enter R to roll: ";
	cin >> R;
	cout << endl;
	cout << "Roll is " << roll << endl;
	cout << endl;

	while (roll != 2 || roll !=3 || roll !=
	

	}
	
	system("pause");
	return 0;
}

so far ive got this code I need it too do an automatic win if roll is 7 or 11 and lost if 2 3 12

@Lilcrew,
a)To do an automatic Win if roll is 7 or 11 and lose if it is 2,3 or 12 :

if(roll==7||roll==11)
{
cout << "You Win!"<<endl;
}
else if(roll==2||roll==3||roll==12)
{
cout << "You lose!"<<endl;
}
else
{
//Enter your code for the action to be taken if roll id not 7,11,2,3 or 12
}

b)In line number 22 and 23 of your code, you are not using the bet variable in any manner. What I mean to say is that, if the user enters something other than Pass(P) or Don't Pass(D) the program will still function. Similarly in line number 43 and 44, if the user enters something other than R, then too the program will still roll.
I'd suggest that you put some if - else if - else loops to check whether the user enters the correct option or not

c)In your program, you have used the system("pause") function. It is recommended not to use this function as this sends a system call to the DOS shell, and tells it to execute the pause function, thus causing a delay in program(although in milliseconds), and using more memory.
You can easily replace it with cin.get() function or with the following code :

cout << "Press any key to exit..."<<endl;
getch();
//Make sure you include the conio.h header file for getch() to work.

d)Although you've made a game called "Crabs", your program outputs "Craps game"(Line 11 and 13).
Another bug in Line 13 -> it should be "simulates", not "stimulates" :D.

I don't understand. You state:

c)In your program, you have used the system("pause") function. It is recommended not to use this function as this sends a system call to the DOS shell, and tells it to execute the pause function, thus causing a delay in program(although in milliseconds), and using more memory.
You can easily replace it with cin.get() function or with the following code :

cout << "Press any key to exit..."<<endl;
getch();
//Make sure you include the conio.h header file for getch() to work.

So which is it? The normal and acceptable cin.get or the non-standard and frowned upon getch() ?

So which is it? The normal and acceptable cin.get or the non-standard and frowned upon getch() ?

I know the getch() is old and non-standard, but cin.get() does not work for me in some situations, so I thought I should mention getch() .

By the way, great signature WaltP :D

I know the getch() is old and non-standard, but cin.get() does not work for me in some situations, so I thought I should mention getch() .

What situations? Using an input block to pause the program really only gets hampered by one thing: a non-empty stream. getch() always works because it doesn't look at the stream buffer, it looks at the keyboard buffer. Have you tried clearing the stream first?

#include <iostream>
#include <ios>
#include <limits>

cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();

Somewhat ironically, the only problem now is when the stream is empty. That too can be worked around, I wrote about the issue here. However, a far superior alternative is not to let the stream get boogered up in the first place. If you restrict user I/O to lines and then parse the lines in memory for further granularity, there's no need to clear the stream before calling cin.get().

getch() is best suited for jobs that cannot be solved portably. One example of a good use is the oft requested password masking:

#include <deque>
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int keybd_getc()
{
    return getch();
}

void keybd_putc(int ch)
{
    putch(ch);
}

void keybd_puts(const char *s)
{
    while (*s != '\0')
        keybd_putc(*s++);
}

string get_password(char mask = '*')
{
    bool done = false;
    deque<char> temp;

    while (!done) {
        int ch = keybd_getc();

        switch (ch) {
        case '\r':
            keybd_putc('\n');
            done = true;
            break;
        case '\b':
            if (!temp.empty()) {
                temp.pop_back();
                keybd_puts("\b \b");
            }
            break;
        default:
            temp.push_back(ch);
            keybd_putc(mask);
        }
    }

    return string(temp.begin(), temp.end());
}

int main()
{
    cout<<"Enter a password: ";

    string pass = get_password();

    if (!pass.empty())
        cout<<"Password was '"<< pass <<"'\n";
}

Okay so how would I get the roll to keep rolling until it reaches the point

What point do you want it to reach. That will determine where you want to put your loop.

When the second roll is the same as the first roll

well you could do something like

///...
srand(unsigned(time(NULL)));
int prevRoll = 0;
int roll = (rand() % 6 + 1) * 2;
while(prevRoll != roll)
{
    std::cout << "you rolled a: " << roll << std::endl;
    prevRoll = roll;
    roll = (rand() % 6 + 1) * 2;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.