I have written the following code below. The outcome I would like to achieve is to be able to get data from a user input stream about city information and to output this stream to create a text file.
Whilst I am able to successfully input and output to a text file, when my program prompts the user for a city name, it skips it and just goes for population and name of mayor.
Actually, I don't know! However, I have some qualms about what you are doing in lines 87 and 89. The size of array CITY_DATA is NOT known at compile time: I think you should be dynamically allocating it, using new. (Alternatively, use a container from the Standard Template Library, but that may be a bit advanced and/or overkill.)
When I commented out your line 87 and set a number of cities explicitly in line 82 then the code seemed to accept data OK. In this case the size of array CITY_DATA would have been known at compile time.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>
usingnamespace std;
constint columns = 3;
void City_Data_Inputtofile(string[][columns], int);
//void City_Data_View(string[][columns], int);
void City_Data_Inputtofile (string City_Data[][columns], int NO_OF_CITIES)
{
ofstream outFile;
string City_Name[NO_OF_CITIES], Population[NO_OF_CITIES], Name_of_Mayor[NO_OF_CITIES];
outFile.open("City_Information.txt");
if (outFile.fail())
{
cout << "File failed to open"<<endl;
}
cout << "City Name" << setw(20) << "Population" << setw(20) << "Name of Mayor" << endl;
outFile << "City Name" << setw(20) << "Population" << setw(20) << "Name of Mayor" << endl;
for (int i = 0; i < NO_OF_CITIES; i++)
{
cout << "Enter the name of the city" << endl;
getline(cin,City_Name[i]);
City_Data[i][0] = City_Name[i];
outFile << City_Data[i][0] << setw(20);
cout << endl;
cout << "Enter the population of the city" <<endl;
getline(cin,Population[i]);
City_Data[i][1] = Population[i];
outFile << City_Data[i][0] << setw(20);
cout << endl;
cout << "Enter the Name of the Mayor" <<endl;
getline(cin,Name_of_Mayor[i]);
City_Data[i][2] = Name_of_Mayor[i];
outFile << City_Data[i][2] << setw(20);
outFile << endl;
cout << endl;
}
for (int i = 0; i < NO_OF_CITIES; i++)
{
cout << "Name of City"<< endl;
cout << City_Data[i][0] << endl;
cout << "Population" <<endl;
cout << City_Data[i][1] << endl;
cout << "Name of Mayor" <<endl;
cout << City_Data[i][2] <<endl;
}
outFile.close();
return;
}
/*void City_Data_Output(string City_Data[][columns], int NO_OF_CITIES)
{
}*/
int main()
{
int no_of_cities = 2;
cout << "This program will make a file to story city information" << endl;
cout << "Enter the number of cities that you would like to store in this file" <<endl;
//cin >> no_of_cities;
string CITY_DATA [no_of_cities][columns];
City_Data_Inputtofile(CITY_DATA, no_of_cities);
//City_Data_Output(CITY_DATA, no_of_cities);
return 0;
}
I decided to break it down and make a function where the you can set the number of cities before the input to file function was called. I also set the no_of_cities variable as a global one.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>
usingnamespace std;
constint columns = 3;
externint no_of_cities = 0;
void City_Data_Inputtofile(string[][columns]);
int set_no_of_cities (int);
//void City_Data_View(string[][columns], int);
int set_no_of_cities(int CITY_NUMBERS)
{
cout << "Enter the number of cities that you would like to store in this file" <<endl;
cin >> no_of_cities;
return 0;
}
void City_Data_Inputtofile (string City_Data[][columns])
{
ofstream outFile;
string City_Name[no_of_cities], Population[no_of_cities], Name_of_Mayor[no_of_cities];
outFile.open("City_Information.txt");
if (outFile.fail())
{
cout << "File failed to open"<<endl;
}
cout << "City Name" << setw(20) << "Population" << setw(20) << "Name of Mayor" << endl;
outFile << "City Name" << setw(20) << "Population" << setw(20) << "Name of Mayor" << endl;
for (int i = 0; i < no_of_cities; i++)
{
cout << "Enter the name of the city" << endl;
getline(cin,City_Name[i]);
City_Data[i][0] = City_Name[i];
outFile << City_Data[i][0] << setw(20);
cout << endl;
cout << "Enter the population of the city" <<endl;
getline(cin,Population[i]);
City_Data[i][1] = Population[i];
outFile << City_Data[i][1] << setw(20);
cout << endl;
cout << "Enter the Name of the Mayor" <<endl;
getline(cin,Name_of_Mayor[i]);
City_Data[i][2] = Name_of_Mayor[i];
outFile << City_Data[i][2] << setw(20);
outFile << endl;
cout << endl;
}
cout << "Here is the data you entered" << endl;
for (int i = 0; i < no_of_cities; i++)
{
cout << "Name of City"<< endl;
cout << City_Data[i][0] << endl;
cout << "Population" <<endl;
cout << City_Data[i][1] << endl;
cout << "Name of Mayor" <<endl;
cout << City_Data[i][2] <<endl;
}
outFile.close();
return;
}
/*void City_Data_Output(string City_Data[][columns], int NO_OF_CITIES)
{
}*/
int main()
{
cout << "This program will make a file to story city information" << endl;
string CITY_DATA [no_of_cities][columns];
set_no_of_cities(no_of_cities);
City_Data_Inputtofile(CITY_DATA);
//City_Data_Output(CITY_DATA, no_of_cities);
return 0;
}
I get the following with the program refusing to work :(
This program will make a file to story city information
Enter the number of cities that you would like to store in this file
2
City Name Population Name of Mayor
Enter the name of the city
Process returned -1073741819 (0xC0000005) execution time : 3.562 s
Press any key to continue.
The number of cities, which you are using to set the size of your array, is NOT known at compile time - however you do it you are inputting it during RUN time (in line 97). Array CITY_DATA needs to be dynamically allocated.