# help on debugging

This is a discussion on help on debugging within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp I'm even more confused. If you have a integer, it is stored in binary in the ...

1. Originally Posted by matsp
I'm even more confused. If you have a integer, it is stored in binary in the computer. If it prints as 12, 14, 0x0c or something else is presentation. If you have a number you want to present in octal, then use the oct. You can not "convert an integer in decimal to octal", because integers are stored internally in the computer as binary. If you want to display an integer as binary, then do this:
Code:
```std::string result;
int x = 12;  // Or 012 if you want it to be octal. 0x12 for hex.
const int numdigits = 10;  // Number of bits you want to display.
for(i = 0; i < numdigits; i++)
{
result += ('0' + (x & 1)) + result;
x >>= 1;
}```
--
Mats
well i got mixed up !!! wait a minute , if its not conversion from decimal to octal and vice versa, how does the oct do the magic? it just gives me a number that is not the previous one any more, i mean , when i enter 10 as decimal , ( here i am the only one who knows the base , ) and when i use oct. teh output number will be 12 , which is to me an octal number in integer! so then it would be stored in memory just like "001 010" representing an octal value( i think). am i right?! so what do we call this process, ? we do arithmatics on these figures i mean scintific stuff works in different bases, so if this is not conversion , what is it?, !i mean , you know , well from what you ve mentioned, i gather, to the system teh binary matters, ! no octal or decimal or stuff. , when we are explicitly working with octal and stff, system works with binary anyway, but it represents as if it is octal! is it right?!!? ( exteremly confused! sorry for being stupid! i just dont understand!)

2. Originally Posted by vart
Something like
Code:
```std::string OcttoBin2(int n)
{
std::bitset<CHAR_BIT * sizeof(int)> bs(OcttoDec(n));

std::stringstream test;
test << bs;
return test.str();
}```
it calls octal to dec and then use the output to get the its binary form?!

3. If you enter 10 (decimal) to a input operator, it will (unless you have specified otherwise) take it as a decimal number. It will be stored as 000...01010 internally in the computer. If you then print that using oct, it will come out in octal. The octal representation of 00...001010 is 12. So, the number stored in the integer is exactly the same whether you entered 0x0a, 012 or 10 [assuming the machine understands that 0x.. is hex, and 0... is octal, and not starting with 0 is decimal].

Integers, no matter how they got into the computer, are stored as binary. You can choose, when you output them and when you input them, given the right input and output methods, to DISPLAY the number in different bases. But it doesn't really change the internal value of the number.

To explain it a different way: We have a piece of wood, that happens to be two meters long. Now, we can express that as 2000 mm, 200 cm or 2m. Or as 6'6" or 78". But it's still the same piece of wood, whichever unit we describe it's length with. The same applies to displaying numbers in computers - we can use "any unit", but the actual number is the same either way.

--
Mats

4. Originally Posted by matsp
If you enter 10 (decimal) to a input operator, it will (unless you have specified otherwise) take it as a decimal number. It will be stored as 000...01010 internally in the computer. If you then print that using oct, it will come out in octal. The octal representation of 00...001010 is 12. So, the number stored in the integer is exactly the same whether you entered 0x0a, 012 or 10 [assuming the machine understands that 0x.. is hex, and 0... is octal, and not starting with 0 is decimal].

Integers, no matter how they got into the computer, are stored as binary. You can choose, when you output them and when you input them, given the right input and output methods, to DISPLAY the number in different bases. But it doesn't really change the internal value of the number.

To explain it a different way: We have a piece of wood, that happens to be two meters long. Now, we can express that as 2000 mm, 200 cm or 2m. Or as 6'6" or 78". But it's still the same piece of wood, whichever unit we describe it's length with. The same applies to displaying numbers in computers - we can use "any unit", but the actual number is the same either way.

--
Mats
thanks thanks thanks , thanks ! i just got it dude, im really amazed! so thats how it is done down there !
iwont forget this . really tanx ..Dear Mats. you taught me couple of new things!

5. hello again , ive almost completely revised the project. removed almost all of the C style string variables, but still i get that error at the end of the program! !
when i pressed debug, the program terminated, and then it pointed at the line: !

Code:
`  OurString = (*usrstr_it).second;`
in this function.:

Code:
``` int CLA::Fetch()
{
if (cntt == 0)
usrstr_it = StringCollector.begin();

while ( usrstr_it != StringCollector.end() && check2)
{
if (cntt != 0) //for first element.after a successful loop,
//this condition becomes true and next string will be fetched.
usrstr_it++;

OurString = (*usrstr_it).second; //get the string.
if (OurString == "exit" ||OurString == "Exit" ||OurString =="EXIT")
{
EXIT = true;
break;
}

cntt++;

check = 1;      //this  indicates fetch is running and should be running.
check2 = false; //geting away from infinite loop.(gets out of the loop when we have he string.

}
if ( usrstr_it == StringCollector.end() || EXIT )
check = 0; //end of loop . end of stringcollector, the end of string processing
if (check ==0 )
condition = false;

return check;

}//end of Fetch();```
and again it indicates that the loop in main.cpp has a probelm :
ive taken a screenshot , so that you can see the actual line !!

well i dont know what to do now, heres the new source code !:
newCLA.zip

pic2
pic1
pic3
hope you can help me with it.

6. any help!

7. Code:
```                        usrstr_it++;

OurString = (*usrstr_it).second; //get the string.```
After incrementing the . . . well, I'm assuming it's an iterator . . . how do you know it's still valid? You should probably check to make sure it's still in range after you increment it.

 That's pretty funny that after some 12 hours, we both post at exactly the same time (I didn't see your post) . . . [/edit]

8. Originally Posted by dwks
Code:
```                        usrstr_it++;

OurString = (*usrstr_it).second; //get the string.```
After incrementing the . . . well, I'm assuming it's an iterator . . . how do you know it's still valid? You should probably check to make sure it's still in range after you increment it.

 That's pretty funny that after some 12 hours, we both post at exactly the same time (I didn't see your post) . . . [/edit]
thanks dear dwks,
( about post :yeah i didnt even notice that)

----------------------
edited: Got it , thanks im checking it now!.
===============================
edited. it works like a charm now.

9. OHHH GOD , Oh GOD , DWKS , YOU ARE AN ANGLE MAN, its Gone , i cant BEleive it , it just happened ! it worked .!! i realllly dont know how to thank you .to thank you alll Dears , my treasures ! ( deeply touched !)

A big big big enormous Thank to you all specially dear DWKS!

10. But if userit is just before end userit++ will put it AT end. That is not a valid place to look at the content.

--
Mats

11. Originally Posted by matsp
But if userit is just before end userit++ will put it AT end. That is not a valid place to look at the content.

--
Mats
yeah, so what should i do ? nothing crosses my mind now to fix this!!
at the moment , ill try putting it lets say at the end!. i ll you what happens.(
but if is not right , ( as it is not right! ) how come the program just works fine?!)

12. changed to :
Code:
``` int CLA::Fetch()
{

if (cntt == 0)
usrstr_it = StringCollector.begin();

while ( usrstr_it != StringCollector.end() && check2)
{
if (cntt != 0) /*for first element.after a successful loop,
this condition becomes true and next string will be fetched.*/

if (usrstr_it == StringCollector.end())
{
break;
}
OurString = (*usrstr_it).second; //get the string.
if (OurString == "exit" ||OurString == "Exit" ||OurString =="EXIT")
{
EXIT = true;
break;
}

cntt++;

check = 1;      //this  indicates fetch is runing and should be running.
check2 = false; //geting away from infinit loop.(gets out of the loop when we have he string.

usrstr_it++;
}
if ( usrstr_it == StringCollector.end() || EXIT )
check = 0; //end of loop . end of stringcollector, the end of string processing
if (check ==0 )
condition = false;

return check;```
and progarm still works fine ! ( i think its better and more secure than before ! many thanks to you Dear Mats.)

Page 4 of 4 First 1234