Cannot get a string from user input stream in first loop of entry

Hello,

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.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

const int 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, Population, Name_of_Mayor;

    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);
            City_Data[i][0] = City_Name;
            outFile << City_Data[i][0] << setw(20);
            cout << endl;

            cout << "Enter the population of the city" <<endl;
            getline(cin,Population);
            City_Data[i][1] = Population;
            outFile << City_Data[i][0] << setw(20);
            cout << endl;

            cout << "Enter the Name of the Mayor" <<endl;
            getline(cin,Name_of_Mayor);
            City_Data[i][2] = Name_of_Mayor;
            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;

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;

}
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.
Dear Last Chance,

I did that and it worked perfectly! But I even tried setting up a seperate function to set up the no of cities.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

const int 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.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

const int columns = 3;
extern int 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.
Last edited on
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.



Topic archived. No new replies allowed.