0
#include <iostream>
#include <string>
#include <random>
#include <Windows.h>
using namespace std;


    int playerHP = 20;
    int enemyHP = 20;
    int playerAtk =(rand()%4 + 5); //Find a random number between 5 and 8
    int playerDef = (rand()%4 + 5);
    int playerAgi = (rand()%4 + 5);
    int enemyAtk = (rand()%4 + 4); //Find a random number between 5 and 7
    int enemyDef = (rand()%4 + 4);
    int enemyAgi = (rand()%4 + 4);
    cout<<"---------------"<< endl; // what in the sam hill is going on
    cout<<"FIGHTER STATS"<< endl;
    cout<<"----------------"<<endl;
    cout<<"Player Stats:"<<endl;
    cout<<"HP "<<playerHP<<endl;
    cout<<"ATK "<<playerAtk<<endl;
    cout<<"DEF "<<playerDef<<endl;
    cout<<"AGI "<<playerAgi<<endl;
    cout<<"ENEMY STATS:"<<endl;
    cout<< "HP "<<enemyHP<<endl;    
    cout<<"ATK "<<enemyAtk<<endl;
    cout<<"DEF "<<enemyDef<<endl;
    cout<<"AGI "<<enemyAgi<<endl;

I can't seem to figure out why my cout statements are creating so many bugs in my program. This is obviously not my whole program, but I wanted to keep things short and sweet. I am getting the errors C2143:syntax eerror : missing ';' before '<<', C4430: missing type specifier-int assumed, and C2086 'int cout':redefinition on almost all of my cout statements and I can't figure out why. Thanks for any and all help and please dumb things down as much as possible, this is my first C++ program ever.

Bob
  • 1,344
  • 3
  • 29
  • 63
  • 9
    statements should go inside a function. Are you trying to compile exactly the code you've posted? If so, try moving the code into a `main` function. i.e. `int main() { /* code goes here */ }` – simonc Aug 30 '13 at 16:16
  • 2
    @simonc - that should probably be an answer, since it would lead to the kinds of errors that are mentioned. – Pete Becker Aug 30 '13 at 16:19
  • 2
    There's no reason to include `windows.h`, `random`, or `string` here. There is a need for `cstdlib`, though, which you don't have, but I would change the random number method instead of the header (`rand()` is bad and this has bias). You also didn't change the seed from the default of 1, making it extremely easy to know which numbers you'll get. A [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would do you well. – chris Aug 30 '13 at 16:22
  • @PeteBecker I've copied/expanded my comment into an answer now – simonc Aug 30 '13 at 16:29

1 Answers1

3

Assuming you've posted exactly the code you're trying to compile, statements like

cout<<"---------------"<< endl;

need to be inside a function.

Earlier lines wouldn't have caused an error as its valid to declare variables with global scope outside of any function. It isn't great practice to do this though and you certainly shouldn't make variables global if they're just required by a single function.

Try moving all your code into a main function. i.e.

int main()
{
    int playerHP = 20;
    int enemyHP = 20;
    int playerAtk =(rand()%4 + 5);
    // rest of your code goes here
}

Once you get your code compiling and running, you'll find that your random numbers are always initialised to the same values. You need to call srand, choosing a value that varies between runs, before calling rand. The current time is an easy seed to choose if you don't mind that it only changes once per second

int main()
{
    int playerHP = 20;
    int enemyHP = 20;
    srand(time(NULL));
    int playerAtk =(rand()%4 + 5);
simonc
  • 41,632
  • 12
  • 85
  • 103