gpt4 book ai didi

c++ - 在 C++ 中将电话号码中的字母转换为数字

转载 作者:行者123 更新时间:2023-11-30 01:31:20 25 4
gpt4 key购买 nike

我整天都在盯着这个看,这段代码让我感到很困难。我需要这个程序来将一串字母(例如 Call Cash)转换为 7 位数的电话号码。我得到两种结果之一。该程序要么根本不输出任何数字,要么陷入无限循环。如有任何帮助,我们将不胜感激。

// Program to convert letters to numbers in a phone number.

//Header file
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
//Declare variables
char input;
char letters;
int number;
int counter;
counter = 0;


//Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
cout << "Enter telephone number using letters: ";
cin >> letters;
cout << endl;

cout << "The corresponding phone number is: ";

while (counter <= 7);
{
cout << number;
if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
switch (letters)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << "2";
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << "3";
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << "4";
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << "5";
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << "6";
break;

case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << "7";
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << "8";
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << "9";
break;

if (counter == 3)
cout << "-";
}
}
}


return 0;
}

编辑:这是我的新代码。我现在遇到的问题是它只返回第一个字母的值,并且在单独的行上返回每个值。我非常感谢您的帮助:

    //Declare variables
char input;
char letters;
int number;
int i;


//Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
cout << "Enter telephone number using letters: ";
cin >> letters;
cout << endl;

cout << "The corresponding phone number is: ";

for(int i = 0; i < 7; i++)
{
if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
switch (letters)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << "2";
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << "3";
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << "4";
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << "5";
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << "6";
break;

case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << "7";
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << "8";
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << "9";
break;
cout << number;
}

if (i == 2)
cout << "-";

}
cout << "To process another telephone number, enter Y/y." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
}


return 0;

最佳答案

你的 while (counter <= 7) 后面有一个分号.您放错了 switch (letters) 的右括号,它应该在 if (counter == 3) 之前 .你有一个 while循环 input ,但您永远不会在程序运行时更改它。

我都得到了吗?

关于c++ - 在 C++ 中将电话号码中的字母转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com