1

我两个月前开始学习计算机科学。我需要这方面的帮助。

编译器给了我这个警告,我的程序没有给出数字。

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;

}
4

5 回答 5

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;

旁注:代码的缩进很重要。如果它更容易阅读,你就会更容易处理它。

于 2013-10-10T14:23:39.290 回答
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功能不起作用,但如果您修复警告,您会发现部分问题。

于 2013-10-10T14:24:07.480 回答
1

前两个错误来自这一行:

    result = 'tens' + "-" + 'units';

我认为你的意思是连接参数变量,所以它应该是:

    result = tens + "-" + units;

最后两个错误来自两行:

cout << word_number;

word_number是一个函数,你应该调用它,而不是试图打印函数本身(它只是打印它的地址)。

cout << word_number("something", "something_else");

我无法弄清楚该函数的参数应该用于什么。似乎它应该num_entered作为参数,而不是使用全局变量。

于 2013-10-10T14:27:32.607 回答
0
  1. 赋值运算符是单个'='(units =="One"应该是units = "One"
  2. 字符串用双引号 '"' (result = 'tens' + "-" + 'units'应该是result = string("tens") + "-" + "units"
于 2013-10-10T14:22:34.517 回答
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);
于 2013-10-10T14:25:00.867 回答