Winston_Churchill
Bazinga!
+521|7006|Toronto | Canada

Okay, I'm been getting this error now with my program (Intro to C) and I can't figure out what I'm missing.  The program works fine now, but it appears my logic is incorrect.  Everything works except at the end of the program, when the money=0, the program is supposed to end.  But mine asks for a number and a bet again and ends the program after that.  I'm not sure how to fix this.  Any help would be appreciated.

Code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
        printf("Place your bet on a number between 0 and 36:\n");
        int choice;
        scanf("%d", &choice);
        printf("Enter a bet in dollars $:\n");
        double bet;
        scanf("%lf", &bet);
        double money;
        money=bet;

        while(money>0 && bet!=0 && bet<=money)
        {
                int spin;
                srand(time(NULL));
                spin=(rand()%37);

                if(spin==choice)
                {
                        printf("The result of the spin is %d.\n", spin);
                        money+=bet+bet*5;
                        printf("You now have $%.2f to gamble with.\n", money);
                }
                else if(spin%2==0 && choice%2==0 || spin%2!=0 && choice%2!=0 )
                {
                        printf("The result of the spin is %d.\n", spin);
                        money+=bet+bet;
                        printf("You now have $%.2f to gamble with.\n", money);
                }
                else
                {
                        printf("The result of the spin is %d.\n", spin);
                        money=money-bet;
                        printf("You now have $%.2f to gamble with.\n", money);
                }
                printf("Place your bet on a number between 0 and 36:\n");
                scanf("%d", &choice);
                printf("Enter a bet in dollars $:\n");
                scanf("%lf", &bet);
        }
        printf("Thank you for playing.  You leave the table with $%.2f.\n", money);
        return 0;
}

Last edited by Winston_Churchill (2008-10-02 18:54:35)

Freezer7Pro
I don't come here a lot anymore.
+1,447|6464|Winland

Ah, if I would only remember the C class I took a year ago
The idea of any hi-fi system is to reproduce the source material as faithfully as possible, and to deliberately add distortion to everything you hear (due to amplifier deficiencies) because it sounds 'nice' is simply not high fidelity. If that is what you want to hear then there is no problem with that, but by adding so much additional material (by way of harmonics and intermodulation) you have a tailored sound system, not a hi-fi. - Rod Elliot, ESP
Beduin
Compensation of Reactive Power in the grid
+510|6017|شمال
lol.. am C noob... Just started =P
الشعب يريد اسقاط النظام
...show me the schematic
Computer_Guy
Member
+54|6964
Im only taking Java as of now
Winston_Churchill
Bazinga!
+521|7006|Toronto | Canada

Anyone know C?  This program is due tomorrow so I gotta figure out whats wrong pretty soon
Winston_Churchill
Bazinga!
+521|7006|Toronto | Canada

OP updated with new error
jsnipy
...
+3,277|6789|...

try this logic ...

Code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
        double money;
        double bet;
        int choice;

        printf("How much money are you starting with?:\n");
        scanf("%lf", &money);


        while(money>0 || (bet!=0 && bet<=money)) /* CHANGE */
        {
        printf("Place your bet on a number between 0 and 36:\n");
                scanf("%d", &choice);
                printf("Enter a bet in dollars $:\n");
                scanf("%lf", &bet);

                int spin;
                srand(time(NULL));
                spin=(rand()%37);

                if(spin==choice)
                {
                        printf("The result of the spin is %d.\n", spin);
                        money+=bet+bet*5;
                        printf("You now have $%.2f to gamble with.\n", money);
                }
                else if(spin%2==0 && choice%2==0 || spin%2!=0 && choice%2!=0 )
                {
                        printf("The result of the spin is %d.\n", spin);
                        money+=bet+bet;
                        printf("You now have $%.2f to gamble with.\n", money);
                }
                else
                {
                        printf("The result of the spin is %d.\n", spin);
                        money=money-bet;
                        printf("You now have $%.2f to gamble with.\n", money);
                }


                printf("Debug: Bottom of loop money=$%.2f bet=$%.2f \n", money,bet);
        }
        printf("Thank you for playing.  You leave the table with $%.2f.\n", money);
        return 0;
}
The main thing was changed was the betting dialog was put in one spot. The while condition now essentially says: Either there is money or there is a better that's not 0 and the bet is less than/equal to the money. HTH

https://img518.imageshack.us/img518/1725/14936160oy3.gif

Last edited by jsnipy (2008-10-02 19:49:24)

san4
The Mas
+311|6955|NYC, a place to live
The request for a bet and a number has to be at the beginning of the loop. Otherwise the questions get asked before the program checks if money was just reduced to zero in the last round.

Last edited by san4 (2008-10-02 20:12:37)

Winston_Churchill
Bazinga!
+521|7006|Toronto | Canada

san4 wrote:

The request for a bet and a number has to be at the beginning of the loop. Otherwise the questions get asked before the program checks if money was just reduced to zero in the last round.
Yeah, thats what it was.  I just added and if (money>0) statement to the request at the end and it fixed it

Thanks guys

Board footer

Privacy Policy - © 2025 Jeff Minard