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
#include <iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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;
}
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
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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;
}
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.
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.
#include <iostream>
usingnamespace 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;
}