2d array

I'm trying to output the cities the user inputted with commas and space. for example New York, New Jersey, etc. I understand the rows and columns of a 2d array. I'm not sure what I'm doing wrong. It doesn't output all the cities I inputted and I don't know how I would insert the comma and space unless I use a loop to output the information, but there must be a easier way
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
28
#include <iostream>
using namespace std;

int main()
{
    int input;

    cout << "How many cities do you want to enter?" << endl;
    cin >> input;
    //declare a 2d array with the rows = 0 and columns set by user
    char cities[100][input];

    cout << "Please enter the names of the " << input << " cities:" << endl;

    //loop to take user input and place into array
    for (int i=0; i<input; i++)
    {
        for (int j=0; j<=input; j++)
        {
            cin >> cities[i][j];
        }
    }

    cout << "You've entered " << input << " cities: " << cities[0] << endl;

    return 0;

}
Yes, you need a loop. When you will progress in your study of C++ you'll use more sophisticated mechanism. For the moment a good old loop is all you need :)
Ciao
ok im lost on how to configure the loop output

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
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main()
{
    int input;

    cout << "How many cities do you want to enter?" << endl;
    cin >> input;
    //declare a 2d array with the rows = 0 and columns set by user
    char cities[100][input];

    cout << "Please enter the names of the " << input << " cities:" << endl;

    //loop to take user input and place into array
    for (int i=0; i<input; i++)
    {
        for (int j=0; j<=input; j++)
        {
            cin >> cities[i][j];
        }
    }

    cout << "You've entered " << input << " cities: ";

    for (int k=0; k<input; k++)
    {
        for (int l=0; l<=input; l++)
        {
            cout << cities[0] << ", ";
        }
    }

    return 0;

}
anyone?
line 11 will not work. you can only create arrays with a fixed size. defining the size during runtime just gives you errors.
Last edited on
If I declare the array with a fixed size, do I need to fulfill all the fixed space? Also, do I just put high fixed numbers to account for long user inputs. And also how will I stop the loop, if the fixed numbers are high.

I was thinking I could set the rows of my loop by user input. and the column will be a fixed number. But how would I close the loop for the columns
Last edited on
have you learned string and vector yet?
yes but i have to use 2d array as part of my assignment
Yes, using std::string and std::vector makes the task much easier:

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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>

using namespace std;


int main()
{
	int city_count;

	cout << "How many cities do you want to enter? ";
	cin >> city_count;
	//declare a 2d array with the rows = 0 and columns set by user
	vector<string> cities;

	cout << "Please enter the names of the " << city_count << " cities:" << endl;

	//loop to take user input and place into array
	for (int i=0; i<city_count; i++)
	{
		cout << endl << i+1 << " : ";
		string city("");
		cin.sync();
		getline(cin, city);
		cities.push_back(city);
	}

	cout << endl << "You've entered " << city_count << " cities: ";

	for (int k=0; k<city_count; k++)
	{
		cout << ", " << cities.at(k);
	}

	return 0;
}
I have to use 2d array as part of my assignment.

Maybe I could use .length or .size to get the length of the cities from user input and run the loop based on the length??
Last edited on
just use a 100 x 100 char array then

you wont enter 100 cities and cities wont have a name with more then 100 characters, so this will work.

use a variable to count the entered cities, and use the variable as index of your char array, it will tell you the first free slot in your array. just make sure you increase it each time you add a city.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;


int main()
{
	int city_count;

	cout << "How many cities do you want to enter? ";
	cin >> city_count;
	
	// declare array of char array's:
	char** cities = new char*[city_count];
	for (int n = 0; n < city_count; ++n)
		cities[n] = NULL;

	cout << "Please enter the names of the " << city_count << " cities:" << endl;

	//loop to take user input and place into array
	for (int n=0; n<city_count; n++)
	{
		cout << endl << n+1 << " : ";
		char buffer[255] = {0};
		cin.sync();
		cin.getline(buffer, 255);

		size_t len = strlen(buffer);
		cities[n] = new char[len+1];
		strcpy_s(cities[n], len+1, buffer);
	}

	cout << endl << "You've entered " << city_count << " cities: ";

	for (int n=0; n<city_count; n++)
	{
		if (cities[n])
		{
			cout << cities[n] << ", ";
			delete[] cities[n];
		}
	}

	delete[] cities;

	return 0;
}
ajh32, there's errors I can't resolve and not sure it's 2d array.

I tried to follow what Darkmaster said but still having difficulties.

I've managed to be able to output but not perfectly. If I run my code and put 4 cities and input test1, test2, test3, test4. It will appear but with no spaces. If I choose to display 5 cities, I would have to input 6 cities and test1 thru test6 appears. Now if I use real city names, the last city that's inputted gets cut off.
My inexperience and this 2d stuff is a real pita.
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
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
using namespace std;

int main()
{
    int input, i, j;

    cout << "How many cities do you want to enter?" << endl;
    cin >> input;
    //declare a 2d array with the rows set by user and columns set to 50
    char cities[100][100];
    string len;

    cout << "Please enter the names of the " << input << " cities:" << endl;

    //loop to take user input and place into array
    for (i=0; i<=input; i++)
    {
        for (j=0; j<input; j++)
        {
            cin >> cities[i][j];
            len = cities[i][j];
        }
    }

    cout << "You've entered " << input << " cities: ";
    for (i=0; i<=input; i++)
    {
        for (j=0; j<input; j++)
        {
            cout << cities[i][j];
        }
    }
    
    cout << endl;
    system("pause");
    return 0;
}
That's why I used:

1
2
cin.sync();
cin.getline...


see my code above
Topic archived. No new replies allowed.