-3

trying to make my first post right so here goes.

I ran into this question and have not been able to figure it out. I keep receiving the error:

error C4700: uninitialized local variable 'miles' used

I have scavenged over all of StackOverflow and keep running into the same answer: I have to initialize my local variable, but when I do that I am creating a set value. I want to set my local variable 'miles' to an unknown value because I want the user to be able to set the value when the program runs.

Everything ran great until I tried to cast the end value 'miles'so that it would truncate.

Please correct me if I'm using incorrect terminology. Fresh-out-of-the-womb-to-programming. And thank you to everyone in advance.

Question: Write a program that prompts the capacity, in gallons, of an automobile fuel tank and the miles per gallon the automobile can be driven. The program outputs the number of miles the automobile can be driven without refueling. Numbers entered for capacity must allow entry of capacity being an integer and the miles per gallon in decimals. The number of miles must be output to the next lowest integer (without decimals).

#include "stdafx.h"

//include statement
#include<iostream>

//include namespace statement
using namespace std;

//main function
int main()
{
    //variable declaration
    double capacity_Gallons;
    double miles_Gallon;
    double miles = static_cast<int>(miles < 0 ? miles - 0.5 : miles + 0.5);

    //inputting capacity of automobile
    cout << "Enter the capacity of the automobile fuel in gallons: ";
    cin >> capacity_Gallons;
    cout << endl;

    //inputting the miles per Gallons
    cout << "Enter the miles per gallons the automobile can be driven: ";
    cin >> miles_Gallon;
    cout << endl;

    //calculating miles
    miles = capacity_Gallons * miles_Gallon;

    //display output data
    cout << "Number of miles driven wihtout refueling: " << miles << endl;

    //pause system for some time for user continuation
    system("pause");



}   //end main
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
5MikesOut
  • 1
  • 1
  • 3
  • 2
    `double miles = static_cast(miles < 0 ? miles - 0.5 : miles + 0.5);` What do you think this is doing? – Kevin Jun 08 '18 at 02:25
  • double miles = static_cast(miles < 0 ? miles - 0.5 : miles + 0.5); at this point in code, you are attempting to initialize the variable "miles" using itself -- which is not initialized. – Ilan Keshet Jun 08 '18 at 02:26
  • What should be the initial value of `miles`? Is it input by user? – zhm Jun 08 '18 at 02:27
  • It does not matter what you initialize `miles` to, or really whether you initialize it at all. The first time it is used, you assign a new value to it. Basic good practice would still be to initialize it, and `0.0` is a pretty standard initial value to use... Or you could declare it later on (_e.g._ when you actually calculate the value) – paddy Jun 08 '18 at 02:34
  • 2
    Please avoid editing your question to contain solutions discussed in comments or answers. It puts everything out of context for future visitors. To answer your remaining question, use [std::trunc](http://en.cppreference.com/w/cpp/numeric/math/trunc) to truncate a positive or negative value towards zero. – paddy Jun 08 '18 at 03:15
  • In C++ it is not required to declare all variables at the start of your functions. Just declare them where you define them. – JHBonarius Jun 08 '18 at 08:29

2 Answers2

0

You should take out that line entirely, and change the later line to double miles = capacity_Gallons * miles_Gallon;.

Instead of your handcrafted rounding code it would be better to use the standard rounding function in the display statement, ... << std::lround(miles) << ... although your assignment stipulation says you should round down , not round to nearest as you are currently doing. (So you can just cast to int there).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • This rounds to the nearest whole integer and not down. – 5MikesOut Jun 08 '18 at 03:01
  • 1
    @5MikesOut The original code rounded to nearest integer, I am offering a cleaner way to do that as well as pointing out that your assignment says to round down. – M.M Jun 08 '18 at 03:16
0

You don't need to declare miles there, you can declare it at the point it has a value.

#include<iostream>

int main()
{  
    //inputting capacity of automobile
    double capacity_Gallons;
    std::cout << "Enter the capacity of the automobile fuel in gallons: ";
    std::cin >> capacity_Gallons;
    std::cout << endl;

    //inputting the miles per Gallons
    double miles_Gallon;
    std::cout << "Enter the miles per gallons the automobile can be driven: ";
    std::cin >> miles_Gallon;
    std::cout << endl;

    //calculating miles
    double miles = capacity_Gallons * miles_Gallon;

    //display output data
    std::cout << "Number of miles driven wihtout refueling: " << miles << std::endl;

    //pause system for some time for user continuation
    system("pause");
}

As an aside, using namespace std is a bad habit.

Caleth
  • 52,200
  • 2
  • 44
  • 75