I am new to c++ and I am trying to increase cars starting with value 50, but only increase by one if the youdamage is greaters than cardamage. I want cars to hold its value for the next time it does through the loop. I hope this makes sense.
int Power (int &car);
int main(){
int car = 50;
// ...
// ...
// ...
int carDamage = 0;
int yourDamage = 0;
// pick a random number between 1 to 50
yourDamage = 0 + rand() % (50 - 0 + 1);
carDamage = 0 + rand() % (50 - 0 + 1);
cout << "You hit the car and cause damage of: " << carDamage << endl;
cout << "The car hits you and causes damage of: " << yourDamage << endl;
cout << endl;
if(carDamage < yourDamage)
{
cout << "You win" << endl;
cout << "You gain strength." << endl;
cout << endl;
int car = car + 1;
}
else
{
You're declaring a new variable shadowing the original one.
change
int car = car + 1;
to
car = car + 1;
You need to reassign to the same variable. You are declaring a new variable.
Change this:
int car = car + 1;
To this:
++car;
By doing this:
int car = car + 1;
You are re-defining car as an integer.
see:
#include <stdio.h>
int car;
int main() {
car = 0;
for (int i = 0; i < 10; i++) {
int car = 0;
car++;
}
printf("%3d", car);
}
vs
#include <stdio.h>
int car;
int main() {
car = 0;
for (int i = 0; i < 10; i++) {
car++;
}
printf("%3d", car);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With