#include <iostream>
#include <Windows.h>
#include <time.h>
#include <cstdlib>
usingnamespace std;
int Throw(int powerused, int userinput);
void menu();
int Power();
int main(){
int userinput;
int score;
int powerused;
menu();
cin >> userinput;
switch (userinput){
case 1:
system("cls");
cout << "You've succesfuly choosed golden knife\n";
break;
case 2:
system("cls");
cout << "You've succesfuly choosed diamond knife\n";
break;
case 3:
system("cls");
cout << "You've succesfuly choosed wooden knife\n";
break;
default:
system("cls");
cout << "Something wrong 404 :D\n";
break;
}
powerused = Power();
score=Throw(powerused, userinput);
system("cls");
if (score >= 75)
cout << "You've hit the tree and scored " << score << "points\n";
elseif (score > 0 && score <75)
cout << "You didnt hit the tree but still scored " << score << "points\n";
else
cout << "You have missed the tree\n"
<< "Game over\n";
system("pause");
}
void menu(){
cout << "Hello\n"
<< "You've just entered knife throwing game\n"
<< "Choose your knife!!!\n"
<< "1) Golden knife\n";
Sleep(1000);
cout << "2) Diamond knife\n";
Sleep(1000);
cout << "3) Wooden knife\n";
Sleep(1000);
}
int Power(){
int powerused;
cout << "How much power do you want to use to throw your knife?\n"
<< "(enter something that is less than 10 and more than 0\n";
cin >> powerused;
return powerused;
}
int Throw(int powerused, int userinput){
int throwRes;
bool flying;
int score;
srand(time(0));
system("cls");
cout << "You are about to throw ur knife\n";
Sleep(1000);
cout << "You've just did it!\n";
Sleep(1000);
cout << "Your knife is in the air now and it is flying to the tree\n";
while (flying){
cout << ". ";
Sleep(2500);
}
if (powerused > 0 && userinput == 1)
score = (rand() % 65 + 1) + 15;
elseif (powerused > 0 && userinput == 2)
score = (rand() % 65 + 1) + 25;
elseif (powerused > 0 && userinput == 3)
score = (rand() % 65 + 1) + 5;
else
score = 0;
flying = false;
cout << "Your knife is no longer in the air!\n";
return score;
}
The question is wrong. You should tell us what you're trying to achieve, what the error/problem is, and then ask us what's wrong(bonus points for throwing your guess, even if it's wrong).
Throwing your code at others without explaining is also wrong.
tl;dr: tell us what's the problem and what have you tried so far instead.
Line 78: flying is an unintialized variable. Hint: It contains garbage.
Line 88: You're testing garbage.
Have no idea if that's the problem you asking about or not.
Line 80: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main(). http://www.cplusplus.com/reference/cstdlib/srand/