Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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/3/arrays/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++_Arrays - Fatal编程技术网

C++中的并行数组

C++中的并行数组,c++,arrays,C++,Arrays,我有一个任务,我们需要两个平行数组,一个是城市名称列表,另一个是销售额。以下是问题的副本: 程序说明: 它需要编译美国各个城市的销售总额。具体来说,当程序运行时,将提示用户输入一个城市。如果城市正确,则系统将提示用户输入销售金额。如果该城市不在列表中,用户将收到一条错误消息,并且没有销售金额提示。如果输入了销售金额,它将累计到该城市的总额中。无论列表中是否存在城市,都会要求用户输入其他城市或退出。 一旦用户退出,应显示所有城市的城市名称和总数,每行一个。然后程序应该停止 只有8个城市可供选择。必

我有一个任务,我们需要两个平行数组,一个是城市名称列表,另一个是销售额。以下是问题的副本:

程序说明:

它需要编译美国各个城市的销售总额。具体来说,当程序运行时,将提示用户输入一个城市。如果城市正确,则系统将提示用户输入销售金额。如果该城市不在列表中,用户将收到一条错误消息,并且没有销售金额提示。如果输入了销售金额,它将累计到该城市的总额中。无论列表中是否存在城市,都会要求用户输入其他城市或退出。 一旦用户退出,应显示所有城市的城市名称和总数,每行一个。然后程序应该停止

只有8个城市可供选择。必须使用2个并行阵列,初始化如下:

City (String)  Sales (Integer)
-------------  ---------------
Atlanta              0
Buffalo              0
Chicago              0
Dallas               0
Houston              0
Honolulu             0
Miami                0
Reno                 0
所有输入保证为单字,后跟enter only。它可能与城市名称不匹配,但不会有空格。这使您的程序保持简单,因为它允许您避免使用getline,这将需要处理单词之间的空格。 销售数据在输入时保证良好

当我试图运行我的程序时,visual Studio发疯了,我拔出头发试图修复它。如果有人能帮我指出我做错了什么,我将不胜感激。这是我的程序的副本:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    //declare city and sales array
    string city[8] = {" "};
    int sales[8] = {0};

    //declare variables
    string cityName = " ";
    int cityTotal = 0;
    int salesAmt = 0;
    int i = 0;
    char another = ' ';

    //init city array
    city[0] = "Atlanta";
    city[1] = "Buffalo";
    city[2] = "Chicago";
    city[3] = "Dallas";
    city[4] = "Houston";
    city[5] = "Honololu";
    city[6] = "Miami";
    city[7] = "Reno";

    do
    {
        //input city name and if found input sales amount
        cout << "Enter a City: Atlanta, Buffalo, Chicago, Dallas, Houston, Honololu, Miami, or Reno: ";
        cin >> cityName;

        for(i = 0; i <= 8; i++)
        {
            if(cityName == city[i])
                {cout << "Enter sales amount: ";
                 cin >> salesAmt;
                 salesAmt += sales[i];}
            else
                {cout << "ERROR: CITY NOT AVAILIABLE" << endl;
                 cout << endl;}
            //end if
        }
        //end for loop

        //ask if another city
            cout << "Enter another city?: ";
            cin >> another;

    } //end do loop
    while(another == 'Y' || another == 'y');
    {
        for(i = 0; i <= 8; i++)
        {
            cout << "City: " << "           " << "Sales: " << endl;
            cout << city[i] << "          " << sales[i] << endl;
        }
        //end for loop
    } //end while loop

    system("pause");
    return 0;
} //end of main

这里一个明显的错误是您使用索引访问数组的方式,您不能让for循环达到8,因为数组的索引最多只有7。将for循环更改为:

for(i = 0; i < 8; i++)

误差为i=0;我的视觉工作室疯了这就是错误的全部描述?尽量更具体一些。std::string有定义良好的默认构造函数,可以用空字符串初始化它。您不需要初始化,您应该标记一个答案,先生。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
    //declare city and sales array
    string city[8] = {" "};
    int sales[8] = {0};

    //declare variables
    string cityName ="";
    int cityTotal = 0;
    int salesAmt = 0;
    int i = 0;
    char another = ' ';

    //init city array
    city[0] = "Atlanta";
    city[1] = "Buffalo";
    city[2] = "Chicago";
    city[3] = "Dallas";
    city[4] = "Houston";
    city[5] = "Honololu";
    city[6] = "Miami";
    city[7] = "Reno";

    do
    {
        //input city name and if found input sales amount
        cout << "Enter a City: Atlanta, Buffalo, Chicago, Dallas, Houston, Honololu, Miami, or Reno: ";
        cin >> cityName;

        for(i = 0; i < 8; i++)
    {
        if(cityName == city[i])
        {
            cout << "Enter sales amount: ";
             cin >> salesAmt;
             sales[i] += salesAmt;      
        } else if (i==7)
        {
            cout << "ERROR: CITY NOT AVAILIABLE" << endl;
        }//end if
    }//end for loop
        //ask if another city
            cout << "Enter another city?: ";
            cin >> another;

    } //end do loop
    while(another == 'Y' || another == 'y');
    {
        for(i = 0; i < 8; i++)
        {
            cout << "City: " << "           " << "Sales: " << endl;
            cout << city[i] << "          " << sales[i] << endl;
        }
        //end for loop
    } //end while loop

    system("pause");
    return 0;
} //end of main