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