错误:来自' char'的转换无效来自' const char'

时间:2016-09-17 20:34:45

标签: c++ enums operator-overloading operator-keyword operands

/*This program 
1. requires user to enter their weight and planet name 
2.use enum
3. display error message if listed planet name isn't entered
*/

/*Weight Factors on Each Planet
Mercury = 0.4155
Venus = 0.8975
Earth = 1
Moon = 0.166
Mars = 0.3507
Jupiter = 2.5374
Saturn = 1.0677
Uranus = 0
Neptune = 1.1794
Pluto = 0.0899
*/

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

enum Planets{MERCURY, VENUS, EARTH, MOON , MARS, JUPITER, SATURN,URANUS, NEPTUNE, PLUTO};

//string array that declares same 10 planets
string planet[] = {"MERCURY", "VENUS", "EARTH", "MOON", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE", "PLUTO"};


//Function Gets the User's Weight
double getweight()
{
    double usrweight;

    cout<<"\n What is Your Weight on a Different Planet?";
    cout << "\n Enter Weight(lbs) Here:  ";
    cin >> usrweight;
    return usrweight;
}


//String Function
string planetlength(string s) 
{        
    for(int i = 0; i < s.size(); i++)              
     {
     s[i] = planetlength(s[i]);                           
     }

     return s;  

      /*1. argument variable 's' of type string is passed to function
       2. size() function checks the length of 's' 
       */
} 


//Verifies User selection is valid planet
int go2planet(string s)                   
{     
int found = -1;     

for( int i = 0; i < 10; i++)    //cycle thru 10 planet names 
{          
if (s == planet[i])          //if s matches one of the 10 planet names in the array, return planet name as value                    
found = i;
}     
return found; 
} 



//Function Selects Planet & Does Arithmetic
int main()
{
int journey;
int i;

cout<<"\n Take a trip to...";

for (i = 0; i < 10; i++)
{
cout<<"-------Planet #--------"<< i+1 << planet[i];
}

string decision = planetlength(decision);

cout<<"\n Type in Desired Planet: ";
cin >> decision;

int destination = go2planet(decision);    

double conversion;    

double myweight = getweight();

    switch (journey)   //switch can anly accept integer values to execute in C++
    {
    case MERCURY: 

    conversion = (myweight)*(0.4155);
    cout<<"\n On "<< planet[0] <<" You Weigh: "<< conversion <<" lbs.";
        break;


    case VENUS: 

    conversion = (myweight)*0.8975;
        cout<<"\n On"<< planet[1] <<"You Weigh:  "<< conversion << " lbs";
        break;


    case EARTH: 

    conversion = (myweight)*1.00;
        cout<<"\n On"<< planet[2] <<"You Weigh:  "<< conversion <<" lbs";
        break;


    case MARS: 

    conversion = (myweight)*0.3507;
        cout<<"\n On"<< planet[3] <<"You Weigh:  "<< conversion <<" lbs";
        break;


    case MOON: 

    conversion = (myweight)*0.166;
        cout<<"\n On"<< planet[4] <<"You Weigh:  "<< conversion <<" lbs";
        break;


    case JUPITER: 

    conversion = (myweight)*2.5374;
        cout<<"\n On"<< planet[5] <<"You Weigh:  "<< conversion <<"  lbs";
        break;


    case SATURN: 

    conversion = (myweight)*1.0677;
        cout<<"\n On"<< planet[6] <<"You Weigh:  "<< conversion <<" lbs";
        break;

    case URANUS: 

    conversion = (myweight) * 0;
        cout<<"\n On" << planet[7] <<"You Weigh:  "<< conversion <<" lbs";
        break;


    case NEPTUNE: 

    conversion = (myweight)*1.1794;
        cout<<"\n On" << planet[8] <<"You Weigh:  "<< conversion <<" lbs";
        break;


    case PLUTO: 
    conversion = (myweight)*0.0899;
        cout<<"\n On"<< planet[9] <<"You Weigh:  "<< conversion <<" lbs";
        break;

    default: cout << "\n Not a Valid Planet. Re-Enter Name";
    }

    return 0;
    }

我使用了一些编程类中的代码,现在我修改了它以适合我的程序。它是planetlength()函数中的一行。 线:s [i] = planetlength(s [i]);我收到的错误现在表示“无法从char转换为const-char&#39;”。我使用字符串数组,我的函数是string类型,我的返回值是一个字符串。这个隐藏的字符在哪里?

1 个答案:

答案 0 :(得分:0)

用于将>>的输入读入变量的std::cin不是C ++语言的一些内在特征,而是运算符(即某种函数)可以处理来自std::cin的输入并将其放入指定的变量中。此类运算符是针对标准类型(例如intfloatstd::string声明和实现的(仅举几例)。 但是,没有像您的行星那样为自定义类型声明和实现此类运算符。这就是为什么你必须为Planets枚举类型实现自己的operator>>以使这段代码按预期工作。