Hi guys, I am having trouble with a specific problem. Is there a way to split a string of integers? For example, I want the user to input a big number, such as 32101234567890987654, and then have the program split the string into a string of 8 integers starting from the end of the string. "32101234567890987654" would be stored as x1=90987654, x2=12345678, x3=3210... I'm thinking that vectors need to be used, but not entirely sure about that..Anyone have any idea?
Thanks sohguanh...This seem to work also, but how can I make it store the strings into variables x1, x2, x3, etc. in the for() and if() loops? In the for() loop, I'm not able to store it into xi(says xi is not declared, although that is what I would want it to do. Sorry, I am still a beginner with only one month of C++ experience. I am learning as much as I can though.
There's no need for a vector, is there? It's not like your array will have to be changing length and you can determine its final size before you even reserve memory for it (see above).
Use: int* arr = newint[count];
And, sohguanh, you're coming very close to giving a full solution. Please do not.
Thank you both for the help, I will study these topics a bit and let you know how it goes. This is actually part of a larger program, in which I need to convert a string of integers into a coefficient vector, then be able to add two inputted numbers, and then convert the solution back to a string. Although sohguanh has shown me a few topics which I need to touch upon to get started with this program, he has not really given me a full solution, so no worries. Once again, thank you for the help. I will update if I have any more trouble with this.
Ok, I've reseached quite a bit and have come up with a solution. The only problem is that it does not work as intended. I have a header file, an implementation file, and a test file. I want the program to ask the user to input a big natural number. Then I want to convert the string into an integer and store it a vector. I want to store it in a vector in blocks of 8 integers starting from the end of the string. Example: if s = "12345678910987654321", then vec=87654321, vec=56789109, and vec=1234. Here is what I have so far:
I get an error saying that vec isn't declared in big_nat.cpp. Also, if I change the to_string() to return something I know will work, the program compiles, but crashes if I input any number with more than 8 integers... This program is making me go crazy! Someone help please! =)
It almost hurts watching the struggle here!
Perhaps a different approach would be easier for khris to work with.
The following is not a full solution for at least 2 reasons:
1) The calls to test.copy() generate warnings about the use of "unsafe" parameters. The program works anyways though (for the couple of test runs I gave it) because the parameter values are correct. Looks like I need to learn about "iterators".
2) The 8 digit #'s generated are not being saved, they are found one-by-one then overwritten with the next value. You would need to create an array of integers of the right size to store them in (or use a vector ).
string test;// = "32101234567890987654";// 20 chars
cout << "Enter a big number: ";
cin >> test;
cout << test << endl;
int len = test.length();
cout << "len = " << len << endl;
int lastLen = len%8;
cout << "lastLen = " << lastLen << endl;
int Nsubs = len/8;
cout << "Nsubs = " << Nsubs << endl;
int n = 0;
for(int j=1; j<= Nsubs; j++)// all of the full 8 char cases
test.copy( sub_str, 8, len - 8*j );// last arg reveals reason for indexing from 1 not 0
n = atoi( sub_str );
cout << n << endl;
if( lastLen )// the last < 8 digit case
test.copy( sub_str, lastLen, 0 );// this generates a warning about unsafe parameters
sub_str[lastLen] = '\0';// it works because the correct values are being used
n = atoi( sub_str );
cout << n << endl;
}// end of main()
fun2code, thanks for this. This makes much more sense to me and I was able to compile it without any errors. I am having one problem though..I was able to store the integers in a vector, but I want to be able to pop it back out and convert it to a string for outputting to the screen. I think I am doing it correctly, but the compiler doesn't think so. Can you or anyone look through the to_string() fuction I have in big_nat.cpp and let me know where I went wrong?
I found another mistake. I should be using vec.back(), before the pop_back()...The problem is that it only outputs vec instead of the whole string...Anyone know how to go about storing each vector element into separate variables and then concatenating them? Or is there a more efficient way to do this?
You're welcome. I'm glad my code was helpful.
I'm not sure I see what you are trying to do. I can't troubleshoot your to_string() function because I am not familiar with the methods used there. I would be using a dynamic array instead of a vector (as suggested by Albatross in an earlier post this thread).
I see this clue though:
The problem is that it only outputs vec instead of the whole string..
What whole string? The one entered by the user? This was stored in the string 'test' and ought to be intact, so cout << test << endl; should still display it. But this is too simple, I must have the task wrong. Please clarify the nature of the remaining task.