尽管我一直在寻找答案,但我对其中的任何一个都不太了解,所以我决定寻求直接帮助。
基本上,该程序应读取该数字的每个数字,然后正常记录下来。现在,这是一个“骨架”代码,因为我正在尝试进一步编程它的主要思想,但是我已经在错误中绊脚石了。
我使用的任何 switch 函数行均出现错误,但我不知道如何解决。我收到错误错误C2659:'=':用作左操作数
#include <iostream>
#include <string>
using namespace std;
int number, digit;
int i;
string word[4];
void lowering();
void units();
void tens();
int main()
{
i = 4;
word[i];
cout << "Enter a number: ";
cin >> number;
cout << endl;
lowering();
units();
i--;
lowering();
tens();
for(int x = 4; x >=0; x--)
cout << word[x];
system("pause");
return 0;
}
void lowering()
{
digit = number % 10;
number = number / 10;
}
void units()
{
switch (digit)
{
case 1:
word[i].append = " one"; break;
case 2:
word[i].append = " two"; break;
case 3:
word[i].append = " three"; break;
case 4:
word[i].append = " four"; break;
case 5:
word[i].append = " five"; break;
case 6:
word[i].append = " six"; break;
case 7:
word[i].append = " seven"; break;
case 8:
word[i].append = " eight"; break;
case 9:
word[i].append = " nine"; break;
default:
word[i].append = "";
}
word[i].append = " ";
}
void tens()
{
switch (digit)
{
case 1:
word[i].append = " ten"; break;
case 2:
word[i].append = " twenty "; break;
case 3:
word[i].append = " thirty "; break;
case 4:
word[i].append = " fourty"; break;
case 5:
word[i].append = " fifty"; break;
case 6:
word[i].append = " sixty"; break;
case 7:
word[i].append = " seventy"; break;
case 8:
word[i].append = " eighty"; break;
case 9:
word[i].append = " ninety"; break;
default:
word[i].append = "";
}
word[i].append = " ";
}
答案 0 :(得分:0)
在切换情况下,您应该使用此命令:
void units()
{
switch (digit)
{
case 1:
word[i].append(" one"); break;
case 2:
word[i].append(" two"); break;
case 3:
word[i].append(" three"); break;
case 4:
word[i].append(" four"); break;
case 5:
word[i].append(" five"); break;
case 6:
word[i].append(" six"); break;
case 7:
word[i].append(" seven"); break;
case 8:
word[i].append(" eight"); break;
case 9:
word[i].append(" nine"); break;
default:
word[i].append("");
}
word[i].append(" ");
}
除了要提高代码质量外,还应该将break;
放在新行中。