-
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.
-
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
-
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...
-
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.
-
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;
}
-
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.
-
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.