Writing a bool function

This is a discussion on Writing a bool function within the C++ Programming forums, part of the General Programming Boards category; ok my function will be bool getRoll () im trying to write a function that will ask the user if ...

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

    Writing a bool function

    ok my function will be bool getRoll ()

    im trying to write a function that will ask the user if they would like to roll for a particular round. The user can input ANY character but only a '1' will allow the user to roll that round. This function will output a boolean.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    bool getRoll()
    {
         char input;
         cout << "Roll dice? " << endl;
         cin >> input;
         input = tolower(input); // include the header iomanip
         if(input == 'y') return true;
         return false;
    }
    Something quick i whipped up, it is fairly simple as you can see

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok so here is where i put the bool function in.

    Code:
    
    //adds the iostream library to the program
    #include <iomanip>
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define BOOL_H
    #include <limits>
    #undef false
    #undef true
    //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 x;
    int Choice;
    int yes;
    int no;
    bool getRoll;
    
    
    /*
    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( x = 1; x<=5; ++x )
       {
          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;
              }
        }// end of for loop
           system("pause\n\n\n");
       return 0;
    }
    bool getRoll(firstDie,secondDie)
    {
         char input;
         cout << "Roll dice? " << endl;
         cin >> input;
         input = 1 || 0;
         if(input == 1) return true;
         else return false;
    }
    i don't know how to use bool functions in code...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Remove the "Would you like to roll this round..." line, as well as the cin >> Choice from the for loop inside main. Instead, you want to call your function. If the function returns true, then do the code with firstDie, secondDie, rand(), etc. To call a function, just type its name followed by parentheses.

    Now, there are some issues with your version of this function. First, you have it taking firstDie and secondDie as parameters. Is that because your instructor told you to do that? The code inside the function doesn't use those, so unless your assignment says that you must have them, I would remove them. Then you can call the function like this: getRoll().

    Some other issues include this line: input = 1 || 0; which does not do what you think it does (it actually always sets input to 1). Also, if input is a char, and the user types 1, you will want to compare input to '1' with the single quotes signifying that it is a character. The character '1' is different than the number 1.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok so change
    Code:
    bool getRoll(firstDie,secondDie)
    {
         char input;
         cout << "Roll dice? " << endl;
         cin >> input;
         input = 1 || 0;
         if(input == 1) return true;
         else return false;
    }
    to

    Code:
    bool getRoll(firstDie,secondDie)
    {
         int input;
         input = 1 || 0;
         if(input == 1) return true;
         else return false;
    }
    Last edited by findme; 11-29-2005 at 04:43 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No. That's not quite right, (although you did change it to an int from char, which is good). If you don't understand something, feel free to ask a specific question about my help and I will clarify it further.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You still have a lot of baggage that you don't need. Maybe your should go back and reread the post you were yawning at earlier.

    Anyway, someone already told you, but you ignored it, so I'll tell you again: "input = 1 || 0;" is not doing what you expect it to do. Tell us what you want to do with that line and we can make suggestions how to achieve it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 07:46 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 07:43 PM

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