C++错误:警告:多字符字符常量/a用于用法

C++ error: warning: multi-character character constant/atof usage

本文关键字:用于 字符常量 用法 字符 错误 警告 C++      更新时间:2023-10-16

C++新手。有人能向我解释一下我收到的错误信息是什么意思吗?或者我为什么会收到它们?非常感谢。

#include <iostream>
#include <string>                                                                                                                            
int main () {
   std::string input;
   double f, k;
   /* edit: codes to go inside the do while loop */                                                                                                                                   
   std::cout << "nEnter Fahrenheit temperature or 'exit' to end program: "; 
   std::getline(std::cin, input);
   do {
     f = std::atof(input.c_str());  //convert string input to double
     k = (f + 459.67) * (5/9);
     std::cout << "Entered Fahrenheit temperature is: " << f << std::endl;                                                                         
     std::cout << "Temperature in Kelvin is " << k << std::endl << std::endl;
   }while(input != 'exit');  //program runs until input is 'exit'
   return 0;
}
Errors:
t.cc:21:19: warning: multi-character character constant
t.cc: In function `int main()':
t.cc:21: error: no match for 'operator!=' in 'input != 1702390132'

编辑:感谢大家对while(input!='exit')的建议。有人能检查一下我在计算部分是否正确使用了atof吗?我对我所拥有的没有任何计算。如果输入45,则得到k=0或250。

不应该使用类型为int和实现定义值的多字符文字,而应该在该语句中使用字符串文字

}while(input != 'exit');

那一定有

}while(input != "exit");

使用:

while(input != "exit"); // Notice double quotes

'exit'给出"警告:多字符字符常量"

您需要使用" " 将其与字符串文字进行比较

'a'是一个字符常量。CCD_ 5也是一个字符常数;形式上,它是一个多字符的字符常量。这里要使用的是"exit",它是一个字符串文字。