我2个月前开始学习计算机科学。我需要帮助。
编译器给了我这个警告,我的程序没有给出单词数字。
Lab4Problem3.cpp:202:11:警告:多字符字符常量[-Wmultichar]
Lab4Problem3.cpp:202:26:警告:字符常量太长,类型[默认启用]
Lab4Problem3.cpp:在函数'int main()'中:
Lab4Problem3.cpp:27:16:警告:'std :: string word_number(std :: string,std :: string)'的地址总是计算为'true'[-Waddress]
Lab4Problem3.cpp:29:16:警告:'std :: string word_number(std :: string,std :: string)'的地址总是评为'true'[-Waddress]
这就是我所拥有的:
#include <iostream>
#include <string>
using namespace std;
string word_number(string units, string tens);
int num_entered;
int main ()
{
char answer;
do
{
cout << "Enter the number of bottles to throw on the wall: " << endl;
cin >> num_entered;
cout << endl;
if (num_entered < 99 && num_entered >= 0)
{
while (num_entered > 0)
{
cout << word_number;
cout << " bottles of beer on the wall,\n";
cout << word_number;
cout << " bottles of beer,\n"
<< "Take one down, pass it around.\n";
num_entered--;
}
if (num_entered == 0)
cout << "Zero bottles of beer on the wall" << endl;
}
else
cout << "Invalid number!" << endl;
cout << "Do you want to try it again? (Y/N)" << endl;
cin >> answer;
} while (answer =='Y' || answer=='y');
return 0;
}
string word_number(string units, string tens)
{
int tens_number, units_number;
string result;
if (num_entered < 20)
{
if(num_entered < 10)
{
units_number = num_entered;
switch (units_number)
{
case 1:
units =="One";
break;
case 2:
units =="Two";
break;
case 3:
units =="Three";
break;
case 4:
units =="Four";
break;
case 5:
units =="Five";
break;
case 6:
units =="Six";
break;
case 7:
units =="Seven";
break;
case 8:
units =="Eight";
break;
case 9:
units =="Nine";
break;
}
return(units);
}
else
units_number = num_entered%10;
switch(units_number)
{
case 0:
units =="Ten";
break;
case 1:
units =="Eleven";
break;
case 2:
units =="Twelve";
break;
case 3:
units =="Thirteen";
break;
case 4:
units =="Fourteen";
break;
case 5:
units =="Fifteen";
break;
case 6:
units =="Sixteen";
break;
case 7:
units =="Seventeen";
break;
case 8:
units =="Eighteen";
break;
case 9:
units =="Nineteen";
break;
}
return (units);
}
else
tens_number = num_entered/10;
units_number = num_entered%10;
switch (tens_number)
{
/*case 0:
tens == "Zero";
break;*/
case 1:
tens =="Ten";
break;
case 2:
tens =="Twenty";
break;
case 3:
tens =="Thirty";
break;
case 4:
tens =="Fourty";
break;
case 5:
tens =="Fifty";
break;
case 6:
tens =="Sixty";
break;
case 7:
tens =="Seventy";
break;
case 8:
tens =="Eighty";
break;
case 9:
tens =="Ninety";
break;
}
units_number = num_entered%10;
switch (units_number)
{
case 1:
units =="One";
break;
case 2:
units =="Two";
break;
case 3:
units =="Three";
break;
case 4:
units =="Four";
break;
case 5:
units =="Five";
break;
case 6:
units =="Six";
break;
case 7:
units =="Seven";
break;
case 8:
units =="Eight";
break;
case 9:
units =="Nine";
break;
}
result = 'tens' + "-" + 'units';
return result;
}
答案 0 :(得分:4)
似乎你在这里遇到语法问题:
std::string word;
if (word == "One") { // <-- this is comparison
...
}
word = "Two"; // <-- this is assignment
当你这样做时:
std::string result'
result = 'tens' + "-" + 'units';
'tens'
和'units'
是多字节字符而不是字符串。你应该做的是:
std::string result;
result = tens + "-" + units;
附注:代码缩进很重要。如果它更容易阅读,那么你就可以更容易地进行阅读。
答案 1 :(得分:2)
为什么不先修复警告?
Lab4Problem3.cpp:202:11:警告:多字符字符常量[-Wmultichar]
字符常量是单引号中的一个。它必须是一个单一的角色。这应该很容易修复,编译器告诉你它在哪一行。
Lab4Problem3.cpp:202:26:警告:字符常量太长,类型[默认启用]
这是由同样的问题引起的。始终先修复第一个诊断,他们也可以解决以后的问题。
Lab4Problem3.cpp:27:16:警告:'std :: string word_number(std :: string,std :: string)'的地址总是计算为'true'[-Waddress]
Lab4Problem3.cpp:29:16:警告:'std :: string word_number(std :: string,std :: string)'的地址总是评为'true'[-Waddress]
同样,编译器会告诉您问题所在的行。如果你试图调用函数word_number
,那么你不是这样做的。函数需要参数。如果你看看你写的是什么并考虑它可能如何运作,这也应该是显而易见的。
这不会使程序正常工作,因为word_number
功能不起作用,但如果你修正警告,你会发现部分问题。
答案 2 :(得分:1)
前两个错误来自这一行:
result = 'tens' + "-" + 'units';
我认为你的意思是连接参数变量,所以它应该是:
result = tens + "-" + units;
最后两个错误来自两行:
cout << word_number;
word_number
是一个函数,你应该调用它,而不是试图打印函数本身(只打印它的地址)。
cout << word_number("something", "something_else");
我无法弄清楚该函数的参数应该用于什么。似乎应该将num_entered
作为参数,而不是使用全局变量。
答案 3 :(得分:0)
units =="One"
应为units = "One"
)result = 'tens' + "-" + 'units'
应为result = string("tens") + "-" + "units"
答案 4 :(得分:0)
第一个警告涉及
result = 'tens' + "-" + 'units';
Python将'
用于字符串(或"
),而C ++将其用于单个字符。
将行更改为
result = std::string("tens") + "-" + "units";
注意:如果您希望流出变量tens
的值,则无法满足您的要求。
接下来的两个警告说你在这里使用了一个函数的地址:
cout << word_number;
您可能打算调用函数
cout << word_number(num_entered, tens);
我会将tens
的定义留作读者的练习,但要注意你可能试图在函数内设置数字:
tens =="Ten";
你可能意味着
tens = "Ten";
如果您希望函数更改tens
,您可以考虑将其更改为参考(对于单位也是如此):
string word_number(string & units, string & tens);