Stuck

This is a discussion on Stuck within the C++ Programming forums, part of the General Programming Boards category; I am trying to get my programme to make sure that the name of the horse entered is either black ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Stuck

    I am trying to get my programme to make sure that the name of the horse entered is either black beauty, red rum, green goddess or silver speedy and if it isnt then it loops back to the question other wise it continues. I haven't been able to get this to work...what do I use?

    This is the code that needs to be checked:

    Code:
    cout<<"Please choose your horse: ";
        getline (cin, horse);
        cout<<endl;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Compare the horse variable with the strings you mentioned and break out of the loop when it matches one of them. With only four possibilities an if/else if series should be fine.

    Do you know how to make a loop? Start by making a loop that keeps asking that question over and over. When you get that to work, add code that breaks out of the loop when the horse is named "black beauty". Then try adding code to check the other names. If you get stuck at any step, post all your code and ask about the specific problem.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I do know how to make a loop but I am not sure which one I should use. And I am not sure about for loops if that is what I should use.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by bumfluff
    I do know how to make a loop but I am not sure which one I should use. And I am not sure about for loops if that is what I should use.
    I'll ignore the unmentionable form of looping who's name starts with a "g"

    There are essentially 3 types of loops, and which one you pick depends on your situation. (In your situation, I would guess you can calculate the number of strings you need to process, and therefore the number of times you need to loop)

    for loops - Most commonly used to run through a static number of times (in other words, the number of times the loop runs is known, or can be calculated by the time the loop is initialised).

    while loops - may run once, indefinitely, or never - or anything inbetween - depending entirely on the condition(s) between its parenthesis. Often, the number of times it loops is not known until the loop finishes.

    do...while loops - the only loop which must run to the end at least once, because the while condition is checked after each iteration. As with plain 'while' loops, the number of times it runs through is often not known until the loop finishes.

    In addition to the above - all loops can be terminated prematurely at any time using a break, return or exit.


    the unmentionable form of loop beginning with the letter "g" should never ever be used in C++, and if it is used, you should wash your hands with antibacterial soap afterwards.
    Last edited by Bench82; 03-22-2006 at 06:15 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I have found out where my mistake was I needed to use horse != "black beauty" || horse != "Black Beauty" etc instead of horse != "black beauty" || "Black Beauty"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > instead of horse != "black beauty" || "Black Beauty"
    Well if you had posted this line in your original post, you would have had your answer 11 hours ago
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I didn't have that line at that stage anyway...I was also using the wrong loop.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How do I make it show the £ sign? as cout<<"£"; shows u acute

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    cout << (char)156;
    Last edited by SlyMaelstrom; 03-23-2006 at 10:11 AM. Reason: To modernize the cast
    Sent from my iPad®

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I now have a problem where it works out what horse won the race and then whooshes through the rest. I wrote this part in English today so I wouldn't be surprised if there were loads of mistakes and I may have completely overlooked mistakes when typing up...

    Code:
    cout<<"Please wait while the race takes place..."<<endl;
        cout<<endl;
        system ("PAUSE");
    
        win = (rand() % (max2 - min2 + 1) + min);
    
        switch (win)
        {
               case 1:
                    winner = "black beauty";
                    winner2 = "Black Beauty";
    
                    winnings = a * bet;
                    break;
               case 2:
                    winner = "red rum";
                    winner2 = "Red Rum";
    
                    winnings = b * bet;
                    break;
               case 3:
                    winner = "green goddess";
                    winner2 = "Green Goddess";
    
                    winnings = c * bet;
                    break;
               case 4:
                    winner = "silver speedy";
                    winner2 = "Silver Speedy";
    
                    winnings = d * bet;
                    break;
        }
    
        if (horse == winner || horse == winner2) {
           cout<<"Congratulations, "<<horse<<" won the race!"<<endl;
           cout<<endl;
           cout<<"You have won £"<<winnings<<endl;
           balance = balance + winnings;
           cout<<endl;
           cout<<"Your balance is now £"<<balance<<endl;
           cout<<endl;
           cout<<"Would you like to play again?(Y/N): ";
           getline (cin, stopch);
                       if (stopch == "n" || stopch == "N") {
                                  break;
                       }
                       else {
                                  system ("CLS");
                                  keepgoing = true;
                       }
       }
       else {
            cout<<"Sorry, "<<horse<<" lost."<<endl;
            cout<<endl;
            balance = balance - bet;
            cout<<"Your balance is now £"<<balance<<endl;
            cout<<endl;
            cout<<"Would you like to play again?(Y/N): ";
            getline (cin, stopch);
                       if (stopch == "n" || stopch == "N") {
                                  break;
                       }
                       else {
                       system ("CLS");
                       keepgoing = true;
                       }
      }
    } while (keepgoing = true);
    
      return 0;
    }
    I have tried to make the formatting in this programme a but better.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Is anybody able to help me as I need to get this programme working.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Can anyone tell me what my problem is?

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by bumfluff
    Is anybody able to help me as I need to get this programme working.
    Help with what? If you get errors on the compile, we need to know what they are and where they are. If you get errors when you run it, we need to know what was expected and what went wrong.

    Most of us aren't psychic and can't always fix errors for you without a little information.


    Also, why call the operating system with system ("PAUSE");?!?!? What's wrong with cin.get();? Leave the O/S out of your program.


    Quote Originally Posted by bumfluff
    I wrote this part in English today
    Good. I find it hard to read Latin and Greek!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > while (keepgoing = true)
    Try
    while (keepgoing == true)

    Or even just
    while (keepgoing)

    Code:
           cout<<"Would you like to play again?(Y/N): ";
           getline (cin, stopch);
                       if (stopch == "n" || stopch == "N") {
                                  break;
                       }
                       else {
                                  system ("CLS");
                                  keepgoing = true;
                       }
    1. Work on the indentation (or use of tabs when posting to the forum)
    2. Make it a separate function - this is code which is duplicated.

    Say
    keepgoing = promptForAnotherRun();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well neither of the changes to the while seem to make a difference.

    Here is all the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      int a;
      int b;
      int c;
      int d;
      int balance = 100;
      int bet;
      int min = 2;
      int max = 12;
      int min2 = 1;
      int max2= 4;
      int win;
      int winnings;
      string stopch;
      string horse;
      string winner;
      string winner2;
      bool keepgoing = false;
    
        cout<<"Welcome to Horse Pundit V1.0"<<endl;
        cout<<endl;
    
    do {
      srand(time(NULL));    
      a = (rand() % (max - min + 1) + min);
      b = (rand() % (max - min + 1) + min);
      c = (rand() % (max - min + 1) + min);
      d = (rand() % (max - min + 1) + min);
    
        cout<<"Your balance is "<<char(156)<<balance<<endl;
        cout<<endl;
        cout <<"Odds...."<<endl;
        cout<<endl;
        cout<<"Black Beauty: "<<a<<"/1"<<endl;
        cout<<"Red Rum: "<<b<<"/1"<<endl;
        cout<<"Green Goddess: "<<c<<"/1"<<endl;
        cout<<"Silver Speedy: "<<d<<"/1"<<endl;
        cout<<endl;
    
        while ( horse != "black beauty" || horse != "Black Beauty" || horse != "red rum" || horse != "Red Rum" || horse != "Green Goddess" || horse != "green goddess" || horse != "Silver Speedy" || horse != "silver speedy")  {
        cout<<"Please choose your horse: ";
        getline (cin, horse);
        cout<<endl;
                   if (horse == "black beauty" || horse == "Black Beauty" || horse == "red rum" || horse == "Red Rum" || horse == "Green Goddess" || horse == "green goddess" || horse == "Silver Speedy" || horse == "silver speedy") {
                             break;
                       }
    
                   else {
                             cout<<"That is not a correct horsename!"<<endl;
                             cout<<endl;
                        }
        }
    
        while ( bet > balance ) {
        cout<<"Please enter the amount you would like to bet on "<<horse<<": £";
        cin>>bet;
        cout<<endl;
                   if ( bet < balance || bet == balance ) {
                            break;
                   }
    
                   else {
                   cout<<"You don't have enough money to bet that amount!"<<endl;
                   cout<<endl;
                   }
    
        }
    
        cout<<"Please wait while the race takes place..."<<endl;
        cout<<endl;
        system ("PAUSE");
    
        win = (rand() % (max2 - min2 + 1) + min);
    
        switch (win)
        {
               case 1:
                    winner = "black beauty";
                    winner2 = "Black Beauty";
    
                    winnings = a * bet;
                    break;
               case 2:
                    winner = "red rum";
                    winner2 = "Red Rum";
    
                    winnings = b * bet;
                    break;
               case 3:
                    winner = "green goddess";
                    winner2 = "Green Goddess";
    
                    winnings = c * bet;
                    break;
               case 4:
                    winner = "silver speedy";
                    winner2 = "Silver Speedy";
    
                    winnings = d * bet;
                    break;
        }
    
        if (horse == winner || horse == winner2) {
           cout<<"Congratulations, "<<horse<<" won the race!"<<endl;
           cout<<endl;
           cout<<"You have won £"<<winnings<<endl;
           balance = balance + winnings;
           cout<<endl;
           cout<<"Your balance is now £"<<balance<<endl;
           cout<<endl;
           cout<<"Would you like to play again?(Y/N): ";
           getline (cin, stopch);
                       if (stopch == "n" || stopch == "N") {
                                  break;
                       }
                       else {
                                  system ("CLS");
                                  keepgoing = true;
                       }
       }
       else {
            cout<<"Sorry, "<<horse<<" lost."<<endl;
            cout<<endl;
            balance = balance - bet;
            cout<<"Your balance is now £"<<balance<<endl;
            cout<<endl;
            cout<<"Would you like to play again?(Y/N): ";
            getline (cin, stopch);
                       if (stopch == "n" || stopch == "N") {
                                  break;
                       }
                       else {
                                  system ("CLS");
                                  keepgoing = true;
                       }
      }
    } while (keepgoing);
    
      return 0;
    }

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21