Problem with sending a signed int to another signed int. Almost random number given.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem with sending a signed int to another signed int. Almost random number given.
The following code is filled with holes (not that it would not make it run, it runs but with a strange problem), because it is a snippet from a bigger block of code of another program. I am doing this to clean up code. My aim in the following section of code is check to see how many elements are in a number within the vector ivar, then combine all of them into one number. So if ivar[0] = 2 and ivar[1] = 4, then the code would combine them and it would be 24. I was using math.h to do exponent operations, but it required changing several variables to doubles, and it would have made bigger problems as the code grew. So just to simplify things I made the function powerfunc. Powerfunc seems to work fine, its the loop in main that does not seem to. Loop outpus correct values but the values that are being added together gives out almost random numbers. Here is that output followed by some more details:
As you can immediatly see, everything goes fine until itemp_store is asked for its value, and it comes out wacked out. Though, if you notice, it is actualy adding the values I wanted into that negative number. So it is almost doing what it needs to do, but I don't know where it is getting that base negative number to begin with. That is basicly my question... I'll add a few comments in the code just to speed things(clear) things up if needed, but I won't swamp it. Thank you in advance!
Code:
#include <iostream>
#include <vector>
using namespace std;
// Every iteration of cout is there to just see what the value or output of something is.
int powerfunc(int x, int y)
{
// power is started at one, and is multiplied by ten everything. 1, 10, 100, 1000, 10000...
int itemp;
for ( int i = 0, power = 1; i <= y; i++, power = (power*x) )
{
// itemp doesn't really need to be changed to a different value every loop, but it
// doesn't kill anything.
itemp = power;
cout << itemp << endl;
}
cout << itemp << endl;
return itemp;
}
int main()
{
// Vector was needed in the code this code came from.
vector<int> ivar(6);
int itemp_store;
ivar[0] = 1;
ivar[1] = 5;
ivar[2] = 2;
ivar[3] = 4;
ivar[4] = 2;
ivar[5] = 8;
for ( int a = 0; a < ivar.size(); a++ )
{
if ( a == ivar.size()-1 )
{
// power goes backwards so it is sychronized when converting ivar's current
// element to it's proper integer location.
for ( int b = 0, power = powerfunc(10,a); b <= a; b++, power = (power/10) )
{
itemp_store += ivar[b] * power;
cout << "itemp_store += " << ivar[b] << " * " << power << " which equals " << ivar[b]*power << endl;
cout << "itemp_store equals " << itemp_store << endl;
}
break;
}
}
return 0;
}
PS: If you think (or know) any part of my code is outright retarded, please give me a comment why it is a waste or etc, so I can learn better coding techniques. Thanks in advance!
EDIT:
Also your complete outer for-loop (for (int a=0...)) is needles, because you only run this loop until a==ivar.size()-1 and do nothing else, so why not simply set a=ivar.size()-1 or maybe even better let the "b-loop" run as long as b<ivar.size():
Code:
for ( int b = 0, power = powerfunc(10,a); b < ivar.size(); b++, power = (power/10) ){
itemp_store += ivar[b] * power;
cout << "itemp_store += " << ivar[b] << " * " << power << " which equals " << ivar[b]*power << endl;
cout << "itemp_store equals " << itemp_store << endl;
}
Thank you it works, and yes that loop works too, I'll use it. BTW why do I have to initialize it? The following works, which is more or less what I tried to do:
Code:
int i;
i += 4;
cout << i << endl;
It outputs 4 on my screen. Or is just not good practice to do what I did, for the error I found?
xhi I know your intelligent when it comes to programming, so the following question does not try to undermine you. I'm not sure what you mean by getting lucky. It works or doesn't. Why does my last piece of code work?
You were lucky that 'i' was initialized to 0 (and not to another value). Not sure, but I have the feeling that some languages do default initializations of variables and others don't.
does not gurantee the initial value of x. x is just as likely to have the value 0 as it is to have the value 39393. youll typically hear the word "garbage" used to describe memory, and this is exactly what is in a variable initially, garbage. even if it ends up being the right value (zero in your case) it is still garbage, just lucky garbage.
so if any operation other than assignment happens to a varible first, then you should initialize the variable to 0 (or an otherwise suitable value)..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.