有没有一种方法可以将用户输入分配给数组?

时间:2019-11-14 00:03:50

标签: c++ arrays string cin

想知道cin是否有办法识别数组名称并将其复制到新变量中。这样,当用户输入类似阿富汗的答案时,将数组“阿富汗”的值复制到数组J中。

    #include <iostream>
    #include <cmath>    

    using namespace std;
    #define PI 3.14159265

    float Afghanistan[2] = { 34.51666667,69.183333 };
    float Albania[2] = { 41.31666667,19.816667 };

    float Countries[2][2] =
    {
        { Afghanistan[0], Afghanistan[1]},
        {Albania[0], Albania[1]}    
    };

    float x;
    float z[2];
    float y;
    float a;
    float b;
    float distances;
    string CountryChoice;
    float distanceBetweenTwoPoints(float x, float y, float a, float b);

    int main() {

        float j[2];
        cout << " Select Country:" << endl;
        cin >> CountryChoice;    

        std::copy(std::begin(CountryChoice), std::end(CountryChoice), std::begin(j)); // idea being that string is entered by user, and if it corresponds with a country , then J is filled with the value of the selected countries array

        cout << " CountryChoice=  " << CountryChoice << endl;
        cout << " J int x =" << j[0] << "           " << j[1] << endl;

        distances = distanceBetweenTwoPoints(j[0], j[1], Albania[0], Albania[1]);
        cout << "The answer is : " << distances << "KM" << endl;
    }

    float distanceBetweenTwoPoints(float x, float y, float a, float b) {
        float diflat = x - y;
        float diflong = a - b;
        float f, c, d;
        float R = 6371;

        f = pow((sin(diflat / 2)), 2) + cos(x) * cos(y) * pow((sin(diflong / 2)), 2);
        c = 2 * atan2(sqrt(f), sqrt(1 - f));
        d = R * c;

        return d;
    }

0 个答案:

没有答案