I am beginning programming in C++ and I wrote this simple program. It's a game where you have a cannon and you choose the angle to fire the cannon to kill enemies.
//War GAme by NW
//A simple war game written in C++
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
usingnamespace std;
int endist; //distance of enemy
int cbleft = 10; //cannonballs left
int killed = 0;
int Gravity;
int Velocity;
int in_angle;
int overshot; //distance you overshoot by
char done;
int distance; //distance of shot
int time_in_air;
void enemydist() {
endist = rand() % 1000 + 1;
}
void Fire() {
Velocity = 200.0;
Gravity = 32.2;
time_in_air = (2.0 * Velocity * sin(in_angle)) / Gravity;
distance = round((Velocity * cos(in_angle)) * time_in_air);
}
void Startup() {
cout << "Welcome to Artillery." << endl;
cout << "You are in the middle of a war and being charged by thousands of enemies" << endl;
cout << "You have one cannon, which you can shoot at any angle." << endl;
cout << "You only have 10 cannonballs for this target.." << endl;
cout << "Let's begin..." << endl;
}
int main() {
int endist;
int shots = 0;
int cbleft = 10;
int killed = 0;
int Gravity;
int Velocity;
int in_angle;
int overshot;
char done;
int distance;
void enemydist() {
srand (time(null));
endist = rand() % 1000 + 1;
}
void Fire() {
Velocity = 200; // initial velocity of 200 ft/sec
Gravity = 32.2; // gravity for distance calculation
// in_angle is the angle the player has entered, converted to radians.
time_in_air = (2.0 * Velocity * sin(in_angle)) / Gravity;
distance = round((Velocity * cos(in_angle)) * time_in_air);
}
void Startup() {
cout << "Welcome to Artillery." << endl;
cout << "You are in the middle of a war and being charged by thousands of enemies" << endl;
cout << "You have one cannon, which you can shoot at any angle." << endl;
cout << "You only have 10 cannonballs for this target.." << endl;
cout << "Let's begin..." << endl;
}
start:
Startup();
fire:
if (cbleft <= 0) {
goto end;
}
enemydist();
cout << "The enemy is " << endist << " feet away." << endl;
cout << "What angle would you like to shoot at?" << endl;
cin >> in_angle;
Fire();
cbleft = cbleft - 1;
if (distance == endist) {
killed = killed + 1;
goto hit;
}
if (distance != endist) {
goto miss;
}
hit:
do {
cout << "I see another more of them, go again?(y/n)" << endl;
cin >> done;
} while (done != 'n');
if (done == 'n') {
goto end;
}
miss:
overshot = endist - distance;
cout << "You overshot by " << overshot << " feet" << endl;
goto fire;
end:
cout << "You killed " << killed << " enemies in 10 shots." << endl;
cout << "I see another more of them, go again?(y/n)" << endl;
cin >> done;
if (done != 'n') {
goto start;
}
else {
cin.get();
}
}
When I compile it, I get these errors.
wargame.cpp: In function 'void Fire()': wargame.cpp:29:1: error: reference to 'distance' is ambiguous wargame.cpp:17:5: error: candidates are: int distance In file included from c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl _algobase.h:67:0, from c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/bits/cha r_traits.h:41, from c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/ios:41, from c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/ostream: 40, from c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/iostream :40, from wargame.cpp:3: c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_iterator_base_funcs.h :114:5: error: template typename std::iter ator_traits::difference_type std::distance(_InputIterator, _InputIterator) wargame.cpp: In function 'int main()': wargame.cpp:51:18: error: a function-definition is not allowed here before '{' t oken wargame.cpp:55:13: error: a function-definition is not allowed here before '{' t oken wargame.cpp:63:16: error: a function-definition is not allowed here before '{' t oken
What's wrong with my code? Anything that sticks out?
EDIT: I changed some stuff using your suggestions and it compiles fine. But now when I run the program it gives the enemy distance as 1979407300 every time. What's causing this? Here's my edited code:
//War GAme by NW
//A simple war game written in C++
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
usingnamespace std;
int endist; //dist of enemy
int cbleft = 10; //cannonballs left
int killed = 0;
int Gravity;
int Velocity;
int in_angle;
int overshot; //dist you overshoot by
char done;
int dist; //dist of shot
int time_in_air;
void enemydist() {
endist = rand() % 100 + 1;
}
void Fire() {
Velocity = 200.0;
Gravity = 32.2;
time_in_air = (2.0 * Velocity * sin((double)in_angle)) / Gravity;
dist = round((Velocity * cos((double)in_angle)) * time_in_air);
}
void Startup() {
cout << "Welcome to Artillery." << endl;
cout << "You are in the middle of a war and being charged by thousands of enemies" << endl;
cout << "You have one cannon, which you can shoot at any angle." << endl;
cout << "You only have 10 cannonballs for this target.." << endl;
cout << "Let's begin..." << endl;
}
int main() {
int endist;
int shots = 0;
int cbleft = 10;
int killed = 0;
int Gravity;
int Velocity;
int in_angle;
int overshot;
char done;
int dist;
start:
Startup();
fire:
if (cbleft <= 0) {
goto end;
}
enemydist();
cout << "The enemy is " << endist << " feet away." << endl;
cout << "What angle would you like to shoot at?" << endl;
cin >> in_angle;
Fire();
cbleft = cbleft - 1;
if (dist == endist) {
killed = killed + 1;
goto hit;
}
if (dist != endist) {
goto miss;
}
hit:
do {
cout << "I see another more of them, go again?(y/n)" << endl;
cin >> done;
} while (done != 'n');
if (done == 'n') {
goto end;
}
miss:
overshot = endist - dist;
cout << "You overshot by " << overshot << " feet" << endl;
goto fire;
end:
cout << "You killed " << killed << " enemies in 10 shots." << endl;
cout << "I see another more of them, go again?(y/n)" << endl;
cin >> done;
if (done != 'n') {
goto start;
}
else {
cin.get();
}
}
wargame.cpp: In function 'void Fire()': wargame.cpp:29:1: error: reference to 'distance' is ambiguous
Prime reason why you should not use usingnamespace. There's a function named 'distance' defined in the std namespace. When you use this clause, you're dumping the entire contents of the namespace into global namespace. The result is that you have two global symbols that are named the same and no way to tell them apart.
wargame.cpp:51:18: error: a function-definition is not allowed here before '{' token
You can't define functions inside of other functions.
One of the problems is the lack of indentation. It makes the code very difficult to read. At the very least there may be mismatched braces { }. Without indentation it's hard to even guess where they belong.
One relatively straightforward problem is with the sin() and cos() functions. You should normally pass a parameter of type double, your code uses int, which is ambiguous - the compiler doesn't know whether to use float, double or long double.
Solution sin((double)in_angle) instead of sin(in_angle)
There are a number of your variables that should be double in the first place Velocity, Angle etc. Integers don't have a fraction part, so this can cause all kinds of problems.
The BIG GLARING THING is you use of goto. It is really very very bad practice, Vlad has advised in the past, to forget that goto even exists in the language. I agree entirely.
Instead of goto, design your code with loops (while or for). Alternatively use functions to achieve want you want. Most likely you will have a mixture of those 2 approaches.
Lines 5 & 6: Why have you included these files? Only include files you need.
Line 7: Should be #include<cmath>
Line 8: Further to what helios said, So put std:: before each std thing you need to use - std::cout, std::cin, std::endl to start with. There are many others, look in the reference section at the top left of this page, all the containers - string, vector etc, all the algorithms- find, sort etc. Pretty much most of the stuff in the reference section needs to have std:: before it.
It would be well worth your time to read some of the reference section - especially look at the code snippets, some of the articles and other documentation.
Unfortunately the code snippets have usingnamespace std;. This is something I wish could be changed, as it just reinforces bad practice, and is not helpful to newbies - as you have just discovered.