Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Initializing a variable that is obtained using cin

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//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;

}

enter image description here

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading