| | |
Converting a String to an int array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 10
Reputation:
Solved Threads: 0
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:
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
C++ Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode"> for(i=0;i<MAX_DIGITS;i++) { n[i]=num[i]-'0'; if(n[i]>9 or n[i]<0) n[i]=0; cout<<"n["<<i<<"]"<<n[i]<<"\n"; }</code></span>
ps- MAX_DIGITS is a global constant
Last edited by geekychick; Oct 10th, 2007 at 1:33 am.
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
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
•
•
Join Date: Sep 2007
Posts: 10
Reputation:
Solved Threads: 0
Well this is what i have written:
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
C++ Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode">int main() { int numbr1[MAX_DIGITS], numbr2[MAX_DIGITS], sum[MAX_DIGITS]; cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(numbr1); cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(numbr2); return 0; } //Read Function void readBig(int n[]) { string num; int i,x,j=0; cin>>num; for(i=0;i<MAX_DIGITS;i++) { n[i]=num[i]-'0'; cout<<"nbefore["<<i<<"]"<<n[i]<<"\n"; //n[i] before my if condition if(n[i]>9 or n[i]<0) n[i]=0; cout<<"n after["<<i<<"]"<<n[i]<<"\n";//n[i] after my if condition system("PAUSE");//added system pause to see what was happening line by line } cout<<endl; for(i=0;i<=(MAX_DIGITS-1)/2;i++) { x=n[i]; n[i]=n[MAX_DIGITS-1-i]; n[MAX_DIGITS-i-1]=x; } for(i=0;i<MAX_DIGITS;i++) { cout<<"n["<<i<<"]"<<n[i]<<"\n"; //after reversing order } for(i=0;i<MAX_DIGITS;i++) { while(n[i]==0) { i++; j=j+1; } } cout<<j<<endl; for(i=j;i<MAX_DIGITS;i++) { n[i-j]=n[i]; } for(i=MAX_DIGITS-j;i<MAX_DIGITS;i++) { n[i]=0; } for(i=0;i<MAX_DIGITS;i++) { cout<<"n["<<i<<"]"<<n[i]<<"\n";// after rev. order and shifting array "j" positions } }</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
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
Val
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Problems from string to int array (C)
- String to Int (C++)
- Converting a String into an integer array (C)
- Conver int Array into a String (Java)
Other Threads in the C++ Forum
- Previous Thread: String sorting troubles
- Next Thread: Enabling a button after previous button click.
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






