C++ 我的基本计算器应用程序中的一个变量是';行不通 #包括 使用名称空间std; int main() { cout>用户名; 库特数; cout>数字2; 双和; 总和=数字一+数字二; cout
在这一行:C++ 我的基本计算器应用程序中的一个变量是';行不通 #包括 使用名称空间std; int main() { cout>用户名; 库特数; cout>数字2; 双和; 总和=数字一+数字二; cout,c++,C++,在这一行: #include <iostream> using namespace std; int main() { cout << "Hello user, please enter your name: "; string userName; cin >> userName; cout << "Hello " + userName + ", welcome to the addition ca
#include <iostream>
using namespace std;
int main()
{
cout << "Hello user, please enter your name: ";
string userName;
cin >> userName;
cout << "Hello " + userName + ", welcome to the addition calculator.\n Please keep in mind that this calculator only supports two numbers.";
cout << "Please enter a number for the process for addition: ";
double numberOne;
cin >> numberOne;
cout << "Please enter a second number for the process for addition: ";
double numberTwo;
cin >> numberTwo;
double sum;
sum = numberOne + numberTwo;
cout << userName + ", thank you for using the addition calculator, the sum of your answer is " + sum;
return 0;
}
或者像这样使用std::to_string
:
cout << userName
<< ", thank you for using the addition calculator, the sum of your answer is "
<< sum;
// ^^^^^^ chaining is fine
coutcin>>userName;
确保只询问名字,否则如果有人在名字和姓氏之间键入空格,那么接下来询问的double中会有不好的数据。我认为将字符串和变量链接在一起会更有效,而且,你说我可以在字符串上加一个double是什么意思,这是一个separate变量对吗?也谢谢你的回答,我很感激。但是类型不匹配,或者更确切地说,没有可以添加双精度和字符串的+
(至少如果你想进行串联)。
cout << userName
<< ", thank you for using the addition calculator, the sum of your answer is "
<< sum;
// ^^^^^^ chaining is fine
cout << userName
+ ", thank you for using the addition calculator, the sum of your answer is "
+ std::to_string(sum);
// ^^^^^^^^^^^^^^^^^^^ converting to a string and concatenating is fine