Please help :(

Hi,

I need help.

I am trying to print out the following:

A
Ap
App
Appl
Appli
Applic
Applica
Applicat
Applicati
Applicatio
Application
Application
Application o
Application of
Application of
Application of C
Application of C+
Application of C++
Press any key to continue . . .

The code is correct so far.
However, in the void printPattern(), I have to specify that i<19.

So every time i change the word in string1[], I have to change the number in my void printPattern().

Is there a better way to do it such that I do not need to specify that i<19?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

void printPattern(char* strMessage);            // function prototype


int main(int argc, char* argv[])
{
	char string1[] = ("Application of C++");
	printPattern(string1);
	
	return 0;
}


void printPattern(char* strMessage)              //printPattern() function
{	
	for (int i=1; i<19; i++) 
	{
		for (int j=0; j<i; j++)
		{
			cout << strMessage[j];
		}
				cout << endl;
	}
}

Don't make things more complicated than they really are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::string MyString = "Application of C++";

    for (int i = 0; i <= MyString.length(); ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            std::cout << MyString[j];
        }

        std::cout << std::endl;
    }

    return 0;
}
I was given:
char string1[] = ("Application of C++");

My objective is to create a function to print out the on top.

I created the function on printPattern();

However, due to this issue, I could not use i<string1 or i<strMessage as both are declared char.

Is there any way I can use char and make i<"char" and then j<i?
http://www.cplusplus.com/reference/cstring/strlen/


Thank you keskiverto!

That solve my problem.
Topic archived. No new replies allowed.