code is doing the opposite of what i tell it to do..

This is a discussion on code is doing the opposite of what i tell it to do.. within the C++ Programming forums, part of the General Programming Boards category; ok well here is my problem. my code is doing the exact opposite i want it to do. it is ...

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

    code is doing the opposite of what i tell it to do..

    ok well here is my problem. my code is doing the exact opposite i want it to do. it is saying when a person enters a 1 or anything else other than a 0 it will show up as a the person rolled but i would like it to show up when a person enters a number or letter thats NOT 1 it will show up as

    Code:
    //adds the iostream library to the program
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define BOOL_H
    #undef true
    #undef false 
    //informs the compiler you are using the standard library set
    using namespace std;
    
    const int LOW = 1;
    const int HIGH = 6;
    int firstDie;
    int secondDie;
    int Choice;
    int yes;
    int no;
    
    /*
    Variables to hold random values
    for the first and the second die on
    each roll.
    */
    
    
    int main ()    
    {
       
       time_t seconds;
       time(&seconds);
       srand((unsigned int) seconds);
       for ( ;; )
       {
          cout << "Round 1 \n";
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
       if ( cin >> yes || no == 0 )
       {}
       else
          {firstDie = rand() % (HIGH - LOW + 1) + LOW;
    (secondDie = rand() % (HIGH - LOW) + 1) + LOW;
    cout << "you rolled a "<< firstDie << " and a " << secondDie << endl; break;}    
       }
           system("pause\n\n\n");
       return 0;
    }

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    what are you trying to do with this line?
    Code:
    if ( cin >> yes || no == 0 )
    cin >> yes will return true if it succesfully reads in ANY number.
    as for no == 0, you're checking the value of an uninitialised variable!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    if ( cin >> yes || no == 0 )
    What do you think this does? Do you tink it takes your input stream and puts it either into yes or no and then checks to see if it's zero? What it says is, wait for input and put it into yes, return true, or if no equals 0, return true. Since it's gonna wait on input and you're gonna have to input eventually, this will always return true and therefor you'll never get the else case to happen.

    I think what you want is:
    Code:
    cin >> choice;
    if (choice == 0) {}
    else if (choice == 1) {}
    Also don't make calls to the system.
    Last edited by SlyMaelstrom; 11-21-2005 at 06:15 PM.
    Sent from my iPad®

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    cin << choice;
    cin>>choice;
    ???

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Whoops, my bad. Typed that quick. Edited.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    As I mentioned in your other thread, a common way to handle bad input is this:
    Code:
    while (!(cin >> yes) || (yes != 0 && yes != 1))
    {
      // clear any fail state from the user entering characters instead of a number
      cin.clear();
    
      // remove all bad characters from the input stream to start fresh
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
      // Prompt again here...
    
    }
    
    // Use yes as you would normally.
    Note that you have to #include <limits> to use numeric_limits. You can also just use a large number there to make sure all bad characters are ignored.

    That code will keep prompting until 0 or 1 is input. Then you can handle 0 or 1 as you wish.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    Code:
    Daved
    i can use this one because this one doesn't actually do what i need it to do. although it has the clear values i need it to repeat the message over and i can't find a code like that yet.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    Quote Originally Posted by SlyMaelstrom
    Code:
    if ( cin >> yes || no == 0 )
    What do you think this does? Do you tink it takes your input stream and puts it either into yes or no and then checks to see if it's zero? What it says is, wait for input and put it into yes, return true, or if no equals 0, return true. Since it's gonna wait on input and you're gonna have to input eventually, this will always return true and therefor you'll never get the else case to happen.

    I think what you want is:
    Code:
    cin >> choice;
    if (choice == 0) {}
    else if (choice == 1) {}
    Also don't make calls to the system.

    also your code doesn't help either because when i use it makes me do the samething i had before which would make it only work with if a person enters 0 then it will repeat the same statement over... i need it to repeat the statement that it


    Code:
     
    //adds the iostream library to the program
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define BOOL_H
    #include <limits>
    #undef true
    #undef false 
    //informs the compiler you are using the standard library set
    using namespace std;
    
    const int LOW = 1;
    const int HIGH = 6;
    int firstDie;
    int secondDie;
    int Choice;
    int yes;
    int no;
    
    /*
    Variables to hold random values
    for the first and the second die on
    each roll.
    */
    
    
    int main ()    
    {
       
       time_t seconds;
       time(&seconds);
       srand((unsigned int) seconds);
       for ( ;; )
       {
          cout << "Round 1 \n";
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
    cin >> Choice;
    if (Choice == 0) {}
    else{      {firstDie = rand() % (HIGH - LOW + 1) + LOW;
    (secondDie = rand() % (HIGH - LOW) + 1) + LOW;
    cout << "you rolled a "<< firstDie << " and a " << secondDie << endl; break;}  }
      
       }
           system("pause\n\n\n");
       return 0;
    }
    i kind of had that part figured out i just needed some help to make it like this.

    example : http://img400.imageshack.us/my.php?image=pig16ft.png

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I've modified your code a bit. You will see that some lines, especially headers, are no longer present. You did not need them. Also, it doesn't really matter if you use "\n" or endl. However, it's way easier to use only one and stay with it.

    The variables are local to the main function, you should not use global variables when you don't need them. I have deleted some of them, you did not use them. I have changed your endless for-loop to a while loop with a controlling variable. Both ways work, it's a matter of personal preference. For me, a for-loop is always something countable, a while loop is controlled by a conditional expression like your loop. Both can be twisted to work the other way though.

    time.h has been replaced by ctime. Stay consistent with the headers, don't mix C and C++ if you don't have to.

    PHP Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    //informs the compiler you are using the standard library set
    using namespace std;

    const 
    int LOW 1;
    const 
    int HIGH 6;

    int main ()    
    {
        
    int firstDie    0;
        
    int secondDie    0;
        
    int choice        0;
        
    bool running    true;
        
    int  round        1;
       
        
    srand time(NULL) );

        while( 
    running )
        {
            
    cout << "Round " << round << endl;
            
    cout << "Would you like to roll this round? [1 for yes, 0 for no]:" << endl;
            
            
    cin >> choice;

            if( 
    choice != 0)
            {
                
    firstDie rand() % (HIGH LOW 1) + LOW;
                
    secondDie rand() % (HIGH LOW 1) + LOW;

                
    cout << "you rolled a "<< firstDie << " and a " << secondDie << endl;
            }
            else 
    /* choice == 0 */
            
    {
                
    running false;
            }

            
    round++;
          }
     
        
    system("pause");
       
        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> i can use this one because this one doesn't actually do what i need it to do. although it has the clear values i need it to repeat the message over and i can't find a code like that yet.

    Sure you can use that code. It wasn't meant as a finished solution, it was meant to be understood and added to your code. If you noticed, there is a comment in the code that says prompt again here. That means you add your message there. If you do that, it will of course repeat the message.

    The while loop runs while the input is bad. The input is bad if cin >> yes returns false, or if it succeeds but yes does not equal 1 and yes does not equal 0. Either way, if the input is bad, then you clear the stream of any errors and ignore any bad or leftover input. Then you prompt again. That is exactly what my code does. You can add that inside your own for loop and it will do what you want.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok but how do you make the code when a person enters 0 or anything other than 1 repeat?
    Code:
    "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    My code does repeat for anything but zero. Read or run it ( better yet, both ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    Quote Originally Posted by nvoigt
    My code does repeat for anything but zero. Read or run it ( better yet, both ).
    yawn read my question atthe top of your post then look at your answer....

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    since you're getting bored
    yawn
    Maybe it's time to stop and think for yourself a bit.

    so he gave you an example where it repeats for anything but zero.
    you want your code to repeat for anything but one.
    or anything other than 1 repeat?
    pretty simple change. Try to understand examples people give you then apply what you learned to fit your requirements
    Last edited by spydoor; 11-23-2005 at 08:22 AM.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    //informs the compiler you are using the standard library set
    using namespace std;
    
    const int LOW = 1;
    const int HIGH = 6;
    
    int main ()    
    {
        int firstDie    = 0;
        int secondDie    = 0;
        int choice        = 0;
        bool running    = true;
        int  round        = 1;
       
        srand ( time(NULL) );
    
        while( running )
        {
            cout << "Round " << round << endl;
            cout << "Would you like to roll this round? [1 for yes, 0 for no]:" << endl;
            
            cin >> choice;
    
            if( choice) // any non-zero value will return true
            {
                firstDie = rand() % (HIGH - LOW + 1) + LOW;
                secondDie = rand() % (HIGH - LOW + 1) + LOW;
    
                cout << "you rolled a "<< firstDie << " and a " << secondDie << endl;
            }
            else /* choice == 0 */
            {
                running = false;
            }
    
            round++;
          }
    
        system("pause");
       
        return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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