Got errors in my second ever made program

Whats wrong?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <cstdlib>
using namespace 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";
	else if (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;
	else if (powerused > 0 && userinput == 2)
		score = (rand() % 65 + 1) + 25;
	else if (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;
}
Last edited on
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.
On line 88, what is the value of flying?
Whats wrong?

With what?

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/
Last edited on
Topic archived. No new replies allowed.