-2

I am new to C++ and using functions is still a bit unclear. I have a program started as follows below, but I keep getting compile errors that I do not understand how to fix. I feel like this is a simple fix, but I just am not seeing how to fix it.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int roll ();

int main()
{   
    srand(time(NULL));
    int die1;
    diel = int roll();
    cout << die1;


}

int roll()

{   
    return rand()%6+1;



} 

Then, the compile error

dice.cpp: In function ‘int main()’:
dice.cpp:15: error: ‘diel’ was not declared in this scope
dice.cpp:15: error: expected primary-expression before ‘int’
dice.cpp:15: error: expected `;' before ‘int’
j.Shry
  • 33
  • 5
  • 1
    Here is a nice list of [C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Jan 14 '17 at 15:50
  • 4
    looks like a typo... die1/diel. int diel = roll(); cout << diel; – Biggy Smith Jan 14 '17 at 15:52
  • I really dislike how this question has so many answers, will this question help people in future? Probably not, it's a super simple mistake by the op that if actually reading the compiler errors would most likely distinguish that there was a typo. Instead people are hounding the answers for an easy few points of rep. (Every answer basically just records line 2 of the compiler error) – Halfpint Jan 14 '17 at 16:11

4 Answers4

2

Well, according to the error message you firstly need to fix the typo (you have diel instead of die1). Secondly there is no need for int before roll().

So, you need

die1 = roll();

instead of

diel = int roll();
grepcake
  • 2,928
  • 3
  • 15
  • 25
-1

There seems to be a typo, die1 is declared, but used diel. Note "one" vs "ell".

second syntax error is the function call itself. You call the function as :

die1 = roll();

The reasoning behind the c++ type declaration syntax is (if i remember correctly): if you have

int f();
int x;

then any place you want an int value, you can use f() or x.

sas1138
  • 336
  • 4
  • 16
-1

There is couple way to do this ether way you want to create a function based on type you want to return or you could use reference of a variable as example :

int roll ();
void roll(int& var);

int main()
{   
    srand(time(NULL));
    int die1;
    die1 = roll(); // assign random value that was generated in roll funtion
    roll(die1 );// call roll function and 50 to die1
    cout << die1;


}

int roll()

{   
    return rand()%6+1; // will return a random value 
} 

void roll(int& var)
{
    var += 50; // will add 50 to variable you are passing
}
yahoo5000
  • 458
  • 5
  • 17
-2

int roll();

int main() {

srand(time(NULL));
int die1;
die1 = roll();
cout << die1;
system("pause");

}

int roll()

{

return rand() % 6 + 1;

}

  • Welcome to SO. This is not a very good answer. A) you haven't formatted properly. B) Your answer is just code. SO is not a code writing service; please provide some explanation of what you changed and why. – Foon Jan 14 '17 at 17:46