Please excuse me if this has already been covered before.

I'm a C++ beginner, and in of the problems I have to do, the input has to be like this:
1) A number n is entered.
2) n number of strings are entered.

I tried using a 2D character array to do the above, but it doesn't work. I have to hit the return key after each letter of each string. Could anyone point me to a method where I can *declare* n number of strings, where the user inputs number n?

Thanks for your time. :)

Post the code you tried.You could probably use the same character array for all n number of strings because you didn't say there was a requirement to save all those strings.

This is the code I tried:

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	char a[n][15];      //size of each string should be 14
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<14; j++)
		cin >> a[i][j];
	}
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<14; j++)
		cout << a[i][j] << endl;
	}
	return 0;
}

I need to save the strings, yes.

line 9: that will not compile with most compilers because that feature is new to c99 standards and most compilers older than that. If you want to use charcter arrays then you will have to allocate them at runtime

int n;
char ** a;
cin >> n;
a = new char*[n];
for (int i=0; i<n; i++)
{
    a[i] = new char[15];
    cin.getline(a[i], 14);
}
commented: Nice code, I did not know this. =) +1

Thanks! =)

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.