Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Mystery -'0' allows string to int array!
New Posts  All Forums:Forum Nav:

Mystery -'0' allows string to int array!

post #1 of 18
Thread Starter 
int main(int argcs, char* pArgs[])
{
string s = "12345";
int numbers[s.length()];
for(int i=0; i<s.length(); i++)
{ numbers[i]=s[i]-'0'; }
system("pause");
return 0;
}

That little operator -'0' allows me to convert the character in the string to an integer! Amazing! But what exactly does it do? I never learned that from C++ for Dummies.
Edited by bad_haze - 6/13/11 at 5:36pm
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
post #2 of 18
All characters have a numerical value, here's an ascii table for reference: http://www.asciitable.com/

The numerical value of the character '1' is 49.
The numerical value of the character '0' is 48.

You're just subtracting 48 from 49, 50, 51, 52, and 53 in this case, giving you 1, 2, 3, 4, and 5.

What you really want to be doing instead of what you're doing is this:

Code:
int main(int argcs, char* pArgs[])
{ 
string s = "12345";
int numbers[s.length()];
for(int i=0; i<s.length(); i++)
{ numbers[i]= atoi(s[i]); } 
system("pause");
return 0;
}
The function atoi is described here:
http://www.cplusplus.com/reference/c.../cstdlib/atoi/
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #3 of 18
Thread Starter 
yes, the atoi seems very helpful! However, when I compile your code I get "invalid conversion from 'char' to 'const char*'".

So... I tried to pass through the value via using a pointer and address:

int main(int argcs, char* pArgs[])
{
string s = "12345";
int *numbers = new int[s.length()];
for(int i=0; i<s.length(); i++)
{ numbers[i]= atoi(&s[i]); }
cout<<numbers[0]<<endl;
system("pause");
return 0;
}

But the output for numbers[0] is "12345"
For numbers[1] is "2345"
numbers[2] is "345"
etc...

What are we missing here?
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
post #4 of 18
Firstly, atoi is a c function so you'll want to call it on a c-string rather than a c++ string.

To explain what's happening you have to start with how your string is stored in memory. It is a null-terminated array of characters in the form {'1', '2', '3', '4', '5', '\\0'}

atoi takes as an argument a null-terminated string. So when i is equal to 2 you are giving atoi a pointer to the array {'3', '4', '5', '\\0'} when it looks like you think you're giving it a pointer just to the '3' character. atoi interprets this as the string "345" and returns the integer 345 to you as a result
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 18
Quote:
Originally Posted by bad_haze View Post
yes, the atoi seems very helpful! However, when I compile your code I get "invalid conversion from 'char' to 'const char*'".

So... I tried to pass through the value via using a pointer and address:

int main(int argcs, char* pArgs[])
{
string s = "12345";
int *numbers = new int[s.length()];
for(int i=0; i<s.length(); i++)
{ numbers[i]= atoi(&s[i]); }
cout<<numbers[0]<<endl;
system("pause");
return 0;
}

But the output for numbers[0] is "12345"
For numbers[1] is "2345"
numbers[2] is "345"
etc...

What are we missing here?
When you do &s[i], I believe the compiler sees that as (&s)[i]. Try wrapping it like this:
Code:
{ numbers[i]= atoi( &(s[i]) ); }
But as the person above me said, it may be trying to convert starting at that address until it hits a null terminator, so if the above code doesn't work, try this:
Code:
int main(int argcs, char* pArgs[])
{ 
string s = "12345";

// I got rid of the needless pointer, you weren't deleting so this was a memory leak.
int numbers[s.length()];

for(int i = 0; i < s.length(); i++)
{ 
    const char myChar = s[i];
    numbers[i] = atoi(&myChar); 

    // You were cout'ing numbers[0] instead of numbers[i]
    // So I moved this here.
    cout << numbers[i] << endl; 
}

system("pause");

return 0;
}

Edited by lordikon - 6/14/11 at 6:54am
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #6 of 18
Thread Starter 
Thank you very much! Putting each letter into a "char" address and then using atoi to convert the stored "letter" into a number has worked perfectly!
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
post #7 of 18
Quote:
Originally Posted by lordikon View Post
When you do &s[i], I believe the compiler sees that as (&s)[i]. Try wrapping it like this:
Code:
{ numbers[i]= atoi( &(s[i]) ); }
But as the person above me said, it may be trying to convert starting at that address until it hits a null terminator, so if the above code doesn't work, try this:
Code:
int main(int argcs, char* pArgs[])
{ 
string s = "12345";

// I got rid of the needless pointer, you weren't deleting so this was a memory leak.
int numbers[s.length()];

for(int i = 0; i < s.length(); i++)
{ 
    const char myChar = s[i];
    numbers[i] = atoi(&myChar); 

    // You were cout'ing numbers[0] instead of numbers[i]
    // So I moved this here.
    cout << numbers[i] << endl; 
}

system("pause");

return 0;
}
Careful with this code. atoi takes a null-terminated c-string, and will continue parsing until it hits a null. You need to ensure that the character after myChar in memory is a null character. I would suggest creating a two-char array, setting the second char to '\\0', and putting what is currently being assigned to myChar into the first element
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #8 of 18
Quote:
Originally Posted by rabidgnome229 View Post
Careful with this code. atoi takes a null-terminated c-string, and will continue parsing until it hits a null. You need to ensure that the character after myChar in memory is a null character. I would suggest creating a two-char array, setting the second char to '\\0', and putting what is currently being assigned to myChar into the first element
Fair enough, from something safer, you could go this route:

Code:

template <typename T> 
T StringToNumber ( const string &Text ) 
{
    stringstream ss(Text);
    T result;     
    return ss >> result ? result : 0;
}
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #9 of 18
Thread Starter 
Update fyi:

When I use this code:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\
int main(int argcs, char* pArgs[])
{ string s = "7316717653133062491922511967442657474235534919493 4969835203127745063262395783180169848018694788518438 58615607891129494954595017379583319528532088055111 25406987471585238630507156932909632952274430435576 68966489504452445231617318564030987111217223831136 22298934233803081353362766142828064444866452387493 03589072962904915604407723907138105158593079608667 01724271218839987979087922749219016997208880937766 57273330010533678812202354218097512545405947522435 25849077116705560136048395864467063244157221553975 36978179778461740649551492908625693219784686224828 39722413756570560574902614079729686524145351004748 21663704844031998900088952434506585412275886668811 64271714799244429282308634656748139191231628245861 78664583591245665294765456828489128831426076900422 42190226710556263211111093705442175069416589604080 71984038509624554443629812309878799272442849091888 45801561660979191338754992005240636899125607176060 58861164671094050775410022569831552000559357297257 1636269561882670428252483600823257530420752963450" ;
char *letters = new char[s.length()];
int numbers[s.length()];
for(int i=0; i<s.length(); i++)
{ char a = s[i];
//letters[i]=s[i];
//numbers[i]=atoi(&letters[i]);
numbers[i]=atoi(&a);
}
cout<<s[50]<<" "<<numbers[50];
system("pause");
return 0;
}
My output is "9" for the string extract, but "92" for the integer array extract.

But when I use this code:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\
int main(int argcs, char* pArgs[])
{ string s = "7316717653133062491922511967442657474235534919493 4969835203127745063262395783180169848018694788518438 58615607891129494954595017379583319528532088055111 25406987471585238630507156932909632952274430435576 68966489504452445231617318564030987111217223831136 22298934233803081353362766142828064444866452387493 03589072962904915604407723907138105158593079608667 01724271218839987979087922749219016997208880937766 57273330010533678812202354218097512545405947522435 25849077116705560136048395864467063244157221553975 36978179778461740649551492908625693219784686224828 39722413756570560574902614079729686524145351004748 21663704844031998900088952434506585412275886668811 64271714799244429282308634656748139191231628245861 78664583591245665294765456828489128831426076900422 42190226710556263211111093705442175069416589604080 71984038509624554443629812309878799272442849091888 45801561660979191338754992005240636899125607176060 58861164671094050775410022569831552000559357297257 1636269561882670428252483600823257530420752963450" ;
char *letters = new char[s.length()];
int numbers[s.length()];
for(int i=0; i<s.length(); i++)
{ char a = s[i];
letters[i]=s[i];
numbers[i]=atoi(&letters[i]);
//numbers[i]=atoi(&a);
}
cout<<s[50]<<" "<<numbers[50];
system("pause");
return 0;
}
I get "9" for the string extract and "9" for the integer array extract.

It seems as if atoi convert only really works when transferring between char and int arrays in this case.
Edited by bad_haze - 6/14/11 at 5:45pm
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
 
Golden Copper
(14 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 3210M (3.1 Ghz) Asus G46VW.210 Nvidia GTX 660M 2GB (GDDR5; 950/1250) Intel HD Graphics 4000M 
RAMHard DriveMonitorMouse
8GB (DDR3; 800; 1T) 750GB (5200rpm) 14" LCD Gigabyte M6900 
CPUMotherboardGraphicsRAM
AMD PII X4 940 BE (235x14.5; 3.4 Ghz; 1.475v) ASUS M4A78-E (NB: 2585Mhz) ATI HD 7870 (3.839 GB; 1000Mhz core; 1200Mhz mem) Kingston 4G Dual Channel DDRII 940 Mhz (2x2G) 
Hard DriveOptical DriveCoolingOS
Western Digital WD10EZEX (1.0 TB) ASUS DVDRW +/- & BR R Scythe Ninja Cu Windows 7 Ultimate (x64) 
MonitorKeyboardPowerCase
LG 27" W2753V Logitech White Backlit CM 750w (60a @ 1x12v) Antec 300 
Mouse
Gigabyte M6900 
  hide details  
Reply
post #10 of 18
This could the problem that rabidgnome described. Change this:

Code:
char a = s[i]; 
letters[i]=s[i];
numbers[i]=atoi(&letters[i]);
To this:
Code:
string a = s[i] + '\\0'; 
numbers[i] = atoi( a.c_str() );

Edited by lordikon - 6/14/11 at 7:37pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Mystery -'0' allows string to int array!