Jump to content

Uninitialized local variable?

FakezZ
Go to solution Solved by Ciccioo,

But if I use strings how will i be able to compare them?

with a simple ==

that's the power of strings, and of the standard namespace: you will be able to use all the "built-in functions and behaviours"

the namespace knows how to compare strings, so

if(string1 == string2)

works just the way you would expect it to

Okay so I have been experimenting a bit in c++ and have ended up with this code:

#include <iostream>#include <cmath>#include <conio.h>#include <string> int main(){long double dx, uzero, t, a;char* e = "uzero";char* c = "dx";char* d = "t";char* v = "a";char* b ;std::cout << "Which is the unknown variable?" << std::endl;std::cin >> b; if (b=e){std::cout << "Now input the stuff you know in the following order: dx, t, a" << std::endl;std::cin >> dx >> t >> a;uzero = dx / t - 1 / 2 * a*t;std::cout << uzero;} if (b=c);{std::cout << "Now input the stuff you know in the following order: u0, t, a" << std::endl;std::cin >> uzero >> t >> a;dx = uzero*t + 1 / 2 * a*pow(t, 2);std::cout << dx;} if (b=v){std::cout << "Now input the stuff you know in the following order: dx (then press enter),t,u0 " << std::endl;std::cin >> dx >> t >> uzero;a = 2 * dx / pow(t, 2) - 2 * uzero / t;std::cout << a;} if (b=d){std::cout << "I am not that smart, yet!";} _getch();return 0;  }

Everything looks fine, but I get the error  C4700: uninitialized local variable 'b' used at line 15...

Edited by FakezZ
added code tags

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

You haven't initialized b.You just named it,but you haven't assigned it a value.It doesn't matter which because you overwrite it later.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Please use the code tag.

oops sorry ill edit it :)

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

You haven't initialized b.You just named it,but you haven't assigned it a value.It doesn't matter which because you overwrite it later.

Yeah but when I do that I get an error asking me to either break or continue :/

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah but when I do that I get an error asking me to either break or continue :/

does char* b ="b" not work? 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

does char* b ="b" not work? 

will try that now

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Try b = ""

nope the same thing :(

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

does char* b ="b" not work? 

didn't work ...

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

You haven't initialized b.You just named it,but you haven't assigned it a value.It doesn't matter which because you overwrite it later.

actually no, b should be a pointer to the memory where the input should be saved

but b is not defined, so the program writes to a random memory location, which is a bug

 

your problem is that you're using a weird mix of c and c++

i actually know about nothing of c++, but you should use the standard namespace and strings instead of char*

especially if you don't know what the asterisk is there for, just don't do things to make the compiler happy and not prompt errors

Link to comment
Share on other sites

Link to post
Share on other sites

didn't work ...

What did it say?And why are you using chars?Why not strings?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

actually no, b should be a pointer to the memory where the input should be saved

but b is not defined, so the program writes to a random memory location, which is a bug

 

your problem is that you're using a weird mix of c and c++

i actually know about nothing of c++, but you should use the standard namespace and strings instead of char*

especially if you don't know what the asterisk is there for, just don't do things to make the compiler happy and not prompt errors

But if I use strings how will i be able to compare them?

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

What did it say?And why are you using chars?Why not strings?

It prompts me to break... I am not using strings because I don't know how to compare them :(

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

But if I use strings how will i be able to compare them?

with a simple ==

that's the power of strings, and of the standard namespace: you will be able to use all the "built-in functions and behaviours"

the namespace knows how to compare strings, so

if(string1 == string2)

works just the way you would expect it to

Link to comment
Share on other sites

Link to post
Share on other sites

with a simple ==

that's the power of strings, and of the standard namespace: you will be able to use all the "built-in functions and behaviours"

the namespace knows how to compare strings, so

if(string1 == string2)
works just the way you would expect it to
Oh ok thank you. When I googled it it said something like str1.compare(str2) == 0 and it didn't work xD

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Oh ok thank you. When I googled it it said something like str1.compare(str2) == 0 and it didn't work xD

actually that should work too

 

i guess it's because you're not using the namespace

after the includes, you need to add a

using namespace std;

i'm not sure if the semicolumn is needed

Link to comment
Share on other sites

Link to post
Share on other sites

I agree with Ciccioo....with that said, if your heart is content on using char* instead of string keep reading (which I can understand, I still prefer that method but just because I built up a collection of stuff that needs char*).

 

So to address a few things that others have said.

b is uninitialized...so it point to a random part of memory.  std::cin will try writing to where b is pointing to (or from my recollection)...so if b is pointing to a random place it is trouble.

The next problem is why @Nineshadow and @JamieF 's solutions didn't work.  The reason is simple "b" and "" are both a dedicated part of memory....you should not be manipulating it at all.  "b" and "" are effectively constant pointers....their memory location is fixed.  This can't be said enough NEVER try to manipulate characters that have been added using quotes.

 

The better solution to this type of problem is creating the actually memory space (as follows)

char b[1024]; //This creates room for 1023 characters, not 1024...the array has 1024 slots...this is because the last input is always 0 to tell end of lineor another syntaxchar* b = char[1024]; //Wrong syntax sorry.  Thanks Ciccioo for correcting me

This creates a problem though, only 1024 characters can be written...although this is hardly a problem (if you are expecting the user will never do this)...just something to keep a note of...as there are ways to solve this, but I really don't feel like writing them out.

 

 

Now we approach a new problem.  You will be getting errors in your code due to if(b=e) and all your other b=___.  There are a few flaws in this, first b=e will simply makes b = e...and since e points to uzero in memory it will always return true.  As a note even b == e is incorrect.  b == e will compare where the memory is pointing.  e.g. e points to uzero at location 0x222222 but b points to uzero at location 0x12121212...while b == e returns false it still is equal.  strcmp was built for this (fyi strcmp is actually a dangerous function to use, but in this case it would be okay).

 

 

So...after writing all of this, go with Ciccioo's solution of using string....I don't think you are ready to handle the concepts of pointers at the moment....but you are welcome to try, and I can always help if you get more errors :P

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

@WanderingFool

I don't know much c++,just the very basics.

My experience comes from Java.Being an OOT language,they are based around almost the same principles.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

char b[] = char[1024]; //This creates room for 1023 characters, not 1024...the array has 1024 slots...this is because the last input is always 0 to tell end of lineor another syntaxchar* b = char[1024];

i agree with your agreement with my statement, but i disagree with yours

 

isn't it

char b[1024];

to declare a static array, or

char* b = new char[1024];

to allocate it at runtime?

Link to comment
Share on other sites

Link to post
Share on other sites

i agree with your agreement with my statement, but i disagree with yours

 

isn't it

char b[1024];

to declare a static array, or

char* b = new char[1024];

to allocate it at runtime?

Oh yes....my bad...I have been away from C++ for too long, I have forgotten some of the syntax :P

 

It is char b[1024];

 

I shall edit my last post to correct that

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

I agree with Ciccioo....with that said, if your heart is content on using char* instead of string keep reading (which I can understand, I still prefer that method but just because I built up a collection of stuff that needs char*).

 

So to address a few things that others have said.

b is uninitialized...so it point to a random part of memory.  std::cin will try writing to where b is pointing to (or from my recollection)...so if b is pointing to a random place it is trouble.

The next problem is why @Nineshadow and @JamieF 's solutions didn't work.  The reason is simple "b" and "" are both a dedicated part of memory....you should not be manipulating it at all.  "b" and "" are effectively constant pointers....their memory location is fixed.  This can't be said enough NEVER try to manipulate characters that have been added using quotes.

 

The better solution to this type of problem is creating the actually memory space (as follows)

char b[1024]; //This creates room for 1023 characters, not 1024...the array has 1024 slots...this is because the last input is always 0 to tell end of lineor another syntaxchar* b = char[1024]; //Wrong syntax sorry.  Thanks Ciccioo for correcting me

This creates a problem though, only 1024 characters can be written...although this is hardly a problem (if you are expecting the user will never do this)...just something to keep a note of...as there are ways to solve this, but I really don't feel like writing them out.

 

 

Now we approach a new problem.  You will be getting errors in your code due to if(b=e) and all your other b=___.  There are a few flaws in this, first b=e will simply makes b = e...and since e points to uzero in memory it will always return true.  As a note even b == e is incorrect.  b == e will compare where the memory is pointing.  e.g. e points to uzero at location 0x222222 but b points to uzero at location 0x12121212...while b == e returns false it still is equal.  strcmp was built for this (fyi strcmp is actually a dangerous function to use, but in this case it would be okay).

 

 

So...after writing all of this, go with Ciccioo's solution of using string....I don't think you are ready to handle the concepts of pointers at the moment....but you are welcome to try, and I can always help if you get more errors :P

Ok so I tried both ways and they worked, but with a problem. If I type for example t, the second if statement runs and not the last one. Am I doing something wrong? Also after all the editing the code now looks like:

#include <iostream>#include <cmath>#include <conio.h>#include <string>int main(){	long double dx, uzero, t, a;	char b[1024];	std::cout << "Which is the unknown variable?" << std::endl;	std::cin >> b;	if (b == "u0")	{		std::cout << "Now input the stuff you know in the following order: dx, t, a" << std::endl;		std::cin >> dx >> t >> a;		uzero = dx / t - 1 / 2 * a*t;		std::cout << uzero;	}	if (b == "dx");	{		std::cout << "Now input the stuff you know in the following order: u0, t, a" << std::endl;		std::cin >> uzero >> t >> a;		dx = uzero*t + 1 / 2 * a*pow(t, 2);		std::cout << dx;	}	if (b == "a")	{		std::cout << "Now input the stuff you know in the following order: dx ,t , u0 " << std::endl;		std::cin >> dx >> t >> uzero;		a = 2 * dx / pow(t, 2) - 2 * uzero / t;		std::cout << a;	}	if (b == "t")	{		std::cout << "I am not that smart, yet!";	}		_getch();	return 0;	}

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry I am in a rush so this will be very quick....but you have a ; on the if statement

if (b == "dx");

so it will always run the second one :P since the if statement is being consumed by the ;

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

i don't know how you're compiling it and if actually works on your machine, but it doesn't on mine

 

in fact, it's not supposed to work, comparing char arrays that way is wrong and shouldn't work, it should compile giving warnings and the if statements shoud always see a "false" result

the correct way to compare char arrays with string literals is via strcmp

char test[10];std::cin >> test;if(strcmp(test, "hey") == 0){        // stuff if they're equal}else{        // other stuff if they're not}

this is also mixing C and C++, which is not the best, especially while learning

 

anyway, the == comparison only works with c++ strings

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry I am in a rush so this will be very quick....but you have a ; on the if statement

if (b == "dx");

so it will always run the second one :P since the if statement is being consumed by the ;

Oh that evil semicolon >:) Thanks!

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

i don't know how you're compiling it and if actually works on your machine, but it doesn't on mine

 

in fact, it's not supposed to work, comparing char arrays that way is wrong and shouldn't work, it should compile giving warnings and the if statements shoud always see a "false" result

the correct way to compare char arrays with string literals is via strcmp

char test[10];std::cin >> test;if(strcmp(test, "hey") == 0){        // stuff if they're equal}else{        // other stuff if they're not}

this is also mixing C and C++, which is not the best, especially while learning

 

anyway, the == comparison only works with c++ strings

I am using visual studio express 2013. It works on my machine, although I did follow your advice and now the code is only c++ and not a mix :D

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×