HEY im pretty lost i need to make a program where user has in enter a name and after it has been entered it has to repeat the name 100 times can someone please help me im fairly new and i cant get it to work =/. this is what i have so far...

#include <iostream>
#include <string>

using namespace std;

int main ()
{
char name[50];

       cout<<"Insert Name"<<'\n';
       cin.getline (name, 50);
     


 return 0;

}

Recommended Answers

All 10 Replies

What you are looking for is a for() loop

Take a look here at control statements (including the for() loop) here.

What you are looking for is a for() loop

Take a look here at control statements (including the for() loop) here.

ok but how can i make it loop the name it only shows u how to loop a number

You put

cout << name << endl;

inside your loop.

I would recommend that you go through that tutorial that I linked you and you try to figure out what is going on.

You put

cout << name << endl;

inside your loop.

I would recommend that you go through that tutorial that I linked you and you try to figure out what is going on.

i got it finally took me about 10 min to figure it out xD ty though this is my code

#include <iostream>
    #include <string>
     
    using namespace std;
     
    int main ()
    {
    char name[50];
    
    cout<<"Insert Name"<<'\n';
    cin.getline (name, 50);
     while (name[50] < 100)
     {
          cout<<name<< '\n';
          ++name[50];
     }
     
     system ("pause");
    return 0;
     
    }

Review the link that was posted before to get a better handling on loops, then try doing your program again.

Mark198995
Is here any thing wrong in your last code?

Mark198995

i got it finally took me about 10 min to figure it out xD ty though this is my code

It is working.What problem has arise. I dont find any thing wrong in your last post.

He is lucky that this works. The reason it does is because when you declare an array the last element is automatically set to NULL (which is 0). Then you use getline() which extracts n-1 characters and adds a NULL at index n. So now you have two NULL characters in a row. Then you come to the while loop that checks element 50 (which is NULL) and that is less than 100 and then you increment the 50'th element by one each time. If you did not use getline() then you would have junk being printed out after the name you entered if the name was as long as the array was. This is because you are overwriting the original NULL character that the array created when the array is being made.

The "proper" (because there are many ways to do this) way of doing this is to use a for() loop that uses an integer to count to 100.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	char name[50];

	cout << "Insert Name" << '\n';
	cin.getline(name, 50);

	for( int i = 0; i < 100; i++ )
	{
		cout << name << '\n';
	}

	return 0;
}

I would really not recommend doing what you did because you might not be running into problems with it now, but something will happen if you do not use getline() or if you use getline() with an extraction size of your arraysize + 1.

sfuo,
You are right.

The "proper" (because there are many ways to do this) way of doing this is to use a for() loop that uses an integer to count to 100.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	char name[50];

	cout << "Insert Name" << '\n';
	cin.getline(name, 50);

	for( int i = 0; i < 100; i++ )
	{
		cout << name << '\n';
	}

	return 0;
}

I would really not recommend doing what you did because you might not be running into problems with it now, but something will happen if you do not use getline() or if you use getline() with an extraction size of your arraysize + 1.

Thank you this is how i really wanted it but i couldnt figure it out for the life of me so i just did the simple way xD... i would appreciate if maybe you can mentor me a lil more if possible u can message me your numbe or skype or whatever

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.