Text Based Game Help - Total Beginner here

Yes, this is help with a school assignment. It's an intro to C++ class so I'm a total beginner here.

We have to create a text-based game with certain functions. I've been checking my progress as I go and for some reason, when I keep getting this error that fProjectile has not been initialized.



#include <iostream>
using namespace std;

int main()
{
int iPlayerAngle; // ask the player for an initial angle
float fPlayerSpeed; //ask the player for an initial speed
float fDistanceFormula; // X value for calculating the distance traveled

cout << "Choose an initial angle of 15, 30, 45, 60, or 75: ";

do
{
cin >> iPlayerAngle;

if( iPlayerAngle > 0, iPlayerAngle < 90, (iPlayerAngle % 15) == 0 )
cout << "That's a valid angle!\n";
else
cout << "Try again!\n";

} while( iPlayerAngle < 0, iPlayerAngle > 90, (iPlayerAngle % 15) != 0 );

cout << "Now choose an initial speed from 1 - 10: ";

do
{
cin >> fPlayerSpeed;

if( fPlayerSpeed > 1.0, fPlayerSpeed < 10.0 )
cout << "Great!\n";
else
cout << "Try again.\n";

} while( fPlayerSpeed < 1.0, fPlayerSpeed > 10.0 );

switch( iPlayerAngle )
{
case '15':
fDistanceFormula == 0.5;
break;
case '30':
fDistanceFormula == 0.866;
break;
case '45':
fDistanceFormula == 1.0;
break;
case '60':
fDistanceFormula == 0.866;
break;
case '75':
fDistanceFormula == 0.5;
break;
}

float fProjectile = fPlayerSpeed * fPlayerSpeed * fDistanceFormula / 10;

cout << "Your projectile flew a total distance of: " << fProjectile << "\n";

std::cin.get();
std::cin.get();
return 0;
}
Is that the only error you get?

Try

float fProjectile = 0;

fProjectile = fPlayerSpeed * fPlayerSpeed * fDistanceFormula / 10;

In your switch statement use one = sign, two is for bool variable, and the rest of the code looks fine, on my phone so can't compile it but will try later if you're still having problems
Well I changed it to:

float fProjectile = 0;

and now I get the runtime error that fDistanceFormula is being used without being initialized. So I also put that as:

float fDistanceFormula = 0;

The program runs, but at the end, the result is always:

"Your projectile flew a total distance of: 0"

I suppose this is because since I initialized fDistanceFormula as zero at the start, and even though it's being changed in my switch case, it's still a zero when I call on it at the end calculation.

This is what it's looking like right now:


#include <iostream>
using namespace std;

int main()
{
int iPlayerAngle; // ask the player for an initial angle
float fPlayerSpeed; //ask the player for an initial speed
float fDistanceFormula = 0; // X value for calculating the distance traveled
float fProjectile = 0;

cout << "Choose an initial angle of 15, 30, 45, 60, or 75: ";

do
{
cin >> iPlayerAngle;

if( iPlayerAngle > 0, iPlayerAngle < 90, (iPlayerAngle % 15) == 0 )
cout << "That's a valid angle!\n";
else
cout << "Try again!\n";

} while( iPlayerAngle < 0, iPlayerAngle > 90, (iPlayerAngle % 15) != 0 );

cout << "Now choose an initial speed from 1 - 10: ";

do
{
cin >> fPlayerSpeed;

if( fPlayerSpeed > 1.0, fPlayerSpeed < 10.0 )
cout << "Great!\n";
else
cout << "Try again.\n";

} while( fPlayerSpeed < 1.0, fPlayerSpeed > 10.0 );

switch( iPlayerAngle )
{
case '15':
fDistanceFormula = 0.5;
break;
case '30':
fDistanceFormula = 0.866;
break;
case '45':
fDistanceFormula = 1.0;
break;
case '60':
fDistanceFormula = 0.866;
break;
case '75':
fDistanceFormula = 0.5;
break;
}

fProjectile = fPlayerSpeed * fPlayerSpeed * fDistanceFormula / 10;

cout << "Your projectile flew a total distance of: " << fProjectile << "\n";

std::cin.get();
std::cin.get();
return 0;
}


When you want to compare things in the switch statement you must use specific types of type identifiers when telling the compiler what kind of data you want it to compare.

When you want to compare a char to a char in a switch statement you use

case 'a':

but when you compare an integer to an integer you use nothing.

case 30:

Make sense?

I almost kicked myself.
Last edited on
Try using if-else statement if you want to check in between numbers
@pogrady - That helped soooo much! Thanks! That makes sense. :)

@Callum5042 - Thank you for your assistance as well!

I may be back in this thread later tonight as I work on it some more, heh. You guys were really helpful and cleared up a lot for me! Thanks! :D
Okay! So now I'm having an issue of "The variable 'fRand_number' is being used without being initialized'". I have no idea why this is happening. It was working fine, then I tried to add a bit of code that was completely unrelated to fRand_number. It didn't work right, so took it out and now I keep getting this error.

If when I initialize it I have it equal to zero, then all the numbers will come out as 0 rather than calculating anything. Any ideas?


#include <iostream>
using namespace std;

int main()
{
int iPlayerAngle; // ask the player for an initial angle
float fPlayerSpeed, fProjectile; //ask the player for an initial speed
float fDistanceFormula = 0; // X value for calculating the distance traveled
int iTries = 0; // tries the player has made
int iLucky; // varying the Target
int numbers_added_together;
float fRand_number;


cout << "Enter your lucky number: \n";
cin >> iLucky;

for (int i=1; i<iLucky; i++)
{
numbers_added_together = iLucky + i;
}

while (numbers_added_together > 10)
{
fRand_number = (numbers_added_together - 9);
break;

}

do //Game Loop Beginning
{


cout << "\n\nThe target is a distance of " << fRand_number << " away.\n";

cout << "\n\nTry to get a distance between " << ( fRand_number - 0.5 );
cout << " and " << (fRand_number + 0.5);


/******* Get Angle *********/

cout << "\n\nChoose an angle of 15, 30, 45, 60, or 75: ";

do
{
cin >> iPlayerAngle;

if( iPlayerAngle > 0, iPlayerAngle < 90, (iPlayerAngle % 15) == 0 )
cout << "That's a valid angle!\n";
else
cout << "That's not a valid angle. It must be one of the five given values.\n";


} while( iPlayerAngle < 0, iPlayerAngle > 90, (iPlayerAngle % 15) != 0 );




/******** Get Speed **********/

cout << "Now choose an initial speed from 1 - 10: ";

do
{
cin >> fPlayerSpeed;

if( fPlayerSpeed >= 1.0, fPlayerSpeed <= 10.0 )
cout << "Great!\n";
else
cout << "That is not a valid speed. It must be between 1.0 and 10.0.\n";

} while( fPlayerSpeed < 1.0, fPlayerSpeed > 10.0 );


/******** get X value for Distance Formula *********/

switch( iPlayerAngle )
{
case 15:
fDistanceFormula = 0.5;
break;
case 30:
fDistanceFormula = 0.866;
break;
case 45:
fDistanceFormula = 1.0;
break;
case 60:
fDistanceFormula = 0.866;
break;
case 75:
fDistanceFormula = 0.5;
break;
}


/******* Calculate the distance the projectile flew ***********/

fProjectile = fPlayerSpeed * fPlayerSpeed * fDistanceFormula / 10;

cout << "Your projectile flew a total distance of: " << fProjectile << "\n\n\n";
++iTries;



} while( (fProjectile < (fRand_number - 0.5)) || (fProjectile > (fRand_number + 0.5)) ); // Game Loop End


iTries == 1 ? cout << "It only took you 1 try!" : cout << "It took you " << iTries << " tries!\n\n";


std::cin.sync();
std::cin.get();
return 0;
}
Topic archived. No new replies allowed.