【发布时间】:2017-02-07 07:04:46
【问题描述】:
所以我正在编写的代码(用于学校)假设将用户给定的整数打印在字符串中(示例输入:154 | 输出:一五四)。
目前我几乎想通了,除了它是倒退的!需要弄清楚如何以正确的方式打印(我的会打印出四五一)。
#include <iostream>
#include <string>
using namespace std;
void toString(int x) {
while (x > 0)
{
int digit = x % 10;
x /= 10;
switch (digit) {
case 1:
cout << "One ";
break;
case 2:
cout << "Two ";
break;
case 3:
cout << "Three ";
break;
case 4:
cout << "Four ";
break;
case 5:
cout << "Five ";
break;
case 6:
cout << "Six ";
break;
case 7:
cout << "Seven ";
break;
case 8:
cout << "Eight ";
break;
case 9:
cout << "Nine ";
break;
case 0:
cout << "Zero ";
break;
}
}
}
int main()
{
int num;
cout << "Please enter a positive number" << endl;
cin >> num;
while (num > 0)
{
toString(num);
cout << "\nPlease enter a positive number" << endl;
cin >> num;
}
cout << "You entered a number less than or equal to zero"
<< endl << "the program was terminated" << endl;
system("pause");
return 0;
}
【问题讨论】:
标签: c++ string int switch-statement