Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 表达式:字符串下标超出范围_C++ - Fatal编程技术网

C++ 表达式:字符串下标超出范围

C++ 表达式:字符串下标超出范围,c++,C++,每当我运行代码时,我都会收到一个错误消息,说我的字符串下标超出范围(错误消息是标题)。我希望它是从使用“计数器”来计算总城市和平均人口。我该如何着手解决这个问题?我尝试过其他的方法来计算它,但没有一种有效 void cities( istream& in, ostream& out ) { ifstream ("cities.txt"); string country, city, city2, state, lat, longi; int pop;

每当我运行代码时,我都会收到一个错误消息,说我的字符串下标超出范围(错误消息是标题)。我希望它是从使用“计数器”来计算总城市和平均人口。我该如何着手解决这个问题?我尝试过其他的方法来计算它,但没有一种有效

void cities( istream& in, ostream& out )
{
    ifstream ("cities.txt");
    string country, city, city2, state, lat, longi;
    int pop;
    int currentPop = 0;
    int smallestPop = 0;
    int largestPop = 0;
    int counter = 0;
    int sum = 0;
    int i = 0;
    int average = 0;
    string largestCity;
    string smallestCity;
    string population;

    readLineOfData(in, country, city, city2, state, pop, lat, longi);
    while(!in.fail())
    {
        counter++;
        output( out, country, city, city2, state, pop, lat, longi );


        readLineOfData(in, country, city, city2, state, pop, lat, longi);

        population[counter] = pop;

        if (pop < smallestPop || smallestPop == 0)
        {
            smallestPop = pop;
            smallestCity = city2;
        }

        if (pop > largestPop || largestPop == 0)
        {
            largestPop = pop;
            largestCity = city2;
        }

        for (int i = 0; i<counter; i++)
        {
            sum += population[i];
            average = sum/counter;
        }
    }

        out << "Smallest City: " << smallestCity << endl;
        out << "Population: " << smallestPop << endl;
        out << endl;
        out << "Largest City: " << largestCity << endl;
        out << "Largest Population: " << largestPop << endl;
        out << endl;
        out << "Total Cities: " << i << endl;
        out << "Average Population: " << average << endl;
    return;
}

void readLineOfData( istream& in, string &country,  string &city, string &city2, 
    string &state, int &pop, string &lat, string &longi)
{
    getline( in, country, ',');
    getline( in, city, ',');
    getline( in, city2, ',');
    getline( in, state, ',');
    in >> pop;
    in.ignore( 200, ',' );
    getline( in, lat, ',');
    getline( in, longi, '\n' );

}

void output( ostream& out, string country, string city, string city2,
    string state, int pop, string lat, string longi )
{
}
void城市(istream&in,ostream&out)
{
ifstream(“cities.txt”);
字符串国家、城市、城市2、州、拉特、朗吉;
int-pop;
int currentPop=0;
int smallestPop=0;
int largestPop=0;
int计数器=0;
整数和=0;
int i=0;
整数平均=0;
弦最大度;
弦小城市;
串种群;
readLineOfData(in、country、city、city2、state、pop、lat、longi);
而(!in.fail())
{
计数器++;
输出(输出、国家、城市、城市2、州、pop、lat、longi);
readLineOfData(in、country、city、city2、state、pop、lat、longi);
人口[计数器]=流行性感冒;
if(poplargestPop | | largestPop==0)
{
最大波普=波普;
最大城市=城市2;
}
对于(inti=0;i声明

string population;
表示
population
是一个字符代码序列,但您将其视为一个数字序列:

population[counter] = pop;
而且在这一点上,它的大小是0,所以索引只会出错或给出未定义的行为

相反,将
population
声明为
std::vector
,并使用

population.push_back( pop );

您正在获取的错误消息?来自titleis填充的是单个字符串或字符串数组在代码中您在哪里获取此错误?pop是一个整数,填充是每个pop的数组。我使用字符串是因为我必须定义数组中有多少个整数,而我将有一个未知的数量。