help with my code

I am new to coding in C++ and to coding in general. I wanted to make a simple program that will allow me to enter a number and print the results of a random number function. This is my code, please help.

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;




int dmglevelone ()
{
int dmg1 = rand() % 6 + 1;
return (dmg1);
}

int dmgleveltwo ()
{
int dmg2 = (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg2);
}

int dmglevelthree ()
{
int dmg3 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg3);
}

int dmglevelfour ()
{
int dmg4 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg4);
}

int dmglevelfive ()
{
int dmg5 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg5);
}

int dmglevelsix ()
{
int dmg6 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg6);
}
int main ()
{
srand (time(NULL));
}

int roll ()
{
int a;
cin >> a;

if (a = 1)
cout << dmglevelone();

else if (a = 2)
cout << dmgleveltwo();

else if (a = 3)
cout << dmglevelthree();

else if (a = 4)
cout << dmglevelfour();

else if (a = 5)
cout << dmglevelfive();

else (a = 6);
cout << dmglevelsix();

return 0;
}

Any help would be appreciated!
Would you please tell us what seems to be the problem?

Also, please use code tages (<>) in the box on the right side.

Something off the bat, couldn't you write the following in a simpler way:
1
2
3
4
5
int dmgleveltwo ()
{
     int dmg2 = (rand() % 6 + 1) + (rand() % 6 + 1);
     return (dmg2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int dmglevelone ()
{
int dmg1 = rand() % 6 + 1; 
return (dmg1);
}

int dmgleveltwo ()
{
int dmg2 = (rand() % 6 + 1) + (rand() % 6 + 1); 
return (dmg2);
}

int dmglevelthree ()
{
int dmg3 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg3);
}

int dmglevelfour ()
{
int dmg4 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg4);
}

int dmglevelfive ()
{
int dmg5 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg5);
}

int dmglevelsix ()
{
int dmg6 = (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
return (dmg6);
}


All this could have been done with just a single function.
something like

1
2
3
4
5
6
int dmgLevel(int nlevel)
{
      int damage;
      //code here
     return damage;
}


Therefore, all this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int a;
cin >> a;

if (a = 1)
cout << dmglevelone();

else if (a = 2)
cout << dmgleveltwo();

else if (a = 3)
cout << dmglevelthree();

else if (a = 4)
cout << dmglevelfour();

else if (a = 5)
cout << dmglevelfive();

else (a = 6);
cout << dmglevelsix();

return 0;


could have been done with 3 lines of code
1
2
3
4
5
6
7
int main()
{
     int a;
     cin >> a
     cout << dmgLevel(a);
     return 0;
}
Last edited on
First of all thank you very much ThangDo! I am very new to any type of programming only having studied it for less than 2 weeks and my brain seems to want to make everything drawn out.

As for the problem, the code does not throw up any errors, it simply isn't the right code for what I want to do. I would like to know why. I want to be able to type a number between 1 and 6 and return one of the rand() functions that I typed out. Also I don't know if there is a simpler way to write it.
closed account (o3hC5Di1)
Hi there,

A Switch construct is mad efor this purpose I would think:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (input)
{
    case 1:
        dmglevelone();
        break;

    case 2:
        dmgleveltwo();
        break;

    //and so on

    default:
        std::cout << "invalid input";
}


This code assumes that you get the user inserted number into the variable "input", but you can customise off course.

On a sidenote, if you use ThangDo's code, you would do:

1
2
3
4
5
6
7
8
9
10
11
12
int dmgLevel(int nlevel)
{
      int damage;
      //code here
     return damage;
}
int main () 
{
    //get input

    std::cout << dmgLevel(input);
}


That would work as well.
Sorry for keeping it a bit vague, but you'll understand better if you experiment for yourself rather than us just telling you what to do.

For more info on switch constructs: http://cplusplus.com/doc/tutorial/control/#switch

Hope that helps.
All the best,
NwN
Last edited on
Have a look at this link for more info on random generator also.
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Topic archived. No new replies allowed.