I am taking a programming fundamentals class for school and am having an issue on an assignment.
I am receiving an error C4700 stating "uninitialized local variable ‘steps’ used on line 19 (double steps; // user’s number of steps.). I am obtaining this value using cin… My professor is not responding, and I have spent a bit trying to resolve this error. Again, I am a beginner. Any help is appreciated. Other advice is also welcomed.
I also attached assignment instructions.
Thanks all.
Here is the source code:
//This program is designed to use the number of steps and direction the user would like to move and display the distance the user has traveled in km and miles.
//By Brooke M
//06/25/2022
#include <iostream>
#include <string>
using namespace std;
int main() {
string direction; // direction user would like to move.
double steps; // user's number of steps.
const double STEPS_PER_MILE = 2000;
double milesMoved = steps / STEPS_PER_MILE;
double kilometers = milesMoved / 0.62137;
//Get the direction user would like to move
cout << "What direction would you like to move? \n";
cin >> direction;
//Get the number steps user is taking.
cout << "How many steps are you taking? \n";
cin >> steps;
//Display result.
cout << "You walked " << milesMoved << " miles and " << kilometers << " kilometers " << direction << ".\n";
system("pause");
return 0;
}
>Solution :
You have the line
double milesMoved = steps / STEPS_PER_MILE;
Before the line
cin >> steps;
Programs execute steps in the order that you write them. You are trying to calculate the milesMoved before you get the number of steps from the user. You need to move the calculations down to after you actually know all of the values that you use.