Sign in to follow this  

need help, don't know how to fix this error

This topic is 4040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think you need to make a function prototype. you can do this by putting:

int attack();

before your "main" and secondly when you call the function delete the void, so your coed should be like:

#include <iosteam>

int attack();

int main{
// Do stuff here
if(input == 'a')
attack();
return 0;
}

int attack(){
// Attack stuff here
}



That should solve your problem, hopefully...

Share this post


Link to post
Share on other sites
Quote:
Original post by alway616
   if (input == 'a')
void attack();
return 0;
}

int attack() <------------------------------line with errors
{

Probably what's confusing the compiler. There may be other errors outside of the snippet you posted. If that change doesn't fix things post more code, inside [source] tags.

Σnigma

Share this post


Link to post
Share on other sites
The only thing I can say for sure is that in this bit of code:
if (input == 'a')
void attack();
return 0;
}
You're missing an opening brace, and 'void' is an error (I'm not sure what your intent is in including it there).

Beyond that, you'll probably need to post in its entirety the function from which the above code is taken.

[Edit: Reading the above posts, I see you may not be missing an opening brace. However, without seeing the entire function we can't really tell for sure.]

Share this post


Link to post
Share on other sites
the void is there because originaly the function was void not int, and the problem was there before i changed it, infact, thats why i changed it


here, ill post the entire program:

#include <iostream>
#include <string>

using namespace std;




int hitpoints = 100;
int evasion = 10;
int power = 10;
int defense = 10;
int accuracy = 10;
int money = 100;
int area = 2;
int xp = 1;
int level = 1;

int attack();
int main()
{
cout<<"Greetings Gladiator. Welcome to Arena Quest."<<endl;
while(1<2)
{
char input;
cin >> input;
if (input == 'a')
int attack();
return 0;
}






int attack()
{
while (hitpoints > 0 && enhitpoints > 0)
{
t=0;
while (t<500)
t=+1;
accuracy-1/4*enevasion=hitchance;
power-1/4*endefense=hitpower;
srand(time(0));
int percent=rand();
int hitmiss=percent%100;
if(hitmiss<=hitchance)
{
srand(time(0));
percent+rand();
int hitstr=percent%100;
int atten=hitpower*hitstr;
}
else
int atten = 0;
enhitpoints=-atten;
cout<<"You dealt"<<atten<<"damage. The enemy has"<<enhitpoints<<"hitpoints left."<<endl;
xp=xp+2*atten;


int enhitchance=enaccuracy-1/4*evasion
int enhitpower=enpower-1/4&defense
srand(time(0));
percent = rand();
hitmiss=percent%100;
if (enhitchance>=hitmiss)
{
srand(time(0));
percent=rand();
int enhitstr=percent
int enatt=enhitpower*enhitstr;
}
else
int enatt=0;
hitpoints=-enatt;
cout<<"The enemy did"<<enatt<<"damage. You have"<<hitpoints<<"hitpoints left."<<endl;
}
return 0;
}






there, as i said, it is not complete, but other than some unidentifyed variables in the attack function, it is fairly workable

Share this post


Link to post
Share on other sites
"void attack();"

That doesn't look right to me at all. What's the void supposed to be doing? You only right the return type when you declare or define the function, not call it.

Edit: OMG I am soo slow.

However, I've noticed that you just put 'int' where you had 'void'. There shouldn't be anything there.

Share this post


Link to post
Share on other sites
You are missing a closing brace } after int attack();, which should be attack(); as someone said.
Missing bracers will confuse the compiler big time, and generate lots of "fake" errors.

Im sure you are aware of the fact that the loop while(1 < 2) will never end, so you might as well use while(1) and get the same effect :D

Share this post


Link to post
Share on other sites
I don't think the only errors left are the ones that are 'supposed to be there' [rolleyes].

Among other things. your use of braces is inconsistent. Here's the original code, with some inferred formatting. I've put a '!' on errors and code I take issue with. I've been a little anal, but that seems to be the fashion.

#include <iostream>
#include <string> // ! 1

using namespace std; // ! 2

int hitpoints = 100;
int evasion = 10;
int power = 10;
int defense = 10;
int accuracy = 10;
int money = 100;
int area = 2;
int xp = 1;
int level = 1;

int attack(); // ! 3

int main()
{
cout << "Greetings Gladiator. Welcome to Arena Quest." << endl;
while (1 < 2) // ! 4
{
char input;
cin >> input;
if (input == 'a')
int attack(); // ! 5
// ! 6
return 0;
}

int attack()
{
while (hitpoints > 0 && enhitpoints > 0) // ! 7
{
t = 0; // ! 7
while (t < 500) // ! 8, 14
t = +1; // ! 9

accuracy - 1/4 * enevasion = hitchance; // ! 10
power - 1/4 * endefense = hitpower; // ! 10
srand (time(0)); // ! 11
int percent = rand();
int hitmiss = percent % 100; // ! 12
if (hitmiss <= hitchance)
{
srand(time(0)); // ! 11
percent + rand(); // ! 13
int hitstr = percent % 100; // ! 12
int atten = hitpower * hitstr;
}
else // ! 14
int atten = 0;

enhitpoints = -atten; // ! 9, 15

cout << "You dealt" << atten << "damage. The enemy has" << enhitpoints << "hitpoints left." << endl;

xp = xp + 2 * atten; // ! 16


int enhitchance = enaccuracy - 1/4 * evasion // ! 17
int enhitpower = enpower - 1/4 & defense // ! 17, 18
srand(time(0)); // ! 11
percent = rand();
hitmiss = percent % 100; // ! 12

if (enhitchance >= hitmiss) // ! 19
{
srand(time(0)); // ! 11
percent = rand();
int enhitstr = percent // !! 12, 17, 20
int enatt = enhitpower * enhitstr;
}
else // ! 14
int enatt = 0;

hitpoints = -enatt; // ! 9, 15

cout << "The enemy did" << enatt << "damage. You have" << hitpoints << "hitpoints left." << endl;
}
return 0;
}


1. In C++, we #include <cstring>.
2. This is largely a matter of taste, but it's a bad idea to get into the habit of #using namespaces as it completely undermines the principle.
3. There is no need to forward-declare attack() when you can simply define it first.
4. The condition in your while loop (1 < 2) here is necessarily true. A better idea would be to use while (true) rather than that perverse inequality, but even that is bad practice. You should introduce a flag to determine whether the program is running so that the user has a means of quitting.
5. When calling a function, you mustn't qualify it with the return type, or the compiler will think you're defining a function. Simply drop the int.
6. You're missing a closing brace. As the code stands, the function will return before the loop repeats.
7. enhitpoints, t, hitchance, evenvasion, hitpower, endefense, enaccuracy, enpower, percent haven't been defined.
8. This loop seems to serve no purpose other than to burn cycles. Lose it. If you want a delay, use Sleep() or something to that effect.
9. If the loop were legitimate, this statement would cause it never to terminate. You are simply assigning the value 1 (i.e. +1) to t. I imagine you intended t += 1 (note the order of the + and =) as you've made the same mistake in other places.
10. Unlike mathematical equality, C++ assignment is not commutative. The = operator must be preceded by an l-value. Here, you need to swap the expressions either side of the =.
11. srand() is a seeding function and usually needs only be called once in the program's lifetime. As is, this is damaging, as any two calls to srand(); rand() within the same second will return the same value - not very random. Take the first srand() into main() and ditch the rest. Don't forget to include ctime if you want to use time().
12. While this is fine, it's less cumbersome to modulus the random variable directly, rather than creating a whole new variable to store the intermediate value. Currently, it's not clear if you're going to use percent at a later point (which you aren't).
13. I think you typoed. The + should be an =.
14. Again, this is okay, but you can help avoid bugs by explicitly providing braces for while, if, elses.
15. The variable atten is being declared inside an earlier set of braces (in both cases) and so at this point in the code, it effectively doesn't exist. If you need to refer to a variable at a lower scope, you'll need to declare it earlier. By simultaneously initialising it to zero, you can also eliminate the else clause.
16. It's a little bit clearer if you use the += operator, here.
17. You forgot the semicolon.
18. I think that ampersand should be an asterisk.
19. Another style idiosyncracy. To match the previous analogous statement, you should flip the equality around.
20. I think you meant to take the modulus.

On more cosmetic notes, you'll probably want some spaces in the console strings so that the numbers come out readable. Also, it seems that the the enemy is way too strong for the user: if he gets a hit in, you're dead outright [rolleyes].

Here's my version of the revised code, for completeness:

#include <iostream>
#include <cstring>
#include <ctime>

int enhitpoints = 100;
int hitpoints = 100;
int evasion = 10;
int enevasion = 10;
int power = 10;
int enpower = 10;
int defense = 10;
int endefense = 10;
int accuracy = 10;
int enaccuracy = 10;
int money = 100;
int area = 2;
int xp = 1;
int level = 1;

int attack()
{
while (hitpoints > 0 && enhitpoints > 0)
{
int hitchance = accuracy - 1/4 * enevasion;
int hitpower = power - 1/4 * endefense;
int hitmiss = rand() % 100;
int atten = 0;

if (hitmiss <= hitchance)
{
int hitstr = rand() % 100;
atten = hitpower * hitstr;
}

enhitpoints -= atten;

std::cout << "You dealt " << atten << " damage. The enemy has " << enhitpoints << " hitpoints left." << std::endl;

xp += 2 * atten;

int enhitchance = enaccuracy - 1/4 * evasion;
int enhitpower = enpower - 1/4 * defense;
hitmiss = rand() % 100;

int enatt = 0;
if (hitmiss <= enhitchance)
{
int enhitstr = rand() % 100;
enatt = enhitpower * enhitstr;
}

hitpoints -= enatt;

std::cout << "The enemy did " << enatt << " damage. You have " << hitpoints << " hitpoints left." << std::endl;
}
return 0;
}

int main()
{
bool bRunning = true;
srand(static_cast<int> (time(0)));

std::cout << "Greetings Gladiator. Welcome to Arena Quest." << std::endl;
while (bRunning)
{
char input;
std::cin >> input;
switch (input) {
case 'a':
attack();
break;
case 'q':
bRunning = false;
break;
}
}
return 0;
}


Regards
Admiral

Share this post


Link to post
Share on other sites
yes i have been working on it since i found the error and now have a perfectly workable version (which still needs to be made into a full game):
____________________________________________________________________

#include <iostream>
#include <string>

using namespace std;

int enhitpoints=100;
int enevasion=10;
int enpower=10;
int endefense=50;
int enaccuracy=50;









int hitpoints = 100;
int evasion = 10;
int power = 10;
int defense = 50;
int accuracy = 50;
int money = 100;
int area = 2;
int xp = 1;
int level = 1;

void attack();
int main()
{
cout<<"Greetings Gladiator. Welcome to Arena Quest."<<endl;
while(1<2)
{
char input;
cin >> input;
if (input == 'a')
attack();
}
return 0;
}






void attack()
{
while (hitpoints > 0 && enhitpoints > 0)
{
int ti;
int tim;
tim = 0;
while (tim<200000000)
{
tim=tim+1;
}
int hitchance;
hitchance=accuracy-1/4*enevasion;
int hitpower;
hitpower=power-1/4*endefense;
srand(time(0)+ti^3*ti);
ti=ti+1;
int percent=rand();
int hitmiss=percent%100;
if(hitmiss<=hitchance)
{
srand(time(0)+ti^2*ti);
ti=ti+1;
percent=rand();
int hitstr=percent%100;
int atten=hitpower*hitstr/100;
}
else
int atten = 0;
int atten;
enhitpoints=enhitpoints-atten;
cout<<"You dealt"<<atten<<"damage. The enemy has"<<enhitpoints<<"hitpoints left."<<endl;
xp=xp+2*atten;


int enhitchance=enaccuracy-1/4*evasion;
int enhitpower=enpower-1/4*defense;
srand(time(0)+ti^ti*ti);
ti=ti+1;
percent = rand();
hitmiss=percent%100;
if (enhitchance>=hitmiss)
{
srand(time(0)+ti^2*ti*ti);
ti=ti+1;
percent=rand();
int enhitstr=percent%100;

int enatt=enhitpower*enhitstr/100;
}
else
int enatt=0;
int enatt;
hitpoints=hitpoints-enatt;
cout<<"The enemy did"<<enatt<<"damage. You have"<<hitpoints<<"hitpoints left."<<endl;
if (enatt > enhitpower || atten > hitpower)
{
break;
cout<<"error"<<endl;
}
}
return;
}






well off to make the rest of the game
by the way, the odd stuff i added in the srand parts are because the srand generates the random number depending on the time so i changed so now it also depends on another part to make each one different

Share this post


Link to post
Share on other sites
I think you're rather missing the point of my critique. It wasn't to make the code work, but to make it good.

While your new code may 'work', it is still a car-crash [rolleyes]. Twelve of my original twenty points are still pertinent and I highly recommend you go to the trouble of reading and taking it all in, lest we find you making the same mistakes again and asking for more help in a week's time.

It is precisely because you are misusing the language that you had to go to the effort of writing in those awful srand() hacks to get the random numbers to work.

And please get into the habit of using [source] tags.

Regards
Admiral

Share this post


Link to post
Share on other sites
@The OP:

A couple of forum etiquette tips. Nobody expects you to know about the [source ] tags the first time you post in the forum, but as of now you've been told at least three times to use them. After being told twice, you were still dumping huge chunks of unformatted code into your posts. You can still run into problems when using these tags (depending on how your code is formatted to begin with), but please at least give it a try. If you're not sure what we mean by 'tags', ask.

Also, don't delete or edit your original post once it's been answered (or you think it's been answered, which is yet another topic). Why you should not do this has been covered extensively elsewhere, so I won't bother repeating it here.

Share this post


Link to post
Share on other sites

This topic is 4040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this