954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with game! & invalid suffix etc

Hey, i was just going to start making a game where srand made a random number between 1&99 and that is the power you would hit of someone else, the one to defeat the other person won,

I got some prefix errors etc :

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int 1attack, 2attack, 1hp, 2hp;
    char attack;
    
    cout << "Player 1, Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         1attack = rand() % 99 + 1; 
         cout << "You hit " << 1attack << "!";
    break;
    
    case default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    getch();
}


I was going to put this on to ask for help with how to do things with the game so i'll leave this thread for help with the errors and maybe some help with the game :)?

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

Line 24, it just default, not case default

default:
         cout << endl << "INVALID SELECTION!";
    break;


And you cannot declare variables starting with numeral. Try this

int attack, attack2, hp2, hp;
hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

Thank you, i still get:
invalid suffix
expected ; before int

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 
i still get: invalid suffix expected ; before int


Then you most probably still use there a variable whose name starts with a digit, so please double-check the code.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Call srand() just ONCE at the start of the program, not every time you want a rand().

At best, it achieves nothing. At worst, it prompts "Why does rand() always return the same value" questions on the message boards.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Thanky you for poiting out the variable parts, now for the game bit, would i use a while loop for when the hp is > 0?

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

Edit: fixed that error, still working on game

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You