Help me with lottery game.

 Lim Soo Zhin · October 10, 2015 Hey guys, I'm new here.Recently I have a project from my school. The project is a lottery project that requires us to produce 3 random numbers and let user to input 3 numbers.If the 3 numbers matched in exact order, they win 1,000,000.If 3 numbers matched without exact order, they win 1,000.If 2 numbers matched, they win 100.If 1 number matched, the win 10.if no number is matched, 0.Here's my code. Can anyone help to check whether there is/are any logical errors in the code?#include #include #include  using namespace std;  int main() {        int choice, random1, random2, random3, input1, input2, input3, match; // Declaration of all variables.    string reward;                                                        // Declaration of reward.        do{        cout << "This is a lottery game, do you want to play?\n";        cout << "1) Yes\n";        cout << "2) No\n";        cout << "Your choice : ";        cin >> choice;        cout << "\n";                if (choice != 1 && choice != 2){            cout << "Please select your choice again,\n\n";        }            }while (choice != 1 && choice != 2);        while (choice==1){        srand(time(NULL)); //the creation of random numbers        random1=rand()%10;        random2=rand()%10;        random3=rand()%10;                do{            cout << "Please enter your first number (0-9) : ";            cin >> input1;                        if (input1 > 9 || input1 < 0){                cout << "\nPlease try again.\n\n";            }                    }while (input1 > 9 || input1 < 0);                do{            cout << "Please enter your second number (0-9) : ";            cin >> input2;                        if (input2 > 9 || input2 < 0){                cout << "\nPlease try again.\n\n";            }        }while (input2 > 9 || input2 < 0);         do{            cout << "Please enter your third number (0-9) : ";            cin >> input3;                        if (input3 > 9 || input3 < 0){                cout << "\nPlease try again.\n\n";            }        }while (input3 > 9 || input3 < 0);         if (random1 != input1){                        if (random1 != input2){                                if (random1 != input3){                                        if (random2 != input1){                                                if (random2 != input2){                                                        if (random2 != input3){                                                                if (random3 == input1 || random3 == input2 || random3 == input3){                                    match = 1;                                }                                else {                                    match = 0;                                }                            }                            else if (random3 == input1 || random3 == input2){                                match = 2;                            }                            else {                                match = 1;                            }                        }                        else if (random3 == input1 || random3 == input3){                            match = 2;                        }                        else {                            match = 1;                        }                    }                    else if (random3 == input2 || random3 == input3){                        match = 2;                    }                    else {                        match = 1;                    }                }                else if (random2 != input1){                    if (random2 != input2){                        if (random3 == input1 || random3 == input2){                            match = 2;                        }                        else {                            match = 1;                        }                    }                    else if (random3 == input1){                        match = 3;                    }                    else {                        match = 2;                    }                }                else if (random3 == input2){                    match = 3;                }                else {                    match = 2;                }            }            else if (random2 != input1){                if (random2 != input3){                    if (random3 == input1 || random3 == input3){                        match = 2;                    }                    else {                        match = 1;                    }                }                else if (random3 == input1){                    match = 3;                }                else {                    match = 2;                }            }            else if (random3 == input3){                match = 3;            }            else {                match = 2;            }        }        else if (random2 != input2){            if (random2 != input3){                if (random3 == input2 || random3 == input3){                    match = 2;                }                else {                    match = 1;                }            }            else if (random3 == input2){                match = 3;            }            else {                match = 2;            }        }        else if (random3 == input3){            match = 4;        }        else {            match = 2;        }                if (match == 4)            reward = "Congratualation!!! You've won \$1,000,000!!!";        else if (match == 3)            reward = "Wow, you've just got yourself \$1000.";        else if (match == 2)            reward = "Nice, your reward is \$100.";        else if (match == 1)            reward = "That was close, \$10 for you.";        else if (match == 0)            reward = "\$0 for you. Aww... Don't feel bad. As the saying goes, when there's a will, there's a way. There will always be a next time.";                cout << "\n\n";        cout << "The lottery number : ";        cout << random1 << "   ";        cout << random2 << "   ";        cout << random3 << endl;        cout << "Your guess         : ";        cout << input1 << "   ";        cout << input2 << "   ";        cout << input3 << "\n\n";        cout << reward << endl << endl;         do{            cout << "Do you still want to play this lottery game?\n";            cout << "1) Yes\n";            cout << "2) No\n";            cout << "Your choice : ";            cin >> choice;            cout << "\n";                        if (choice != 1 && choice != 2){                cout << "Please select your choice again,\n\n";            }                    }while (choice != 1 && choice != 2);    }            return 0;}

Replies

 Jon Z. · October 11, 2015 Seems fine to me. Do you have any issues with it? Jason Hall · October 12, 2015 I copied your code and ran it trying to find errors, unfortunately I did, I found there was a problem with the loops (do-while) loops so I changed it like so, NOTE I am not the greatest at programming so im sure it can be shortened down however I fixed the problem by using the goto function instead:#include #include #include using namespace std;int random1, random2, random3, input1, input2, input3, match; // Declaration of all variables. string reward, choice, choice2; // Declaration of reward.int main() { start: cout << "This is a lottery game, do you want to play?\n"; cout << "1) Yes\n"; cout << "2) No\n"; cout << "Your choice : "; cin >> choice; cout << "\n";if ((choice != "yes") and (choice != "no")){    cout << "\nYou did not type yes or no. Please try again.\n" << endl;    goto start;} if (choice=="yes"){ srand(time(NULL)); //the creation of random numbers random1=rand()%10; random2=rand()%10; random3=rand()%10; { num1: cout << "Please enter your first number (0-9) : "; cin >> input1; if (input1 > 9 || input1<0){ cout << "\nPlease try again.\n\n"; goto num1; } } { num2: cout << "Please enter your second number (0-9) : "; cin >> input2; if (input2 > 9 || input2 < 0){ cout << "\nPlease try again.\n\n"; goto num2; } } { num3: cout << "Please enter your third number (0-9) : "; cin >> input3; if (input3 > 9 || input3 < 0){ cout << "\nPlease try again.\n\n"; goto num3; } } if (random1 != input1){ if (random1 != input2){ if (random1 != input3){ if (random2 != input1){ if (random2 != input2){ if (random2 != input3){ if (random3 == input1 || random3 == input2 || random3 == input3){ match = 1; } else { match = 0; } } else if (random3 == input1 || random3 == input2){ match = 2; } else { match = 1; } } else if (random3 == input1 || random3 == input3){ match = 2; } else { match = 1; } } else if (random3 == input2 || random3 == input3){ match = 2; } else { match = 1; } } else if (random2 != input1){ if (random2 != input2){ if (random3 == input1 || random3 == input2){ match = 2; } else { match = 1; } } else if (random3 == input1){ match = 3; } else { match = 2; } } else if (random3 == input2){ match = 3; } else { match = 2; } } else if (random2 != input1){ if (random2 != input3){ if (random3 == input1 || random3 == input3){ match = 2; } else { match = 1; } } else if (random3 == input1){ match = 3; } else { match = 2; } } else if (random3 == input3){ match = 3; } else { match = 2; } } else if (random2 != input2){ if (random2 != input3){ if (random3 == input2 || random3 == input3){ match = 2; } else { match = 1; } } else if (random3 == input2){ match = 3; } else { match = 2; } } else if (random3 == input3){ match = 4; } else { match = 2; } if (match == 4) reward = "Congratualation!!! You've won \$1,000,000!!!"; else if (match == 3) reward = "Wow, you've just got yourself \$1000."; else if (match == 2) reward = "Nice, your reward is \$100."; else if (match == 1) reward = "That was close, \$10 for you."; else if (match == 0) reward = "\$0 for you. Aww... Don't feel bad. As the saying goes, when there's a will, there's a way. There will always be a next time."; cout << "\n\n"; cout << "The lottery number : "; cout << random1 << " "; cout << random2 << " "; cout << random3 << endl; cout << "Your guess : "; cout << input1 << " "; cout << input2 << " "; cout << input3 << "\n\n"; cout << reward << endl << endl; do{ cout << "Do you still want to play this lottery game?\n"; cout << "1) Yes\n"; cout << "2) No\n"; cout << "Your choice : "; cin >> choice2; cout << "\n"; if (choice2 != "yes" && choice2 != "no"){ cout << "You did not type yes or no,\n\n"; } }while (choice2 != "yes" && choice2 != "no"); if (choice2 == "yes")    goto num1; } return 0;} Jason Hall · October 12, 2015 ``Issues: (do-while loops)````MyVersion: (fixed using GOTO function)````Bugs: Letters breaks it all ;(````#include #include #include using namespace std;int random1, random2, random3, input1, input2, input3, match; // Declaration of all variables. string reward, choice, choice2; // Declaration of reward.int main() { start: cout `` Robert Berg · October 12, 2015 Hi!Im a beginner at c++ and have been watching a lot of Buckys fantastic tutorials.I needed something to exercise with, so I decided to try your problem.I made a working program and feel free to use that and change it the way you want.#include #include #include using namespace std;int smallest(int a[10], int b[10]);int perfektm(int rnumb[3], int mnumb[3]);int numberofm(int rfrnumb[10], int mfrnumb[10]);int main(){   int manch[10] = {0,0,0,0,0,0,0,0,0,0};    int ranch[10] = {0,0,0,0,0,0,0,0,0,0};    srand(time(0));    cout<<"Do you wanna cheat? (1 for yes)"<>cheat;    int randnumb[3];    for(int x = 0;x;x++){        randnumb[x] = rand()%10;        ranch[randnumb[x]] +=1;        if (cheat==1){            cout<<"Random number "<>mannumb[x];        manch[mannumb[x]] +=1;}    int mnbr = perfektm(randnumb,mannumb) + numberofm(ranch,manch);    switch(mnbr){        case 1:            cout<<"Congratulations you have won 10 dollar"<b){a=b;}    return a;}int perfektm(int rnumb[],int mnumb[]){    int stillperfekt = 1;        for(int x = 0;x;x++){            if(rnumb[x]!=mnumb[x]){            stillperfekt = 0;        }    }    return stillperfekt;}int numberofm(int rfrnumb[],int mfrnumb[]){    int summatch = 0;    for(int x = 0;x<10;x++){        summatch += smallest(rfrnumb[x],mfrnumb[x]);    }    return summatch;}Feel also free to ask me if anything is unclear. Robert Berg · October 12, 2015 Hi my name is RobertI tried to post a program here, that actually works! But I had a problem pasting in my code here (computer and internet idiot as I am) Robert Berg · October 12, 2015 After a couple of days I passed the needles I and am now able to post things in this forumMy program is not so well commented, but I wanted to see if there was a possibility reduce thenumber of if-statements.By creating two arrays [10] were each elements represent the number of times the element number is choosen by human respectivly rand function it became easier, This way you just have to sum the number of matches going through all element pairs. This explanation wasnt crystal clear but perhaps its clearer when you look at the code;I was to lazy to do the fine work with checking indata,This took me some ours of zapping back and forth between like 20 of buckys tutorials Lim Soo Zhin · October 13, 2015 Jon Z. : Actually, I don't know the issue that I had. But I think i fixed it with Jason Hall's suggestions.Jason Hall : Thanks man. I was just thinking about how to solve the "letters-breaks-it-all" problem. You really helped me a lot. Thanks again.Robert Berg : Will find time to try your code. Robert Berg · October 13, 2015 Nice of u to take your time even though you´ve already got your problems fixed.Always nice with feedback, I see now that ;< + 3 =
• 1

C++

131,223 followers