C++ 如何编写使用用户输入和测试文件结束函数的程序
我正在写一个程序,可以把字母转换成电话号码。我刚开始C++,并不真正熟悉类或成员,我在Web上看到的大多数例子都涉及这些概念。我有开关盒部分的工作,但无法得到文件结束部分的权利。我已将代码复制到下面:C++ 如何编写使用用户输入和测试文件结束函数的程序,c++,C++,我正在写一个程序,可以把字母转换成电话号码。我刚开始C++,并不真正熟悉类或成员,我在Web上看到的大多数例子都涉及这些概念。我有开关盒部分的工作,但无法得到文件结束部分的权利。我已将代码复制到下面: //Explanation of Program //This program converts inputted characters into numbers based on the telephone digit scheme //******************
//Explanation of Program
//This program converts inputted characters into numbers based on the telephone digit scheme
//**************************************Program begins here******************************************************************
#include <iostream> //Includes library file for input / output functionality
using namespace std;
int main()
{
//Declaration of variables
char cCharacter = 0; // User inputed character
char cNumber = 0; // Converted numbers from inputed characters
//Beginning of main program
cout << "This program converts inputted characters into numbers based on the telephone digit scheme"; //Explanation of program
while ( cCharacter != EOF)
{
cout << endl <<"Please hit '<ctrl> z' to exit, or enter a word on character at a time: ";
cin >> cCharacter;
cCharacter = toupper (cCharacter);
if (cCharacter == EOF)
{
break;
}
switch (cCharacter)
{
case 'A':
case 'B':
case 'C':
cNumber = '2';
break;
case 'D':
case 'E':
case 'F':
cNumber = '3';
break;
case 'G':
case 'H':
case 'I':
cNumber = '4';
break;
case 'J':
case 'K':
case 'L':
cNumber = '5';
break;
case 'M':
case 'N':
case 'O':
cNumber = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cNumber = '7';
break;
case 'T':
case 'U':
case 'V':
cNumber = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cNumber = '9';
break;
}
cout << "The number associated with the letter "<< cCharacter <<" is: "<<cNumber <<endl;
}
return 0;
}
另外,我想知道是否有一种方法(可能使用数组)来存储每个数字,然后按顺序打印每个数字。我不认为有什么问题,因为我的数组必须是可变长度的。我会改变很多事情,但作为一个简介CS程序,这并不太糟糕。有几件事:
我希望这对你有点帮助。如果没有您提供有关程序失败原因的更多信息,我只能根据以上列表对错误进行有根据的猜测。问题是,您期待的是一个神奇的“EOF”角色,而您不会得到它。结束程序的字符必须是用户可以实际输入的字符。“x”和“0”具有潜力,空白字符串也具有潜力(从技术上讲,在这种情况下,您需要测试换行符,即“\n”) 也就是说,我注意到你的代码有一些奇怪之处 首先,为什么要求一次输入一个字符?我希望一个电话号码“翻译”将整个电话号码以“字母形式”转换成我可以拨打的数字号码;否则,只要看一张桌子,或是看一看手机本身,我就无能为力了。你的教练说你必须这样做吗 你的循环是多余的;它的条件告诉它只在cCharacter不等于EOF时运行,但在循环中使用if时,它的EOF也会中断。其中只有一个是必要的。一种流行的方法是在循环前和循环结束时获取第一个字符,但也可以保留if break部分,并将while条件更改为1 为什么cNumber是字符?诚然,它对程序的执行没有任何实际影响,但提供类型是有原因的;一般来说,如果代码在语义上是正确的(在本例中,这意味着使用char表示字符,int表示整数),则代码更清晰,更易于维护。这对这个项目来说毫无意义,但这是一个好习惯
至于在数组中存储数字,是的,这是非常可能的。可以使用calloc()函数动态定义给定长度的数组,也可以使用向量。但是,这两种方法可能都远远超出了当前类的范围。通过点击输入设备上的特定控制序列,可以将标准输入设置为emultate“文件结束”。但这是特定于操作系统的: 在Unix/Linux(以及Mac OS-X)上
Ctrl-D 在Windows上
Ctrl-Z(我想) 虽然EOF字符EOF是C文件流的遗留物,所以最好测试流本身,看看它是否已经到达文件的末尾
while (!cin.good()) {/* Stream still readable ie not EOF */}
幸运的是;如果在布尔上下文中使用流,它会自动将自身转换为一个对象,如果流仍然可用,则该对象可转换为值为true的bool;如果字符串已进入错误状态(如EOF),则该对象可转换为值为false的bool
这里的问题是,直到您尝试读取文件末尾之后,才真正设置EOF。所以这里的测试是在你阅读之前完成的。现在,当您尝试阅读时,可能会失败,因此从技术上讲,您需要在阅读后进行另一项测试:
while(cin)
{
char x;
cin >> x;
if (cin) { /* The read worked */ }
}
但是运算符>>的结果返回对流的引用,因此我们可以一次完成所有操作:
char x;
while(cin>> x) // read a character into x and return cin.
// cin then used as the test to see if the stream is OK
{
/* cin is OK so the read into x must have worked. */
}
谢谢你的回复和帮助。我张贴我的最终解决方案,为任何其他人谁可能有相同的问题,并在这个页面上遇到。如果我的研究是正确的,eof()是stdio.h头文件的一个宏(或成员函数),是C编程的一个保留。下面是我的最终代码以及在下面的评论中发布的测试用例: //程序说明 //该程序根据电话数字方案将输入的字符转换为数字
//**************************************Program begins here*********************
#include <iostream> //Includes library file for input / output functionality
using namespace std;
int main()
{
//Declaration of variables
char cCharacter = 0; // User inputed character
char cNumber = 0; // Converted numbers from inputed characters
//Beginning of main program
cout << "This program converts inputted characters into numbers based on the telephone digit scheme"; //Explanation of program
while (!cin.eof()) // While condition runs unless 'eof' key is hit
{
cout << endl <<"Please hit '<ctrl> z' to exit, or enter a word on character at a time: ";
cin >> cCharacter; //User inputs a character value
cCharacter = toupper (cCharacter); // Converts lowercase values to uppercase
//Switch case function to select numbers from characters inputted
switch (cCharacter)
{
case 'A':
case 'B':
case 'C':
cNumber = '2'; // Letters A, B, or C converted to Number 2
break;
case 'D':
case 'E':
case 'F':
cNumber = '3'; // Letters D, E, or F converted to Number 3
break;
case 'G':
case 'H':
case 'I':
cNumber = '4'; // Letters G, H, or I converted to Number 4
break;
case 'J':
case 'K':
case 'L':
cNumber = '5'; // Letters J, K, or L converted to Number 5
break;
case 'M':
case 'N':
case 'O':
cNumber = '6'; // Letters M, N, or O converted to Number 6
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cNumber = '7'; // Letters P, Q, R, or S converted to Number 7
break;
case 'T':
case 'U':
case 'V':
cNumber = '8'; // Letters T, U, or V converted to Number 8
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cNumber = '9'; // Letters W, X, Y, or Z converted to Number 9
break;
// defualt adds newline
default:
cout <<endl;
break;
}
//Output of character inputted and number associated with it
cout << "The number associated with the letter "<<cCharacter <<" is: "<<cNumber <<endl;
}//End while loop
return 0; //indicates program success
} //End Main () function
/* Testing of variables
Test 1 - TOAST ******************************************************************************************
This program converts inputted characters into numbers based on the telephone di
git scheme
Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
The number associated with the letter T is: 8
Please hit '<ctrl> z' to exit, or enter a word on character at a time: o
The number associated with the letter O is: 6
Please hit '<ctrl> z' to exit, or enter a word on character at a time: a
The number associated with the letter A is: 2
Please hit '<ctrl> z' to exit, or enter a word on character at a time: s
The number associated with the letter S is: 7
Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
The number associated with the letter T is: 8
Please hit '<ctrl> z' to exit, or enter a word on character at a time: ^Z
The number associated with the letter T is: 8
Press any key to continue . . .
TOAST
86278
/*********************************程序从这里开始*********************
#include//包含用于输入/输出功能的库文件
使用名称空间std;
int main()
{
//变量声明
char cCharacter=0;//用户输入的字符
char cNumber=0;//从输入的字符转换的数字
//主程序的开始
你说你不能“正确”是什么意思?编译器错误?不能工作?其他什么?很抱歉造成混淆。我认为我的程序在运行时会在点击“z”后进入一个无止境的循环。我最终用“while(!cin.eof())解决了它。”经过数小时的搜索,我在网上找到了它。我想这是iostream标题的内置功能,但不确定。谢谢你的时间和考虑。谢谢你的帮助。我最终使用了我想要的
//**************************************Program begins here*********************
#include <iostream> //Includes library file for input / output functionality
using namespace std;
int main()
{
//Declaration of variables
char cCharacter = 0; // User inputed character
char cNumber = 0; // Converted numbers from inputed characters
//Beginning of main program
cout << "This program converts inputted characters into numbers based on the telephone digit scheme"; //Explanation of program
while (!cin.eof()) // While condition runs unless 'eof' key is hit
{
cout << endl <<"Please hit '<ctrl> z' to exit, or enter a word on character at a time: ";
cin >> cCharacter; //User inputs a character value
cCharacter = toupper (cCharacter); // Converts lowercase values to uppercase
//Switch case function to select numbers from characters inputted
switch (cCharacter)
{
case 'A':
case 'B':
case 'C':
cNumber = '2'; // Letters A, B, or C converted to Number 2
break;
case 'D':
case 'E':
case 'F':
cNumber = '3'; // Letters D, E, or F converted to Number 3
break;
case 'G':
case 'H':
case 'I':
cNumber = '4'; // Letters G, H, or I converted to Number 4
break;
case 'J':
case 'K':
case 'L':
cNumber = '5'; // Letters J, K, or L converted to Number 5
break;
case 'M':
case 'N':
case 'O':
cNumber = '6'; // Letters M, N, or O converted to Number 6
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cNumber = '7'; // Letters P, Q, R, or S converted to Number 7
break;
case 'T':
case 'U':
case 'V':
cNumber = '8'; // Letters T, U, or V converted to Number 8
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cNumber = '9'; // Letters W, X, Y, or Z converted to Number 9
break;
// defualt adds newline
default:
cout <<endl;
break;
}
//Output of character inputted and number associated with it
cout << "The number associated with the letter "<<cCharacter <<" is: "<<cNumber <<endl;
}//End while loop
return 0; //indicates program success
} //End Main () function
/* Testing of variables
Test 1 - TOAST ******************************************************************************************
This program converts inputted characters into numbers based on the telephone di
git scheme
Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
The number associated with the letter T is: 8
Please hit '<ctrl> z' to exit, or enter a word on character at a time: o
The number associated with the letter O is: 6
Please hit '<ctrl> z' to exit, or enter a word on character at a time: a
The number associated with the letter A is: 2
Please hit '<ctrl> z' to exit, or enter a word on character at a time: s
The number associated with the letter S is: 7
Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
The number associated with the letter T is: 8
Please hit '<ctrl> z' to exit, or enter a word on character at a time: ^Z
The number associated with the letter T is: 8
Press any key to continue . . .
TOAST
86278