PDA

View Full Version : craps game, final issue


Recklein3
10-22-2009, 10:24 AM
Trying to write a program in C++ to play craps.
But I have one little problem left.
I can't get it to exit when you press n, just continue when you press y.
I've tried a few ways, but none seem to get both to work at the same time.

Help would great.

Thanks!


#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
#include "CrapsCal.h"

void main()
{

double cashmoney(50.00);
double bet(0.00);
char timeup(0);

do
{
int point_one(0);
int point_two(0);


char timeup(0);
char srand (time (0));



cout << "Cash in hand: $" << cashmoney << endl;
cout << "Place your bet and roll the dice (maximum bet equals cash in hand): ";
cin >> bet;

if(bet > cashmoney && bet > 0)
{
cout << "Bet is too high, please bet lower or equal to amount of cash on hand: ";
cin >> bet;
}

cashmoney -= bet;
point_one = throwdice();
cout << "Your roll equals: " << point_one << endl;

if(cal1(point_one) == 1)
{
cout << "You Win!" << endl;

cashmoney += (bet*2);
point_one = 0;
bet = 0.00;

cout << "Total cash in hand equals: $" << cashmoney << endl;
}
else if(cal1(point_one) == -1)
{
cout << "You Lost...." << endl;

cashmoney - bet;
point_one = 0;
bet = 0.00;

cout << "Total cash in hand equals: $" << cashmoney << endl;
}
else
{
do
{
point_two = throwdice();

cout << "New roll equals: "<< point_two << endl;
cout << "Old Point equals: "<< point_one << endl;
cout << "New Point equals: "<< point_two << endl;

if(cal2(point_one , point_two) == 1)
{
cout << "You Win!" << endl;

cashmoney += (bet*2);
point_one = 0;
bet = 0.00;

cout << "Total cash in hand equals: $" << cashmoney << endl;
}
else if(cal2(point_one , point_two) == -1)
{
cout << "You Lost...." << endl;

cashmoney - bet;
point_one = 0;
bet = 0.00;

cout << "Total cash in hand equals: $" << cashmoney << endl;
}
}
while(cal2(point_one , point_two) != 1 && cal2(point_one , point_two) != -1);


}



cout << "Would you like to test your luck again? (y/n) ";
cin >> timeup;





}
while((timeup != 'y') || (timeup != 'Y') && (timeup == 'n') || (timeup == 'N'));
{
if ((timeup == 'n') || (timeup == 'N'));
exit (0);

}

TheShaner
10-22-2009, 05:12 PM
First, you don't need to bold your text. We can read normal text just fine.

Now, you want the loop to continue if they press y or Y, else break out of the loop. The below will do this.
while((timeup == 'y') || (timeup == 'Y'));
The timeup variable needs to equal y or Y to continue, else break.

Also, in the future, you should use the String compare method (http://www.cppreference.com/wiki/string/compare) for comparing Strings.

Since the above will exit the loop as long as a y or Y isn't entered, you don't need to check for an n or N and thus can remove that code and just do an exit when the loop is broken.

-Shane

oracleguy
10-22-2009, 05:17 PM
Why isn't the Y/N prompt be inside the loop? Right now if you enter say, a it will just loop forever and the program will be frozen. You want to use a do while loop with the prompt inside of it.

TheShaner
10-22-2009, 06:00 PM
Why isn't the Y/N prompt be inside the loop? Right now if you enter say, a it will just loop forever and the program will be frozen. You want to use a do while loop with the prompt inside of it.
It is. It's a giant Do-While loop. The While is at the end of the Do. What's confusing is the open brace after the while. That should not be there. Neither should that IF check for n or N.

-Shane

oracleguy
10-22-2009, 06:10 PM
It is. It's a giant Do-While loop. The While is at the end of the Do. What's confusing is the open brace after the while. That should not be there. Neither should that IF check for n or N.

-Shane

I totally missed the 'do' on my first read through. You're right.

Also main should return an int and not be void.

Rhodge09
11-19-2009, 11:41 PM
Hey i had a quick question if someone had made a craps game out of PERL scripting if so i'd like to know how asap.

oracleguy
11-19-2009, 11:49 PM
Hey i had a quick question if someone had made a craps game out of PERL scripting if so i'd like to know how asap.

Please do not thread jack.

Rhodge09
11-20-2009, 12:39 AM
how am i jacking your thread when you already have your answer? do you think you can help me if its possible?

vithiancamelia
11-21-2009, 03:03 AM
Merci, c'est justement ce que je cherchais :P:thumbsup: