Couple of small things...

Started by
8 comments, last by Durfy 16 years, 8 months ago
Hi everyone I've made a slot machine program that compiles, links and executes fine. There's just one thing I want it to do that it doesn't plus I get a couple of warning messages that I don't understand. I want the program to re-give the user the option of selecting 1) play slot 2) exit after they have given an invalid selection and seen the message. I think I need the return 0 but I'm not sure where. I get the following warning messages:


------ Build started: Project: 3.7.6, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.6\3.7.6\main.cpp(36) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.6\3.7.6\main.cpp(44) : warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.6\3.7.6\main.cpp(50) : warning C4806: '!=' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.6\3.7.6\Debug\BuildLog.htm"
3.7.6 - 0 error(s), 3 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========




Here's my code:


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

int main()
{
	int choice = 1;
	int chips = 1000;

	while(choice == 1 && chips != 0)
	{
		cout << "Players's chips: $" << chips << endl;
		cout << "1) Play slot 2) Exit: ";
		cin >> choice;

		if( choice == 2 )
		{
			choice = 2;
			cout << "Exiting..." << endl;
		}
		else if( choice != 1 && choice != 2)
		{
			cout << "You did not make a valid selection!" << endl;
			return 0;
		}
		else
		{
			int bet = 0;
			cout << "Enter your bet: ";
			cin >> bet;
			cout << endl;

			if(bet > 0 && bet <= chips)
			{
				srand ( time(0) );

				int r0 = 2 + rand() % 6;
				int r1 = 2 + rand() % 6;
				int r2 = 2 + rand() % 6;

				cout << r0 << " " << r1 << " " << r2 << endl;

				if( r0 == r1 == r2 == 7 )
				{
					cout << "You win!" << endl;
					chips = (chips - bet) + (10 * bet);
					cout << "Player's chips: $" << chips << endl << endl;
				}
				else if( (r0 == r1 == r2 != 7) )
				{
					cout << "You win!" << endl;
					chips = (chips - bet) + (7 * bet);
					cout << "Player's chips: $" << chips << endl << endl;
				}
				else if( (r0 == r1 && r0 != r2 && r1 != r2) || (r0 == r2 && r0 != r1 && r2 != r1) || (r1 == r2 && r1 != r0 && r2 != r0) )
				{
					cout << "You win!" << endl;
					chips = (chips - bet) + (3 * bet);
					cout << "Player's chips: $" << chips << endl << endl;
				}
				else
				{
					cout << "You lose!" << endl;
					chips -= bet;
					cout << "Player's chips: $" << chips << endl << endl;
				}
			}
			else
			{
				cout << "You did bot enter a valid bet";
			}
		
			if( chips == 0 )
			{
				chips = 0;
				cout << "You've lost all your chips!" << endl << "Exiting..." << endl << endl;
			}
		}
	}
}


Advertisement
you can´t do something like:

 r0 == r1 == r2 == 7 


because the == operator returns a bool and you can´t go
on working with a bool for the next comparison

PS: well you can but a bool can never be 7 (that is what the compiler is
complaining about)

it has to be something like:
if( r0 == r1 && r1 == r2 && r2 == 7 ) 


your error in line 36 is that srand wants an unsigned int and time_t
has a different type (but that shouldn´t be an issue)

[Edited by - Noobico on August 10, 2007 7:48:52 AM]
If you use a static cast it will make the C4244 warning messsage go away:
time_t time_t_var;unsigned int foo=static_cast<unsigned int>(time_t_var);
I dont understand the logic of your while loop...
You do:
while ( choice == 1 && chips != 0)

Yet you explicitly check for choices with other #'s.
if (choice == 2) {
choice = 2;
}

what is the point of that? if choice is 2 why are u resetting it to 2 again. And also it will never get to that point right? cuz its while condition won't be met if that is true and it will kick out no? I like your idea for the program tho keep up the good work.
Thanks,
-durfy
Hi, thanks for your replies.

I made the suggested changes and it works now, thank you.

stevenmarky, thank you for your suggestion about my other warning, I think I will just leave this for now since I haven't covered anything like that yet.

With my while loop, I wasn't sure where to put the case 'choice == 2', should that go after the while loop? I set it to 2 if 2 is inputted so that you exit out of the loop.

If you enter anything other than 1 or 2 for choice, you get the message come up but then just 'Press any key to continue' but I want the choice menu to come back up, is there any way I can easily do that?
just replace your

return 0; in line 25
(this exits your whole program)

with

continue; (the code continues at the while statement)

PS: to exit if choice = 2 just put
return 0; in line 21
Hi, thank you for your reply. I tried that but it still exits the program. I think I may have made some small adjustments to my code so here is the revised code:

#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){	int choice = 1;	int chips = 1000;	while(choice == 1 && chips != 0)	{		cout << "Players's chips: $" << chips << endl;		cout << "1) Play slot 2) Exit: ";		cin >> choice;		if( choice == 2 )		{			choice = 2;			cout << "Exiting..." << endl;		}		else if( choice != 1 && choice != 2)		{			cout << "You did not make a valid selection!" << endl;			continue;		}		else		{			int bet = 0;			cout << "Enter your bet: ";			cin >> bet;			cout << endl;			if(bet > 0 && bet <= chips)			{				srand ( time(0) );				int r0 = 2 + rand() % 6;				int r1 = 2 + rand() % 6;				int r2 = 2 + rand() % 6;				cout << r0 << " " << r1 << " " << r2 << endl;				if( r0 == r1 && r1 == r2 && r2 == 7 )				{					cout << "You win!" << endl;					chips = (chips - bet) + (10 * bet);					cout << "Player's chips: $" << chips << endl << endl;				}				else if( r0 == r1 && r1 == r2 && r2 == 6 || r0 == r1 && r1 == r2 && r2 == 5 || r0 == r1 && r1 == r2 && r2 == 4 || r0 == r1 && r1 == r2 && r2 == 3 || r0 == r1 && r1 == r2 && r2 == 2 )				{					cout << "You win!" << endl;					chips = (chips - bet) + (7 * bet);					cout << "Player's chips: $" << chips << endl << endl;				}				else if( (r0 == r1 && r0 != r2 && r1 != r2) || (r0 == r2 && r0 != r1 && r2 != r1) || (r1 == r2 && r1 != r0 && r2 != r0) )				{					cout << "You win!" << endl;					chips = (chips - bet) + (3 * bet);					cout << "Player's chips: $" << chips << endl << endl;				}				else				{					cout << "You lose!" << endl;					chips -= bet;					cout << "Player's chips: $" << chips << endl << endl;				}			}			else			{				cout << "You did bot enter a valid bet";			}					if( chips == 0 )			{				chips = 0;				cout << "You've lost all your chips!" << endl << "Exiting..." << endl << endl;			}		}	}}
you have to inser a line before continue;

something like that:
choice=1;
continue;

cause your while statement is false otherwise -> exit
Great! Thank you for all your replies, I understand the logic of needed an extra statement in there to make the while condition true still.

Thanks,
Chris.
Or you can try putting your exit message outside of the loop. That way it will be executed on program termination :-)
-durfy

This topic is closed to new replies.

Advertisement