Hi, I'm trying to write a program that basically rolls some dice "behind the scenes" and outputs to the user the desired die to roll, how many times to roll it, and how many rolls it succeeded in getting that number the user picked. I already have the bulk of the code completed but I'm new to these return-value functions and hit a road block. Can someone please show me where I went wrong and what I can do to fix it? Thanks!
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int rollDice(int rollCount, int num);
int main()
{
int sum = 0;
int num = 0;
int rollCount = 0;
int outCome = 0;
cout << "What number do you want to roll? (2 - 12)" << endl;
cin >> num;
cout << "How many times do you want to roll the dice?" << endl;
cin >> rollCount;
outCome = rollDice(int rollCount, int num)
cout << "Out of " << rollCount << " rolls you rolled a " << num << " on " << outCome << " rolls!" << endl;
system("pause");
return 0;
}
int getRollDice()
{
int die1 = 0;
int die2 = 0;
int sum = 0;
int count = 0;
srand(time(0));
for (count = 0; count <= rollCount;)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
if (getRollDice() == num)
{
count++;
return sum;
}
}
}
These are the compiler warnings / errors, you can get this yourself by using the gear icon at the top right of your code, turn on all the warning options. If you have compiler output then you should post here verbatim.
In function 'int main()': 21:21: error: expected primary-expression before 'int'
21:36: error: expected primary-expression before 'int'
11:6: warning: unused variable 'sum' [-Wunused-variable] In function 'int getRollDice()':
38:27: error: 'rollCount' was not declared in this scope
44:24: error: 'num' was not declared in this scope
50:1: warning: control reaches end of non-void function [-Wreturn-type]
Line 21 remove the types, the are used in the function declaration / definition
Lines 38 & 44: Variables have scope - the vars in main aren't seen in functions unless you pass them to the function as arguments.
Line 50: you must return an int, but your return is inside an if , so it may not happen.
Line 7 : you have a function declaration, but it is not defined, and called on line 21
Thanks for the quick reply.. I changed a couple of things according to what you caught but this is exactly where I was before I tried changing code around to fit my objective. The program breaks after the first or second output.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int rollDice(int rollCount, int num);
int main()
{
int num = 0;
int rollCount = 0;
int outCome = 0;
cout << "What number do you want to roll? (2 - 12)" << endl;
cin >> num;
cout << "How many times do you want to roll the dice?" << endl;
cin >> rollCount;
outCome = rollDice();
cout << "Out of " << rollCount << " rolls you rolled a " << num << " on " << outCome << " rolls!" << endl;
system("pause");
return 0;
}
int rollDice(int rollCount, int num)
{
int die1 = 0;
int die2 = 0;
int sum = 0;
int count = 0;
srand(time(0));
for (count = 0; count <= rollCount;)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
if (rollDice() == num)
{
count++;
}
return sum;
}
}
Do I need to add the same parameters to my function everywhere I call for it? I know I'm almost asking for the code from someone but I'm also trying to learn how these things work..
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int RollDice();
int main()
{
srand (time(0));
int num, outCome=0;
int rollCount, hit = 0;
cout << "What number do you want to roll? (2 - 12)" << endl;
cin >> num;
cout << "How many times do you want to roll the dice?" << endl;
cin >> rollCount;
for (int i=1;i<rollCount;i++)
{
outCome = RollDice();
if (outCome==num) {hit++;}
}
cout << "Out of " << rollCount << " rolls you rolled a " << num << " on " << hit << " rolls!" << endl;
system ("pause");
return 0;
}
int RollDice(){
int die1=rand()%6+1;
int die2=rand()%6+1;
cout << "Die1 = " << die1 << " Die2 = "<< die2 << endl;
int sum=die1+die2;
return sum;
}
Do I need to add the same parameters to my function everywhere I call for it?
every time one call s a function, it must have arguments that match the signature of the function - that is the same number of arguments with the same types as in the function definition. The values you send to the function can vary of course.
An argument is a variable that one calls a function with, a parameter is a variable inside the parentheses of the function definition:
1 2 3
int rollDice(int rollCount, int num) // <--- parameters rollCount and num
{
int die1 = 0;
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int RollDice(int); // prototypes don't need the names of the variables, just the type
int main()
{
srand (time(0)); // Only called one time
int num, outCome=0;
int rollCount, hit = 0; // added a variable
cout << "What number do you want to roll? (2 - 12)" << endl;
cin >> num;
cout << "How many times do you want to roll the dice?" << endl;
cin >> rollCount;
for (int i=1;i<rollCount;i++) // Put your loop here to call the function *i* times
{
outCome = RollDice(num); // pass your variables here
if (outCome==num) {hit++;} // here you do your comparisons
}
cout << "Out of " << rollCount << " rolls you rolled a " << num << " on " << hit << " rolls!" << endl;
system ("pause");
return 0;
}
int RollDice(int num){ // intialize your parameter here
int die1=rand()%6+1;
int die2=rand()%6+1;
cout << "Die1 = " << die1 << " Die2 = "<< die2 << endl; // optional printing
int sum=die1+die2;
return sum; // return sum
}
Wow thanks for all the help everyone, and for the comments! I get why it didn't work before and why it should work now, anyway. This is my first time dealing with these return-value functions so I do appreciate it.