Uninitialized Local Variable - c++

I'm new and just learning C++ and came across this problem that I've spent maybe an hour trying to fix and researching answers on but I cant seem to figure out what I'm doing wrong. I'm using Visual Studios as my IDE, the most recent version.
#include "stdafx.h"
#include <iostream>
#include "constant.h"
//height of the tower
double towerHeight(double x)
{
using namespace std;
cout << "Enter a height for the tower" << '\n';
cin >> x;
return x;
}
//the number of seconds since the ball has been dropped to determine the distance
double secondsSinceDrop(double x)
{
using namespace std;
cout << "How long has it been since you dropped the ball (Seconds): ";
cin >> x;
return x;
}
//finds how far off the ground the ball is
double currentBallHeight(double x, double y)
{
return y * constant::gravity - x;
}
//prints how far off the ground the ball is
void printResult(double x, double y)
{
using namespace std;
if (currentBallHeight(x, y) < 0)
cout << "At " << y << " the ball is on the ground." << '\n';
else
cout << "At " << y << " the ball is at: " << currentBallHeight(x, y) << '\n';
}
int main()
{
double x = towerHeight(x);
double y = secondsSinceDrop(x);
printResult(x,y);
return 0;
}
This is the Error Code
- chapter 2 comprehensive quiz (part 2).cpp(46): error C4700: uninitialized local variable 'x' used
-Line (46) is - double x = towerHeight(x);
I've been getting this and I've changed my code around to get it down to just this one error but i cant figure out how to fix it. Its probably something simple and I'm dumb for overlooking it but any help would be greatly appreciated.

These lines will be throwing errors
because the variable 'x' you are sending as an argument does not exist in the scope of main
int main()
{
-> double x = towerHeight(x);
-> double y = secondsSinceDrop(x);
printResult(x,y);
return 0;
}
Instead you could try something like this.
#include "stdafx.h"
#include <iostream>
#include "constant.h"
using namespace std;
//height of the tower
double towerHeight()
{
double height;
cout << "Enter a height for the tower" << '\n';
cin >> height
return height;
}
//the number of seconds since the ball has been dropped to determine the distance
double secondsSinceDrop()
{
double seconds;
cout << "How long has it been since you dropped the ball (Seconds): ";
cin >> seconds;
return seconds;
}
//finds how far off the ground the ball is
double currentBallHeight(double x, double y)
{
return y * constant::gravity - x;
}
//prints how far off the ground the ball is
void printResult(double x, double y)
{
if (currentBallHeight(x, y) < 0)
cout << "At " << y << " the ball is on the ground." << '\n';
else
cout << "At " << y << " the ball is at: " << currentBallHeight(x, y) << '\n';
}
int main()
{
double height = towerHeight();
double seconds = secondsSinceDrop();
printResult(height, seconds);
return 0;
}
Some tips that I would recommend
Declare your variables as much as relevant to you instead of using 'x/y/z'
There is no need to add the using namespace std; inside each function

Your first line of code in main() is double x = towerHeight(x);, what value of x are you sending to the function, when you have not initialized it.
When you are using a variable without initializing the value of it is undefined.
You can pass the variable as a reference to your function and accept the values inside it.
//height of the tower
void towerHeight(double &x)
{
using namespace std;
cout << "Enter a height for the tower" << '\n';
cin >> x;
}
//the number of seconds since the ball has been dropped to determine the distance
void secondsSinceDrop(double &y)
{
using namespace std;
cout << "How long has it been since you dropped the ball (Seconds): ";
cin >> y;
}
int main()
{
double x = 0.0, y = 0.0;
towerHeight(x);
secondsSinceDrop(y);
printResult(x, y);
return 0;
}

You seem to be struggling to connect the mental dots on what the computer is doing when you
declare variables with an initial value
define function parameters
return a value from a function
Not sure how this question will fair with the SO community as the preference is for Q/A that is succinct and reusable (maybe some editing can help) but for your benefit let me explain some of these concepts.
Let's start with a variable declaration
int x = 5;
int y = x;
When you define int x; it creates a space in RAM for an integer (4 bytes). Adding the = 5 initializes it immediately. It's important that the value on the right side of = (5 in this case) is known before the computer tries to make space for x.
It's fine to use values that aren't constant for variables like this (notice the second line in the example) but x has to be known before you declare y. In other words, this would obviously be a problem:
int y = x;
int x = 5;
For this same reason, the line: double x = towerHeight(x); is problematic because you're using x when you call towerHeight before ever defining x
When you define a function's parameters:
double towerHeight(double x) {
This tells the computer that you are going to copy the value from whatever called towerHeight to a new place in RAM and call it "x". This means that the value outside of the function doesn't get modified. Consider the following example:
double towerHeight(double x) {
x = 5;
std::cout << x << std::endl; // outputs 5
}
int main() {
double x = 10;
towerHeight(x);
std::cout << x << std::endl; // outputs 10
return 0;
}
Even though x was changed in towerHeight that was a "different copy of x" which also happened to be called the same name.
When you return a value from a function, in the same manner as passing a function argument, the return value is copied and used in places of the function call. Let's modify the previous example slightly:
double towerHeight(double x) {
x = 5;
return x;
}
int main() {
double x = 10;
x = towerHeight(x); // returns the value "5"
std::cout << x << std::endl; // Outputs "5"
return 0;
}
You can think of towerHeight(x) being replaced by "5" so the code would read x = 5;
Conclusion
You should try and use different variable names for
function arguments (the variables/values you pass to the function)
function parameters (what they are called inside the function)
to avoid this kind of confusion. Though there may be times where using the same name makes sense (i.e. passing by reference, which is another question). It's important for you to be aware of what's really going on.
Here is what you probably intend to do:
double towerHeight()
{
double height;
std::cout << "Enter a height for the tower" << std::endl;
std::cin >> height;
return height;
}
double secondsSinceDrop()
{
double seconds;
std::cout << "How long has it been since you dropped the ball (Seconds): ";
std::cin >> seconds;
return seconds;
}
double currentBallHeight(double y0, double t)
{
return y0 - (constant::gravity * t * t / 2);
}
void printResult(double y0, double t)
{
double currentHeight = currentBallHeight(y0, t);
if (currentHeight < 0)
std::cout << "At " << t << "s the ball is on the ground." << std::endl;
else
std::cout << "At " << t << "s the ball is at: " << currentHeight << std::endl;
}
int main()
{
double y0 = towerHeight();
double t = secondsSinceDrop();
printResult(y0, t);
return 0;
}
Summarizing what I've changed:
Renamed x to y0 since y(0)/h(0) is typically used for "initial height" in physics classes, and similarly y with t (though time would be an even better name).
Don't pass anything to towerHeight or secondsSinceDrop; you're not trying to give those functions something, you're trying to get something out of them.
Move the definition of x from a function parameter to a local variable defined in the function for towerHeight and secondsSinceDrop
Removed the duplicated call to currentBallHeight (no need to do the same math twice, it takes time to crunch numbers after all, however small in this case)
Rewrote for proper usage of std::cout and std::endl
Rewrote the currentBallHeight equation to match constant free-fall kinematics (y(t) = y(0) - 0.5g * t^2) as an added bonus (assuming constant::gravity > 0)
At some point it will be valuable for you to become aware of the more technical terminology and definitions for the concepts I've outlined here. Here are some recommended readings (just to get you started; keep learning, always):
Sequence Points
Parameters and Arguments
Passing by Reference vs by Value
Passing pointers vs by Reference
Making sure you understand what using namespace std; does and why you should never use it

Rewrite your function as following:
//height of the tower
double towerHeight()
{
double x;
using namespace std;
cout << "Enter a height for the tower" << '\n';
cin >> x;
return x;
}
and in int main(){} rewrite following line:
double x = towerHeight();
I guess this will do but you can actually modify your double secondsSinceDrop(double x); function this way as it doesn't really need a double value as parameter.

Related

invalid type 'float*[float]' for array subscript

I want to display the range of x and f(x) and keep f(x) in array but i always get this error:
invalid type 'float*[float]' for array subscript
can someone help me? I'm still stuck.
Here's the code:
#include <iostream>
#include <cmath>
#include <math.h>
using std::cin;
using std::cout;
using namespace std;
void displayValue(float funx[], float j, float x);
int main()
{
float num9[]={};
float a, r;
displayValue(num9, a, r);
return 0;
}
void displayValue(float funx[], float j, float x)
{
float i;
cout << "Please enter range of x: " << endl;
for (i=0; i<1; i++)
{
cin >> x >> j;
}
for (float i=1; i<=160.5; i++)
{
x+=0.5;
funx[i]=1/sin(x)+1/tan(x);
//1.2 Display f(x) and x within the range
}cout << x << " = " << funx[i] << "\n";
}
The problems you're trying to solve aren't actually problems you need to solve. There's a lot going wrong in this code that can simply be removed because you're using the wrong tools.
You don't need an array here. If you did you'd need to allocate one, not pass in something that's empty, or you'd be using it out of bounds. In C++ for arrays like this use std::vector.
That being said, here's a simplified version of the code:
#include <iostream>
#include <cmath>
#include <math.h>
// Don't add "using namespace std", that separation exists for a reason.
// Separate the math function to make it clear what's being done
float f(const float x) {
return 1/sin(x)+1/tan(x);
}
// Define your functions before they're used to avoid having to declare
// then later define them.
void displayValue(const float min, const float max, const float step = 0.5)
{
for (float x = min; x <= max; x += step)
{
// Note how the f(x) function here is a lot easier to follow
std::cout << "f(" << x << ") = " << f(x) << std::endl;
}
}
int main()
{
std::cout << "Please enter range of x: " << std::endl;
// Capture the range values once and once only
float min, max;
std::cin >> min >> max;
// Display over the range of values
displayValue(min, max);
return 0;
}
There's some important C++ fundamentals here:
float num9[]={}; is not an empty array that you can later add to, it is a permanently zero-length array, or in other words, it's useless.
Pay close attention to the variables you've defined and avoid defining them twice in the same scope.
Turn on all your compiler warnings while you're learning to be alerted to potential problems. C++ is full of nuances and gotchas.

Why isn't my mutator function setting anything? Or my constructor?

I'm trying to make a fairly basic program but I'm getting some very inconsistent outputs. In particular the setter doesn't seem to be setting values, although when I mess with the argument variables in ways which shouldn't alter the output I'm sometimes getting working results.
Here is my code:
point.cpp
public:
point()
{
x = 0;
y = 0;
}
point(double x, double y)
{
x = x;
y = y;
}
void set_x(double x)
{
x = x;
}
void set_y(double y)
{
y = y;
}
double get_x() const
{
return x;
}
double get_y() const
{
return y;
}
private:
double x;
double y;
};
Main
point pointA;
double x,y;
cout << "Enter x value for point A: " << endl;
cin >> x;
pointA.set_x(x);
cout << "Enter y value for point A: " << endl;
cin >> y;
pointA.set_y(y);
point pointB(x,y);
cout << "X value for point A is: " << pointA.get_x() << endl;
cout << "Y value for point A is: " << pointA.get_y() << endl;
cout << "X value for point B is: " << pointB.get_x() << endl;
cout << "Y value for point B is: " << pointB.get_y() << endl;
Output:
X value for point A is: 10
Y value for point A is: 10
X value for point B is: 3.18463e-314
Y value for point B is: 2.12199e-314
I'm really confused about all this since essentially the same functions are working in other similarly basic programs. If anyone could point out what obvious mistake I'm making it would be greatly appreciated.
Let's examine one constructor, though the problem is the same everywhere
point(double x, double y)
{
x = x;
y = y;
}
x refers to to the parameter. So you are assigning the parameter to itself. Two and a half solutions are possible:
Use different names for the members and parameters.
Explicitly name the member with this, i.e. this->x = x;.
For the c'tor only. Use a member initializer list, point(double x, double y) : x(x), y(y) {}. Here there are special rules about what x is being referred to inside and outside the initializer. I recommend you use a member initializer list even if you adopt one of the previous solutions. It's more idiomatic C++.
The problem - as stated already by others - is that your member variable has the same name as the parameter.
Take a look at this method by itself. It is complete and does assign x to itself.
void set_x(double x)
{
x = x;
}
In your code this double x hides the external (from the functions point of view) variable x.
I would recommend to prefix you member variables with m_ or m (m for member).
This would make for unique names and help you distinguish and prevent such problems.
void set_x(double x)
{
m_x = x;
}
Now you can see that the parameter x is assigned to the member variable m_x.
Alternatively you can also refer to the member with the same name by using the this-Pointer like this:
void set_x(double x)
{
this->x = x;
}

error: called object type 'double' is not a function or function pointer

I am relatively new to c++ programming, I have an assignment to code the Newton Raphson method however I have the error error:
called object type 'double' is not a function or function pointer
This error appears when I am trying to compile my code. I tried some basic changes to assign pointer but I probably did it in the wrong way, my code is printed below, can anybody explain how can I overcome this?
#include <iostream>
#include <math.h>
using namespace std;
double f(double x); //this is f(x)
double f(double x) {
double eq1 = exp(x) + pow(x,3) + 5;
return eq1;
}
double f1(double x); //this is the first derivative f'(x)
double f1(double x) {
double eq2 = exp(x) + 3*pow(x,2);
return eq2;
}
int main() {
double x, xn, f, f1, eps;
cout << "Select first root :" << '\n'; //Here we select our first guess
cin >> xn;
cout << "Select Epsilon accuracy :" << '\n';
cin >> epsi;
f = f(x);
f1 = f1(x);
cout << "x_n" << " " << "x_(n+1)" << " " << "|x_(n+1) - x_1|" << '\n';
do {
x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
f = f(x);
f1 = f1(x);
xn = x - (f/f1); //this the formula that sets the itenaration going
cout << x << " " << xn << " " << fabs(xn - x) << '\n';
}
while( fabs(xn - x) < epsi ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues
cout << "The root of the equation is " << xn << '\n';
return 0;
}
Thank you
You have local variables with the same name as the functions, thus
f = f(x);
f1 = f1(x);
cannot work.
Rename either the functions or the variables. Anyhow single letter variable/function names are not nice. Use descriptive names. You (or anybody else) taking a look at the code after some weeks will be thankful for that.
PS: you also dont need the forward declarations. And the functions can be written a bit shorter:
//double f(double x); // this you dont need
double f(double x) {
return exp(x) + pow(x,3) + 5;
}
Also using namespace std; is considered bad practice. In this case it does little to no harm, but you better get rid of this bad habit before it does matter.
Last but not least you should format your code properly. This
while( fabs(xn - x) < epsi );
looks very nasty, because it seems to be an infinite loop. I almost never use the do-while loop, however, I suggest you to write it like this:
do {
// ...
} while ();
because usually whenever you see a while with a ; in the same line you should start to panic ;) (while loops are much more common than do-while and errors caused by a ; after the condition in a while loop can be a pain in the a** to debug)
You are attempting to use functions called f and f1 and doubles called f and f1. If you call the variables or the function something else, then you can resolve the error. It would be good coding practice to give these variables better names that tell the reader what they do and avoid mistakes like this one.
There were several errors in your code. I made it compilable:
#include <iostream>
#include <math.h>
using namespace std;
double func(double x); //this is f(x)
double func(double x) {
double eq1 = exp(x) + pow(x,3) + 5;
return eq1;
}
double func1(double x); //this is the first derivative f'(x)
double func1(double x) {
double eq2 = exp(x) + 3*pow(x,2);
return eq2;
}
int main() {
double x, xn, f, f1, eps;
cout << "Select first root :" << '\n'; //Here we select our first guess
cin >> xn;
cout << "Select Epsilon accuracy :" << '\n';
cin >> eps;
f = func(x);
f1 = func1(x);
cout << "x_n" << " " << "x_(n+1)" << " " << "|x_(n+1) - x_1|" << '\n';
do {
x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
f = func(x);
f1 = func1(x);
xn = x - (f/f1); //this the formula that sets the itenaration going
cout << x << " " << xn << " " << fabs(xn - x) << '\n';
}
while( fabs(xn - x) < eps ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues
cout << "The root of the equation is " << xn << '\n';
return 0;
}
The main problems were:
you defined the variable f with the same name of the f(x) function (the same error was repeated for the f'(x) function) and
you declared the eps variable to represent epsilon in your program but you tried to access to it several times by calling it epsi.

Setting Default Parameters in C++

I have a little question about how default values are given to function parameters in C++. The problem I faced is probably due to my lack of understanding as to where the parameters are supposed to be declared/defined in the function prototype or the function header, or both? Codes are below with the errors noted:
#include <iostream>
using namespace std;
float volume(float l, float w, float h);
int main() {
float length;
float width;
float height;
cout << volume() << endl; // Here, with 'volume()' underlined, it says:
//no matching function for call to 'volume()'
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l = 1, float w = 1, float h = 1){
float vol = l * w * h;
return vol;
}
In another attempt, here's what happened:
#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
float length;
float width;
float height;
cout << volume() << endl;
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l = 1, float w = 1, float h = 1){ //Here, Xcode says that
// that the error is: Redefinition of default argument. < which I believe I understand.
float vol = l * w * h;
return vol;
}
In my last attempt, which is the one that worked, I did this:
#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
float length;
float width;
float height;
cout << volume() << endl;
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l, float w, float h){
float vol = l * w * h;
return vol;
}
Could someone please explain to me the logic behind why the latter worked while the first two did not? Is there another way that the code would still work in the same way with the parameters specified elsewhere or the default values set in some place else? Are there any conventions or more favored practices in this area?
Adam
C++ and C are parsed top-down. When the compiler interprets a statement, it doesn't know about things it hasn't read yet.
In your first example, you declare a function called "volume", prototyped as taking 3 floats and returning a float. You then try to call a function called "volume" that takes no parameters, which doesn't exist yet (it would be a different function, since C++ supports polymorphism). You later define a function that can take 0, 1, 2, or 3 floats, but it is both too late and has an incompatible prototype to the first.
Your second example intuitively makes sense to be wrong, kind of like defining variables twice, but I don't have any specific information about why it is invalid code when the default values are identical.
Default parameters must be specified in the function prototype, which must occur prior to first usage in order for the compiler to know about it. Typically, you would put the prototypes with their default values in a header file that gets included above the code.
One thing to watch out for when dealing with default parameters from a shared header file, especially if you use it with dynamic libraries: The default values for the parameters are stored with the caller, and not the function being called. That is, if you update the function with new default values and don't rebuild the code calling that function, the old defaults will still be used by the calling code.
Adding to the above answer from Bjarne Stroustrup
A default argument is type checked at the time of the function declaration and evaluated at the time of the call. Default arguments may be provided for trailing arguments only. For example:
int f (int, int =0 , char * =0 ); // ok
int g (int =0 , int =0 , char *); // error
int h (int =0 , int , char * =0 ); // error
A default argument can be repeated in a subsequent declaration in the same scope but not changed.
For example:
void f (int x = 7 );
void f (int = 7 ); // ok
void f (int = 8 ); // error: different default arguments
void g () { void f (int x = 9 ); // ok: this declaration hides the outer one }
Declaring a name in a nested scope so that the name hides a declaration of the same name in an outer scope is error prone.
Default values could be defined in function declaration, as you done in your third attempt. This means usually they're appeared in header files, although this is not a rule.
Note that function declarations are scoped. This means that you could have more than one declaration for a function as long as they have different scopes:
void f(int);
int main() {
f(3); // argument should specified.
void f(int = 1);
f(); // calls f(1)
}
void f(int n = 2) {
}
void g() {
f(); // calls f(2)
}
In your second attempt you put default values on both declaration and definition of the function. This cause the compiler to confused because they are in the same scope.

Reading from file and storing attributes gives wrong output

I'm trying to read a text file that consists of the following three attributes;
RouterID, X-coordinate, Y-coordinate.
A brief snippet of the txt file is shown below;
100 0 0
1 20.56 310.47
2 46.34 219.22
3 240.40 59.52
4 372.76 88.95
Now, what I'm trying to achieve is to make a node for every RouterID and store its corresponding x and y co-ordinates. For this purpose, I have created the following class;
class Node {
public:
float routerID;
float x;
float y;
void set_rid (float routerID) {
routerID = routerID;
}
void set_x_y (float x, float y) {
x = x;
y = y;
}
};
And I have the following which performs the job of creating a new node for every routerID;
const std::string fileName = "sampleInput.txt";
std::list<Node> nodeList;
int main (void) {
std::ifstream infile(fileName);
float a(0);
float b(0), c(0);
//This reads the file and makes new nodes associated with every input
while (infile >> a >> b >> c) {
Node newNode;
newNode.set_rid (a);
newNode.set_x_y (b, c);
std::cout << "newNode " << "rid = " << newNode.routerID << " x = " << newNode.x << " y = " << newNode.y << std::endl;
nodeList.push_back(newNode);
}
I'm performing the following line inside my while loop just to check whether or not the values being assigned are correct or not.
std::cout << "newNode " << "rid = " << newNode.routerID << " x = " << newNode.x << " y = " << newNode.y << std::endl;
When I compile and run the code, I get the following as my output for all of them;
newNode rid = -1.07374e+008 x = -1.07374e+008 y = -1.07374e+008
I've just started learning C++ last week and this is my first "big" program that I am trying to code. Could anyone please point me in the right direction?
void set_rid (float routerID) {
routerID = routerID;
}
This doesn't do what you seem to think it does. It assigns the parameter to itself; the value of this->routerID remains unchanged. Same with set_x_y. Just give the method parameters some names that are different from those of data members.
Another to distinguish class variables from input parameter is by using the keyword this. thus you can make a reference to class variables by calling this.routerID, this.x and this.y