C++ '的赋值类型不兼容;常量字符[2]到字符[1]';
我写了这段代码(顺便说一句,我刚开始编码),在13:17有一个错误: 错误:将“常量字符[2]”赋值给“字符[1]”时,类型不兼容 该错误也可以在27:25中找到 这不是我如何修复它的问题,而是解释为什么使用C++ '的赋值类型不兼容;常量字符[2]到字符[1]';,c++,C++,我写了这段代码(顺便说一句,我刚开始编码),在13:17有一个错误: 错误:将“常量字符[2]”赋值给“字符[1]”时,类型不兼容 该错误也可以在27:25中找到 这不是我如何修复它的问题,而是解释为什么使用if(opinion=“y”)不起作用,因为它是一个字符。我尝试过使用cin.getline(),但没有任何结果(我还没有学会这一点(我确实使用了#incude,即使字符串包含在std库中) 有什么想法吗 #include <iostream> #include <stri
if(opinion=“y”)
不起作用,因为它是一个字符。我尝试过使用cin.getline()
,但没有任何结果(我还没有学会这一点(我确实使用了#incude
,即使字符串包含在std
库中)
有什么想法吗
#include <iostream>
#include <string>
using namespace std;
int main(){
int mycarrots=10; //Initiates number of carrots that I have (mycarrots)
int yourcarrots=0; //Inititate the numbe of carrots the user has
int wantcarrots=0; //initiates the number of carrots user wants
int wantcarrots2=0; //initiates how many more carrots user wants apart from the ones that I can give him
char opinion1[1], opinion2[1]; //initiates opinion whether user want carrots. Opionion2 Initiates opinion whether user has enough money to buy more carrots
int ymoney=0; //initiates how much money the user has
cout<<"I have some carrots I want to give away, would you like some? (y/n)"<<endl; //initiates convo, ask user whether he wants carrts
cin>>opinion1; //input of opinion (y/n)
if (opinion1="n"){ //if the opinion is no, execute "Have a good day"
cout<<"Have a good day!"<<endl;
}
else { //otherwise, resume convo
cout<<"How many do you want?"<<endl;
cin>>wantcarrots;
if (wantcarrots>mycarrots){
cout<<"I don't have that many carrots, you'll have to get some from the store."<<endl;
cout<<"They're $1.5 each, so you'll have to pay "<<(wantcarrots-mycarrots)*1.5<<" dollars";
cout<<"do you have enough money for that? (y/n)"<<endl;
cin.getline(opinion2,1);
wantcarrots2=wantcarrots-mycarrots;
if (opinion2="y"){
cout<<"I can give you "<<mycarrots<<" carrots, but you'll have to get the other"<<mycarrots-wantcarrots<<" from the store."<<endl;
cout<<"Now off you go to the store then.";
}
else {
cout<<"how much money do you have?"<<endl;
cin>>ymoney;
if (ymoney>=(wantcarrots2*1.5)){
cout<<"Off you go to the store."<<endl;
}
else if (ymoney<(wantcarrots2*1.5)){
cout<<"You'll have to settle for "<<ymoney/1.5<<" carrots."<<endl;
}
}}
else{
cout<<"fatal errors. i am not prgrammed to do this"<<endl;}
}
else {
cout<<"Here are your "<<wantcarrots<<" carrots"<<endl;
cout<<"now you have "<<yourcarrots<<" carrots"<<endl;}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int mycarrots=10;//启动我拥有的胡萝卜数(mycarrots)
int yourcarrots=0;//初始化用户拥有的胡萝卜数量
int wantcarrots=0;//初始化用户想要的胡萝卜数
int wantcarrots2=0;//初始化用户除了我可以给他的胡萝卜之外还需要多少胡萝卜
char opinion1[1],opinion2[1];//对用户是否想要胡萝卜发表意见。opinion2对用户是否有足够的钱购买更多胡萝卜发表意见
int ymoney=0;//初始化用户有多少钱
无法if(opinion1=“y”)
将不起作用,因为未声明意见,而if(opinion1=“y”)
将不起作用,因为无法分配给数组
要检查iss读取的内容是否为y
,请尝试if(*opinion1==“y”)
要点是
- 做比较,而不是分配
- 在比较之前取消引用从数组转换的指针。您也可以使用
opinion1[0]
- 使用字符文字而不是字符串文字
更新:正如@SamVarshavchik所建议的,此代码将导致越界访问
如果您不更改opinion1
的类型,请使用cin.get(*选项1);
而不是cin>>opinion1;
除了其他答案中指出的代码问题之外,我还将指出,如果输入不是空字符串,这将导致内存损坏和未定义的行为
char opinion1[1]
一个字符数组只能容纳一个空字符串
cin>>opinion1;
…在此处输入一个字符后,这将使数组缓冲区溢出,损坏堆栈,并导致未定义的行为。首先,opinion2=“y”
是赋值,而不是比较。其次,我强烈建议使用std::string而不是字符指针。opinion1
和“y”都不是
是char
。
char opinion1[1]