I've been working on some code that I have been interested in doing for a little while. Essentially, my goal is to take a string, of numbers, say.... 12345
My code would be something like this.
string a ("12345");
int i, j;
for (i=0; i < a.length(); i++)(
cout << a.at(i) << endl);
(J is used later).
My main goal here, is to use a for loop to print out the elements from string in the following fashion:
1
12
123
1234
12345
However, my main issue has been getting more than 1 character of the string on each line... I can't think of a clever way to do it without using an array....
Can someone fill me in on the logic (and perhaps some commented source code?) to help me understand what is beyond my grasp here?
I gave this a shot and it worked somewhat to spec (only printed out 1234, but I can fix that easily enough). My main issue with that is that I was also hoping to count back down using another for loop. That would mean that my overall output would be:
1
12
123
1234
12345
12345
1234
123
12
1
I'm trying to do this using 2 for loops (my buddy asked me to try it out with for, and I'm struggling), I would probably do it easier with a while....
The second double for loop would look similar to the first, except initialization of the inner loop counter variable would be reversed, so as to count down from j = i to 0 inclusive, while the outer loop's initialization would begin at a.length-1 and count down (I think).
If you are uncomfortable with for loops, this can be done with while loops, but would take a bit more work in keeping track of and updating the index variables.
The mechanics of a for loop are simple:
for ( int i = 0; //initialization: gets done ONCE at the beginning of the loop.
___________ i < limit; //while the value 'i' is below (less than) the value at 'limit'.
__________________ i++ ) //increment i AFTER the loop is executed.
The nice thing about for loops is all the dealings with iterator variables (i, in the above example), is taken care of in that one line. You'll get used to the idea of ';' after each part of the loop - that tends to trick people up when first learning them.
The initialization gets done only once, at the beginning of the loop.
The condition (thing in the middle) is checked EACH TIME BEFORE the loop executes
The incrementation (or decrement) is done EACH TIME AFTER the loop executes.
After the increment or decrement (or whatever change is made to the counter variable), the CONDITION in the middle is reevaluated. If it evaluates to true, the body of the loop is executed again, (with the new condition of the counter variable, if any), and the process completes until the condition evaluates to false, at which point the body is NOT executed again, and the loop terminates.
A string is a sequence of characters, what you have done above is print out integer numbers in succession. To do this with strings, (if you want to see numbers on the screen), you would need to declare it as you had above: a("12345");
Access each character one at a time, and print it:
cout << a[position];...
Also, see the example I posted above. Compile and run, see how it works.
The example you posted above is a good start on my thoughts, however my main issue right now is simply not knowing / understanding how to properly use a for loop with a string output. I was able to solve my problem using 2 nested for loops, but I'm not content with simply solving the problem... my goal isn't to simply be right, but to understand how c++ works as a language.
C++ is very different in some of the conventions of logic than I thought it would be... I've been struggling with the way the c++ tutorials on here are presented (they seem to assume a pretty high base knowledge with some of their definitions / code examples)
I applaud your genuine interest in learning. I am a beginner as well, and share your interest in understanding what happens.
If you are unfamiliar with how for loops work, I've found the best way to learn about things like that is to copy/past examples into your code and change bits and parts of it, and see how it affects your output.
1 2 3 4 5
for ( int i = 0; i < 5; i++ )
{
//for loop body
cout << i << "\n";
}
Try copy/pasting that into your editor, compile+run, and see the output. Then change some of the numbers, then the variables, then the condition (i < 5 ), then the iteration (i++). Watch what happens when you change each of these to your liking and run it.
I have a pretty decent grasp on how the for loops are working (one increments by one when the sub-loop hits the present value of i, then the sub-loop gets reset as i increments) my big issue is not really understanding how to handle strings.
I tried to use something like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
int i=0, j=0;
string a("12345");
for (i=0; i<5; i++ ){
for (j=0; j<=i; j++){
cout << a.npos;
}
cout << endl;
}
cin >> j;
return 0;
}
For npos: http://www.cplusplus.com/reference/string/string/npos/
a[j] works because a string is a character array.
Character arrays are defined as: const char a[] = "my characters";
This character array can be accessed like any other: a[position]
The string class just encapsulates additional functionality of character arrays that is normally not provided just using arrays (though there are methods for dealing with them). For the purpose above, you can think of them the same.