General [M]ayhem

Go Back   General [M]ayhem > Real Time Sub-Forums > CompuGlobalHyperMegaNet
Register Members List Mark Forums Read [M]erchandise Calendar

Reply
 
Thread Tools
Grazehell
 
Grazehell's Avatar
 
C++: atoi problem when character array has 11 or more entries

...It would put some arbitrary set of numbers into the integer once the array size goes past 11 entries. Is this a limitation of the atoi(using strtol results in the same thing)?

my code is dead simple

Code:
int_binary = atoi(char_binary);
of course I left out other parts of it but I figured it it works once it has less than 11 entries it can't be my code.
Old 04-24-2012, 07:58 PM Grazehell is offline  
Reply With Quote
#1  

Advertisement [Remove Advertisement]

Redrum
Crate&Barrel Roll
 
Redrum's Avatar
 
poast ur full code plz

maybe your char array is fixed at 11 elements
__________________
lol
Old 04-24-2012, 08:15 PM Redrum is offline  
Reply With Quote
#2  

:ninja:
My cooter sweats, and reeks like rotting sea vermon.
 
:ninja:'s Avatar
 
You're overflowing your integer. The same happens with strol since on x86 int and long are both 32 bits. Use long long and strtoull if your environment has it.
__________________
Use Linux and BSD

Last edited by :ninja:; 04-24-2012 at 09:26 PM..
Old 04-24-2012, 09:21 PM :ninja: is offline  
Reply With Quote
#3  

Grazehell
 
Grazehell's Avatar
 
Quote:
Originally Posted by :ninja: View Post
You're overflowing your integer. The same happens with strol since on x86 int and long are both 32 bits. Use long long and strtoull if your environment has it.

Does the same thing unfortunately and Visual Studios would not let me use strtoull

Quote:
Originally Posted by Redrum View Post
poast ur full code plz

maybe your char array is fixed at 11 elements

My code without all the other nonsense attached to it.


Code:
int main() {



char char_binary[100];
unsigned long long int_binary;


cout<<"Enter numbers into char array"<<endl;
cin>> char_binary;

	  
int_binary = strtoul(char_binary, NULL, 10);

cout<<"Content of interger data type is "<<int_binary<<endl;



system("PAUSE");


return 0;
}
Old 04-24-2012, 09:54 PM Grazehell is offline  
Reply With Quote
#4  

jfkil1a
 
Your code is weird to me m8, why not just declare a const or a pointer?
__________________
You open the mailbox, there is absolutely nothing inside.
Nothing after nothing came bursting out.
Old 04-24-2012, 09:59 PM jfkil1a is offline  
Reply With Quote
#5  

:ninja:
My cooter sweats, and reeks like rotting sea vermon.
 
:ninja:'s Avatar
 
Quote:
Originally Posted by Grazehell View Post
Does the same thing unfortunately and Visual Studios would not let me use strtoull

Looks like there are some replacements for winblowz since the C standards are too hard to implement:
http://msdn.microsoft.com/en-us/library/ee404774.aspx
http://msdn.microsoft.com/en-us/libr...vs.100%29.aspx
__________________
Use Linux and BSD
Old 04-24-2012, 10:03 PM :ninja: is offline  
Reply With Quote
#6  

Redrum
Crate&Barrel Roll
 
Redrum's Avatar
 
when you're populating an array of characters you can't use cin

you have use either cin.get or getline or another one of those standard functions for placing arrays of characters into the stream
__________________
lol
Old 04-24-2012, 11:08 PM Redrum is offline  
Reply With Quote
#7  

Redrum
Crate&Barrel Roll
 
Redrum's Avatar
 
er nevermind

yeah anything beyond 11 digits is probably just too big for the function
__________________
lol
Old 04-24-2012, 11:13 PM Redrum is offline  
Reply With Quote
#8  

Redrum
Crate&Barrel Roll
 
Redrum's Avatar
 
__________________
lol
Old 04-24-2012, 11:16 PM Redrum is offline  
Reply With Quote
#9  

Jehannum
 
Jehannum's Avatar
 
Quote:
Originally Posted by :ninja: View Post
You're overflowing your integer. The same happens with strol since on x86 int and long are both 32 bits. Use long long and strtoull if your environment has it.

TSRH. Either use a larger primitive (uint64_t or something) or use smaller integers. It makes no sense to declare a 100 character buffer, when the largest integer you can use is 4,294,967,295 (assuming unsigned) or 2,147,483,647 (assuming signed). Make your array 11 characters, and you'll be able to fit the smallest and largest signed ints, or make it 10 and be able to fit the largest unsigned int.
__________________
Your powers are useless! I'm wearing my tin foil underwear!

1992 300ZX: Not stock, 433 RWHP
1971 240Z: Toyota front brakes, 123 RWHP
1967 Pontiac GTO: not stock.
Old 04-25-2012, 06:04 AM Jehannum is offline  
Reply With Quote
#10  

Grazehell
 
Grazehell's Avatar
 
Thanks guys. I was not using this for binary numbers not integers(lots of 0's and 1's). I guess I would just have to go about this another way. It would not be too big of a deal.
Old 04-25-2012, 06:31 PM Grazehell is offline  
Reply With Quote
#11  

gee
MY TITLE ISNT LONG ENOUGH
 
gee's Avatar
 
If you're reading binary numbers, just use strtoul() with a base of 2.
Old 04-25-2012, 06:40 PM gee is offline  
Reply With Quote
#12  

Grazehell
 
Grazehell's Avatar
 
Quote:
Originally Posted by gee View Post
If you're reading binary numbers, just use strtoul() with a base of 2.

Yes that was be an option but I wanted to reinvent the wheel! Guess that would be more challenging than I thought!
Old 04-25-2012, 06:45 PM Grazehell is offline  
Reply With Quote
#13  

Jehannum
 
Jehannum's Avatar
 
Quote:
Originally Posted by Grazehell View Post
Yes that was be an option but I wanted to reinvent the wheel! Guess that would be more challenging than I thought!

You could always use a linked list or array, and atoi() each character of the input string, I guess.
__________________
Your powers are useless! I'm wearing my tin foil underwear!

1992 300ZX: Not stock, 433 RWHP
1971 240Z: Toyota front brakes, 123 RWHP
1967 Pontiac GTO: not stock.
Old 04-25-2012, 06:53 PM Jehannum is offline  
Reply With Quote
#14  

gee
MY TITLE ISNT LONG ENOUGH
 
gee's Avatar
 
int result = 0;
while (*str) {
result <<= 1;
if (*str++ == '1') result |= 1;
}

?
Old 04-25-2012, 07:15 PM gee is offline  
Reply With Quote
#15  

Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -7. The time now is 08:41 PM.



Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.