gpt4 book ai didi

C++ 循环 - 流错误?

转载 作者:行者123 更新时间:2023-11-28 06:42:15 25 4
gpt4 key购买 nike

在我的编程课上,我们的任务是编写一个程序,将单词或短语转换为电话号码,评估每个字符并将其翻译成相应的数字。

这是目前的代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
char letter;
int noOfLetters;
char response;

cout << "Enter Y/y to convert a telephone number "
<< "form letters to digits.\n"
<< "Enter any other letter to terminate the program: ";

cin >> response;
cout << endl;

while (response == 'Y' || response == 'y')
{

cout << "Enter a telephone number using letters: ";
cin >> letter;

noOfLetters = 0;

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

while (noOfLetters != 7)
{
//cout << "[" << noOfLetters << "]";
noOfLetters++;

switch (toupper(letter))
{
case 'A':
case 'B':
case 'C':
cout << 2;
break;
case 'D':
case 'E':
case 'F':
cout << 3;
break;
case 'G':
case 'H':
case 'I':
cout << 4;
break;
case 'J':
case 'K':
case 'L':
cout << 5;
break;
case 'M':
case 'N':
case 'O':
cout << 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cout << 7;
break;
case 'T':
case 'U':
case 'V':
cout << 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cout << 9;
break;
default:
cout << "[invalid]";
}

if (noOfLetters == 3)
{
cout << '-';
}
else if (noOfLetters > 7)
{
cin.ignore();
}
cin >> letter;

//noOfLetters++;
}
cout << endl;
cin.ignore(100, '\n');

cout << "\nTo process another telephone number, enter Y/y \n"
<< "Enter any other letter to terminate the program: ";
cin >> response;
}
}

如果我输入超过 7 个字符,一切正常。问题是,如果我正好输入 7 个字符,它就会把自己弄乱。

假设我输入“honk honk”(不包括空格,长度为 8 个字符)并运行程序。输出如下:

Enter Y/y to convert a telephone number form letters to digits. Enter any other letter to terminate the program: y

Enter a telephone number using letters: honk honk The corresponding telephone number is: 466-5466

To process another telephone number, enter Y/y Enter any other letter to terminate the program: n Press any key to continue . . .

所以,这很好用。但是如果我输入“七十”(恰好 7 个字符),程序会强制我在继续之前输入另一个字符(不包括空格和换行符),如下所示:

Enter Y/y to convert a telephone number form letters to digits. Enter any other letter to terminate the program: y

Enter a telephone number using letters: seventy The corresponding telephone number is: 738-3689 h

To process another telephone number, enter Y/y Enter any other letter to terminate the program: n Press any key to continue . . .

我想修改程序,以便我能够在程序中输入 7 个字符的短语并使其正确运行而不会出现任何错误,例如当我输入任何长度为 8 个字符或更多的字符时。

我已经尝试了所有我能做的,但没有运气。有人可以提出建议来帮助我修复此错误吗?

最佳答案

你的逻辑流程有点困惑。请注意,当 noOfLetters 为七时,您仍会在 while 循环结束时要求另一个字母。考虑在循环的顶部读取。如果这样更容易,请缓冲循环中的输出。

cout << "Enter a telephone number using letters: " << flush;
// Flush may be necessary if output is line-buffered ^

noOfLetters = 0;

// This is a buffer where we will be storing the output phone number so that we
// don't have to deal with mixing input with output.
//
// You will need to "#include <sstream>".
stringstream phoneNumber;

// Reading becomes part of the loop condition; if the end of the input is reached,
// we want the loop to terminate. "cin >> letter" will evaluate to false in
// boolean context if reading failed.
//
// Note that && short-circuits; if the left side is false then the right side is
// not even evaluated. So when noOfLetters == 7, the loop terminates without
// reading another character.
while (noOfLetters != 7 && cin >> letter) {
++noOfLetters;

// Your switch block goes here. Replace "cout" with "phoneNumber".

if (noOfLetters == 3) phoneNumber << '-';
}

cout << "The corresponding telephone number is: " << phoneNumber.rdbuf() << endl;

关于C++ 循环 - 流错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25772300/

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