Converting a String to an int array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 10
Reputation: geekychick is an unknown quantity at this point 
Solved Threads: 0
geekychick geekychick is offline Offline
Newbie Poster

Converting a String to an int array

 
0
  #1
Oct 10th, 2007
I'm attempting to write a function that scans a number from the user as a string and converts the string to an array of int type, then reverses the order of the array and puts zeroes where no scanned data exists (eg. program says "Enter a number up to 5 digits" user imputs 22, if working properly, the function should scan the number 22 as a string, and reverse the order so, n[0]=2,n[1]=2,n[2]=0,n[3]=0,n[4]=0. ) My issue is creating the zeroes I know that if nothing is scanned in by the user, some random number will be stored in the memory slot (for lack of a better term) so I wrote this for loop in hopes of obtaining my zeroes:
  1. <span class="ad_notxt"><code class="inlinecode">
  2. for(i=0;i<MAX_DIGITS;i++)
  3. {
  4. n[i]=num[i]-'0';
  5. if(n[i]>9 or n[i]<0)
  6. n[i]=0;
  7. cout<<"n["<<i<<"]"<<n[i]<<"\n";
  8. }</code></span>
where num is the string scanned by the user, and n[] is my int array. Here's my problem, the number 9 is always the "random number" stored in the position num[3] the second time i call the function if the string is less than 4 digits, so my "if" condition doesn't catch it. is there a better way to approach this?
ps- MAX_DIGITS is a global constant
Last edited by geekychick; Oct 10th, 2007 at 1:33 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Converting a String to an int array

 
0
  #2
Oct 10th, 2007
I don't think there's any "random numbers" being stored. Keep in mind that the array num[] does not get cleared out every time you read in a new string. It simply get overwritten from the 0 position for as many characters (including the terminating NULL) as needed.

So, if on your first run you enter 12345 (I hope you sized the num[] array to six elements!) the array will contain the characters 1 2 3 4 5 \0. On your next run, user enters 765. The array contains 7 6 5 \0 5 \0. How does your code handle this?

Val
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 10
Reputation: geekychick is an unknown quantity at this point 
Solved Threads: 0
geekychick geekychick is offline Offline
Newbie Poster

Re: Converting a String to an int array

 
0
  #3
Oct 10th, 2007
Well this is what i have written:
  1. <span class="ad_notxt"><code class="inlinecode">int main()
  2. {
  3. int numbr1[MAX_DIGITS], numbr2[MAX_DIGITS], sum[MAX_DIGITS];
  4. cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
  5. readBig(numbr1);
  6. cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
  7. readBig(numbr2);
  8. return 0;
  9. }
  10.  
  11. //Read Function
  12.  
  13. void readBig(int n[])
  14. {
  15. string num;
  16. int i,x,j=0;
  17. cin>>num;
  18. for(i=0;i<MAX_DIGITS;i++)
  19. {
  20. n[i]=num[i]-'0';
  21. cout<<"nbefore["<<i<<"]"<<n[i]<<"\n"; //n[i] before my if condition
  22. if(n[i]>9 or n[i]<0)
  23. n[i]=0;
  24. cout<<"n after["<<i<<"]"<<n[i]<<"\n";//n[i] after my if condition
  25. system("PAUSE");//added system pause to see what was happening line by line
  26. }
  27. cout<<endl;
  28. for(i=0;i<=(MAX_DIGITS-1)/2;i++)
  29. {
  30. x=n[i];
  31. n[i]=n[MAX_DIGITS-1-i];
  32. n[MAX_DIGITS-i-1]=x;
  33. }
  34. for(i=0;i<MAX_DIGITS;i++)
  35. {
  36. cout<<"n["<<i<<"]"<<n[i]<<"\n"; //after reversing order
  37. }
  38. for(i=0;i<MAX_DIGITS;i++)
  39. { while(n[i]==0)
  40. {
  41. i++;
  42. j=j+1;
  43. }
  44. }
  45. cout<<j<<endl;
  46. for(i=j;i<MAX_DIGITS;i++)
  47. {
  48. n[i-j]=n[i];
  49. }
  50. for(i=MAX_DIGITS-j;i<MAX_DIGITS;i++)
  51. {
  52. n[i]=0;
  53. }
  54. for(i=0;i<MAX_DIGITS;i++)
  55. {
  56. cout<<"n["<<i<<"]"<<n[i]<<"\n";// after rev. order and shifting array "j" positions
  57. }
  58.  
  59. }</code></span>

and this is my output:
Please enter a number up to 5 digits: 123
nbefore[0]1
n after[0]1

nbefore[1]2
n after[1]2

nbefore[2]3
n after[2]3

nbefore[3]-48
n after[3]0

nbefore[4]-42
n after[4]0


n[0]0
n[1]0
n[2]3
n[3]2
n[4]1
2
n[0]3
n[1]2
n[2]1
n[3]0
n[4]0
Please enter a number up to 5 digits: 2
nbefore[0]2
n after[0]2

nbefore[1]-48
n after[1]0

nbefore[2]19
n after[2]0

nbefore[3]9
n after[3]9

nbefore[4]-45
n after[4]0


n[0]0
n[1]9
n[2]0
n[3]0
n[4]2
3
n[0]0
n[1]2
n[2]0
n[3]0
n[4]0
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 10
Reputation: geekychick is an unknown quantity at this point 
Solved Threads: 0
geekychick geekychick is offline Offline
Newbie Poster

Re: Converting a String to an int array

 
0
  #4
Oct 10th, 2007
aaaaand I'm really bad at this, I just discovered SEVERAL flaws in my logic (eg- the string 100 for one) I have no idea what i'm doing anymore
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Converting a String to an int array

 
0
  #5
Oct 10th, 2007
Ahh, now I see a big problem. In readBig, you take the input as a string type. When I run your code (after fixing a couple thing that kept if from compiling in VC++ 2005) the program would crash on a 3 digit input.

For the string, accessing any characters past the input is undefined. Well, many implementations do tack on the NULL terminator ('\0', which you see as the value -48 after the subtraction), but past that you're in no-man's-land. Change your declaration string num; to char num[6] and the program works fine.

Val
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 10
Reputation: geekychick is an unknown quantity at this point 
Solved Threads: 0
geekychick geekychick is offline Offline
Newbie Poster

Re: Converting a String to an int array

 
0
  #6
Oct 10th, 2007
why 6? sorry if that's a dumb question...
Last edited by geekychick; Oct 10th, 2007 at 3:12 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Converting a String to an int array

 
0
  #7
Oct 10th, 2007
You have to remember the extra slot for the null terminator '\0'
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC