Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
The following program (below my question) produces the results I want, but I am trying to create a for loop to replace all of the cout statements.
What I have so far is:
for (int i = 0; i <= len; i++)
cout << str[i + 1];This will prduce: "eed help with C++."
Could someone clue me in on how to create the for loop? Should I be using a nested for loop? Any help would be appreciated.
Program:#include<iostream>
#include<cstring>using namespace std;
void main ()
{
const int arraySize = 20;
char str[arraySize] = "Need help with C++.";
int len = strlen (str);cout << "The sentence \"Need help with C++.\" has "
<< len << " characters."
<< endl << endl;cout << str << endl;
cout << str + 1 << endl;
cout << str + 2 << endl;
cout << str + 3 << endl;
cout << str + 4 << endl;
cout << str + 5 << endl;
cout << str + 6 << endl;
cout << str + 7 << endl;
cout << str + 8 << endl;
cout << str + 9 << endl;
cout << str + 10 << endl;
cout << str + 11 << endl;
cout << str + 12 << endl;
cout << str + 13 << endl;
cout << str + 14 << endl;
cout << str + 15 << endl;
cout << str + 16 << endl;
cout << str + 17 << endl;
cout << str + 18 << endl;
}Results:
The sentence "Need help with C++." has 19 characters.
Need help with C++.
eed help with C++.
ed help with C++.
d help with C++.
help with C++.
help with C++.
elp with C++.
lp with C++.
p with C++.
with C++.
with C++.
ith C++.
th C++.
h C++.
C++.
C++.
++.
+.
.

YOUR CODE:
for (int i = 0; i <= len; i++)
cout << str[i + 1];Assumption: len is the number of characters in the string, not the position of the string ending NULL.
Your loop is looping thru i values of 0 thru len. Thus your cout (with it's i+1) outputs str positions 1 thru len+1.
There are two problems. You are skipping str[0], which you noted. Also, str[len+1] is a NULL character, which you don't normally output.
So what you want is for the cout to output str positions 0 thru len (str[len] is the last character of the string. str[len+1] is the null).
Changing the i+1 to i in the cout, will cause you to output 0 thru len.
for (int i = 0; i <= len; i++)
cout << str[i];

Thanks for the input.
len is the length of the string.
Using:
for (int i = 0; i <= len; i++)
cout << str[i];outputs "Need help with C++" only once. I need to loop through the sentence and print it less one character each time.
The result should look like the following:
Need help with C++.
eed help with C++.
ed help with C++.
...

The generic form of your cout statements is:
cout << str + i << endl;
If you put this is a for loop that loops i from 0 to len, you should have it.

I don't know that the for loop lends itself to be that useful here, but with a little trickery, you can try something like...
#include <iostream>
int main(void)
{
int i, j;
char str[] = "Need help in C++.";
i=j=0;
for(; str[j] != '\0'; )
{
if(str[i] == '\0')
{
std::cout << std::endl;
j++;
i=j;
}
else
std::cout << str[i++];
}while(std::cin.get() != '\n');
return 0;
}... to force the desired effect. If the above works, then that grade is mine. :)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |