C++ storing an element of a string array into a va

2019-08-31 07:36发布

问题:

I am currently coding in C++ and am fairly new to it. I am running into issues with converting an element of an array of strings into a variable. The cout statement dcity[selection-1] works as intended. However, I am unable to store dcity[selection-1] into a variable named departureLocation. Visual Studios gives me the error that there is no suitable conversion function from a string to a char. Does anyone have advice on how to properly store this? Thanks!

int main()
{

int selection = 0;
char departureLocation;


std::string dcity[] = { "Seattle Detroit Seattle Chicago Houston Seattle" };
std::cout << "Please choose a number from the list";
std::cin >> selection;
std::cout << dcity[selection-1];


departureLocation=dcity[selection-1]

};

回答1:

Since C++ is a strongly-typed language, it doesn't like type mismatches. You've declare your variable in the following way:

char departureLocation;

This means departureLocation is a variable of type char, or a single character. So 'C' can go into departureLocation but "Chicago" cannot, as it is more that one character.

You've also declared your array like so:

std::string dcity[] =

Here, you have defined the type of the array as std::string. So elements of the array are strings, not chars.

The short answer is that you need to change the type of departureLocation to a string when you declare it, instead of a char. Something like:

std::string departureLocation;

I also didn't see any include statements in your code above. For C++ to recognize the string class, you'll need to make sure the following is at the top of your code somewhere:

#include <string>



回答2:

dcity is an array of std::strings. departureLocation is a char which can only hold one character. To store (copy) an element of the array dcity, departureLocation must be of type std::string (or any other type that can be constructed fron a std::string):

std::string dcity[] = { "Seattle Detroit Seattle Chicago Houston Seattle" };
std::string departureLocation = dcity[0];

Be aware thet dcity is an array consisting of only one element. Maybe you want an array where each city is a seperate array element:

std::string dcity[] = { "Seattle", "Detroit", "Seattle",
                        "Chicago", "Houston", "Seattle" };
std::string departureLocation = dcity[2];
std::cin >> selection;
std::cout << dcity[selection-1];

Also you should do some error checking before using user input as an array index:

if (!(std::cin >> selection) || selection < 1 || selection > sizeof(dcity) / sizeof(*dcity)) {
    std::cerr << "Input error!\n";
    return EXIT_FAILURE;
}
std::string departureLocation = dcity[selection - 1];

If you don't need independent copies of your array elements you could also use a reference to std::string:

std::string &departureLocation = dcity[selection - 1];

Be aware that changes to the string using departureLocation will now reflect on the array element departureLocation references. If you don't want to allow changes, use a const reference:

std::string const &departureLocation = dcity[selection - 1];


回答3:

Your code needs little modification. 1. An array is initialized by giving its component values separated by a comma. Note each individual element should be of the same type as that of the declared array. 2. The C++ is very strict with the type matching. We can assign objects only of the same type. In the above code, a string object is being assigned to a character variable which is against the rule. A C string is an array of characters terminated by null character (\0). In C++, string is more advanced and it is an object which has very useful member function. For e.g. to get length of the string, just say obj.length(). We need to have character array since in string there can be more than one char element. We need to have c-string of the string object to get into an array of characters. Please see below the modified code.

#include <iostream>
#include <cstring>
#include <string>
using namespace std;    //std::  is not required in front of cout 
int main()
{
    int selection = 0;

    string dcity[] = { "Seattle", "Detroit" ,"Seattle", "Chicago", "Houston","Seattle" };
    cout << "Please choose a number from the list";
    cin >> selection;
    cout << dcity[selection-1] << endl;

    char departureLocation[dcity[selection-1].length()+1];
    strcpy(departureLocation,dcity[selection-1].c_str());
    cout << departureLocation;
   // cout << *c;
    return 0;
}


回答4:

You could to store the selection in a std::string, because char can only store a single character. Be also careful of where you access your array to not go out of bounds (what happens if somebody enters 1000 etc.) and that non-programmer users index lists starting with 1. You can try this code:

#include <iostream>
#include <string>

int main()
{
    const auto Cities = { "Seattle", "Detroit", "Seattle", "Chicago", "Houston", "Seattle" };
    std::cout << "Please choose a number from the list (1-" << Cities.size() << "): ";

    int selection = 0;
    std::cin >> selection;
    if (selection < 1 || selection > Cities.size()) {
        std::cout << "\ninvalid selection!\n";
        return -1;
    }

    const std::string departureLocation = *(Cities.begin() + selection - 1);
    std::cout << "\nyou selected: " << departureLocation;

    return 0;
};


回答5:

First of all the compiler thinks that there is only one element in the array because arrays in c++ should have commas between each element and each string must be enclosed inside double quotes.The second error is that you are trying to save data of type string into a variable having data type char.

Code

#include<string>
#include<iostream>

using namespace std;
int main()
{

int selection=0;
string depaturelocation;
string dcity[]={"Seattle","Detroit","Seattle","Chicago","Houstan","Seattle"};
int size=sizeof(dcity)/sizeof(dcity[0]);
for(int i=0;i<size;i++)
{
   cout<<i+1<<" : "<<dcity[i]<<endl;
}
cout<<"please enter the destination no:";
cin>>selection;

for(int i=0;i<size;i++)
{
    if(selection-1==i)
    {
        depaturelocation=dcity[i];
    }
}
cout<<"destination:"<<depaturelocation;
return 0;
}