【发布时间】:2016-03-04 00:43:02
【问题描述】:
现在我能够制作一个程序,它只将单词的第一个字母转换为相应的数字,但在第一次转换后就停止了。 如果我在每个“案例”之后不使用“中断”,程序就会继续输出以下不是我想要的案例。
开关 (nameChar) { 案例“a”:案例“b”:案例“c”: cout
我可以让这个程序重复单词的以下字母直到单词中没有更多的字母吗?
#include <iostream>
#include<string>
using namespace std;
int main () {
char nameChar;
cout << "enter a name";
cin >> nameChar;
switch (nameChar)
{
case 'a': case 'b': case 'c':
cout << "1";
break;
case 'd': case 'e': case 'f':
cout << "2";
break;
case 'g': case 'h': case 'i':
cout << "3";
break;
case 'j': case 'k': case 'l':
cout << "4";
break;
case 'm': case 'n': case 'o':
cout << "5";
break;
case 'p': case 'q': case 'r':
cout << "6";
break;
case 's': case 't': case 'u':
cout << "7";
break;
case 'v': case 'w': case 'x':
cout << "8";
break;
case 'y': case 'z':
cout << "9";
break;
default:
return 0;
char nameChar;
cout << nameChar;
}
}
【问题讨论】:
-
不要再次声明nameChar。您要求一个新的未初始化变量。
标签: c++ switch-statement character digits