Computing.Net > Forums > Programming > C++ - need FOR loop help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ - need FOR loop help

Reply to Message Icon

Name: guinness4all
Date: January 19, 2004 at 13:52:08 Pacific
OS: Windows XP
CPU/Ram: Pentium III & 256
Comment:

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++.
++.
+.
.




Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: January 19, 2004 at 15:14:41 Pacific
Reply:

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];


0

Response Number 2
Name: guinness4all
Date: January 19, 2004 at 16:18:06 Pacific
Reply:

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++.
...



0

Response Number 3
Name: JasonR
Date: January 19, 2004 at 18:59:34 Pacific
Reply:

Don' put a semicolon on the end of the for loop and
it should work


0

Response Number 4
Name: Don Arnett
Date: January 19, 2004 at 19:00:20 Pacific
Reply:

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.


0

Response Number 5
Name: Don Arnett
Date: January 19, 2004 at 19:04:56 Pacific
Reply:

I forgot to add that I, obviously, didn't read your first post very carefully.

Sorry about that.


0

Related Posts

See More



Response Number 6
Name: Ronin1
Date: January 19, 2004 at 20:31:16 Pacific
Reply:

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. :)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ - need FOR loop help

Batch file nested FOR loops help www.computing.net/answers/programming/batch-file-nested-for-loops-help/15196.html

Batch programming for loop help www.computing.net/answers/programming/batch-programming-for-loop-help/18116.html

FOR loops in C www.computing.net/answers/programming/for-loops-in-c/8449.html