我该如何解决这个问题?(99瓶啤酒)
How do I fix this? (99 bottles of beer)
我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;
}
似乎您在这里的语法方面遇到了问题:
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;
旁注:代码缩进很重要。如果它更容易阅读,你就会更容易处理它。
你为什么不从修复警告开始呢?
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
功能不起作用,但是如果您修复警告,您会发现部分问题。
前两个错误来自这一行:
result = 'tens' + "-" + 'units';
我认为您的意思是连接参数变量,因此应该是:
result = tens + "-" + units;
最后两个错误来自两行:
cout << word_number;
word_number
是一个函数,你应该调用它,而不是尝试打印函数本身(它只是打印它的地址(。
cout << word_number("something", "something_else");
我无法弄清楚这个函数的参数应该用于什么。似乎它应该将num_entered
作为参数,而不是使用全局变量。
- 赋值运算符是单个"="(
units =="One"
应units = "One"
( - 字符串括在双引号 '"' (
result = 'tens' + "-" + 'units'
应result = string("tens") + "-" + "units"
第一个警告涉及
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);
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 我该如何解决这个问题?(99瓶啤酒)