I'm super new to c++ obviously. I decided to take on a challenge and see if I could use my limited knowledge of variables and functions to make a simple battle script where you fight a monster by rolling a random number. (Wanted to work on the skills i've learned so far.) By the way i'm using Orwell Dev C++ if that's something you need to know. I've been working on it for hours and I'm getting nowhere.
Too list the many problems
-"elses" arn't connected to "ifs"
-None of the "gotos" work
-I'm pretty sure the thing I did to check if they are dead is wrong
The debug log
Compiler: TDM-GCC 4.6.1 64-bit
Building Makefile "C:\Dev-Cpp\Makefile.win"
Executing make...
mingw32-make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -g3
main.cpp: In function 'int monster(int, int)':
main.cpp:17:2: error: 'else' without a previous 'if'
main.cpp: In function 'int player(int, int)':
main.cpp:30:5: error: 'else' without a previous 'if'
main.cpp: In function 'int main()':
main.cpp:48:18: error: expected primary-expression before 'int'
main.cpp:48:28: error: expected primary-expression before 'int'
main.cpp:49:15: error: expected primary-expression before 'int'
main.cpp:49:15: error: expected ')' before 'int'
main.cpp:53:5: error: expected '}' before 'else'
main.cpp:53:14: error: expected primary-expression before 'int'
main.cpp:53:14: error: expected ')' before 'int'
main.cpp:51:13: error: label 'died' used but not defined
main.cpp: At global scope:
main.cpp:57:3: error: expected unqualified-id before 'else'
main.cpp:59:10: error: expected constructor, destructor, or type conversion before '(' token
main.cpp:60:7: error: expected unqualified-id before 'goto'
main.cpp:62:5: error: 'yourattack' does not name a type
main.cpp:75:3: error: expected unqualified-id before 'else'
main.cpp:77:7: error: expected unqualified-id before 'goto'
main.cpp:81:1: error: expected declaration before '}' token
mingw32-make.exe: *** [main.o] Error 1
Execution terminated
--------------------------------------------------------------------------------
The actual script
--------------------------------------------------------------------------------
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
|
#include <cstdlib>
#include <iostream>
using namespace std;
int mhealth =100; // monster health
int health = 100; // your health
int attack = rand() / 1000; // amount of damage attack does (variables later)
int attackroll = rand(); // roll to wether or not attack is successful
int monster(int dead, int alive)
{
if (mhealth <= 0) // Checks if monster's health is 0 or lower
cout << "You killed the monster!" << endl;
return dead;
else
cout << "He's still alive!" << endl;
system("PAUSE");
return alive;
}
int player(int dead, int alive)
{
if (health <= 0) // Checks if your health is 0 or lower
cout << "You were killed!" << endl;
system("PAUSE");
return dead;
else
cout << "Your still standing!" << endl;
system("PAUSE");
return alive;
}
int main()
{
cout << "Testing a battle. All coded by Thomas" << endl;
cout << "A monster is attacking you!" << endl;
cout << "Your Health: "<< health << "Monster Health: "<< mhealth << endl; //Displaying both current healt amounts
int attack;
int attackroll;
monsterattack:
cout << "Monster's turn" << endl;
if (attackroll > 2400) // Seeing if the hit was a success or not, 75% chance to hit
{
int health = health - attack; // your health is now your previous health minus the attack
cout << "Attack success! Your health now: "<< health << endl;\
monster(int dead, int alive);
if (int dead)
system("PAUSE");
goto died;
else if (int alive)
system("PAUSE");
return 1;
}
else
cout << "Monster missed!" << endl;
system("PAUSE");
goto yourattack;
yourattack:
if (attackroll > 2800) // Seeing if the hit was a success or not, 87.5% chance to hit
{
int mhealth = health - attack; // monster health is now his previous health minus the attack
cout << "Attack Success! Monster's health now: " << mhealth << endl;
player(int dead, int alive)
if (int dead)
system("PAUSE");
goto dead;
else if (int alive)
system("PAUSE");
return 1;
}
else
cout << "You missed!" << endl;
goto monsterattack;
}
|