帮助 C++ 中的字符输入和打印
我想从控制台读取字符并仅在它们具有特定值时才将它们逐一打印。
好吧,我尝试使用这样的东西:
char c;
while (c != '\n') {
c = getch();
if (printable(c)) cout << c; // where printable is a function which checks
// if the character is of a certain value
}
但这不起作用,因为它会打印所有字符,所以我应该使用什么想法?
多谢!
编辑
嗯,我想制作多项式计算器,用户在其中输入项直到按下 Enter,但是如果例如用户输入“r”或“R”,它将重置输入或“q”并且'Q' 退出程序,即使用户输入非法字符,如 '@'、'、'、';' 等(我也不希望打印 'r' 或 'q'),它也不会将它们打印在屏幕上。
这也是可打印的函数:
bool printable(char c)
{
return (
((int(c) > 42 && int(c) < 123) || isspace(c)) && int(c) != 44 && int(c) != 46 && int(c) != 47 &&
int(c) != 58 && int(c) != 59 &&
int(c) != 60 && int(c) != 61 && int(c) != 62 && int(c) != 63 && int(c) != 64 && int(c) != 65 &&
int(c) != 91 && int(c) != 92 && int(c) != 93 && int(c) != 95 && int(c) != 96
);
}
i want to read characters from the console and print them one after another only if they have a certain value.
Well i tried using something like this:
char c;
while (c != '\n') {
c = getch();
if (printable(c)) cout << c; // where printable is a function which checks
// if the character is of a certain value
}
But this doesn't work as it prints all the characters, so any ideas what should i use?
Thanks a lot!
Edit
Well i want to make polynomial calculator in which the user inputs the terms until pressed Enter, but if for example the user inputs 'r' or 'R' it will reset the input or 'q' and 'Q' to quit the program and also even if the user inputs illegall characters like '@',',',';', etc (also i don't want 'r' or 'q' printed) it won't print them on screen.
Also here's the printable function:
bool printable(char c)
{
return (
((int(c) > 42 && int(c) < 123) || isspace(c)) && int(c) != 44 && int(c) != 46 && int(c) != 47 &&
int(c) != 58 && int(c) != 59 &&
int(c) != 60 && int(c) != 61 && int(c) != 62 && int(c) != 63 && int(c) != 64 && int(c) != 65 &&
int(c) != 91 && int(c) != 92 && int(c) != 93 && int(c) != 95 && int(c) != 96
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能需要将 cout 语句更改为
cout << “您刚刚输入:” << c;
这样您就可以实际查看是否成功满足 if 条件。还发布可打印()。
这是一个仅抓取字符的示例,不确定为什么您使用 getch() 您应该使用 cin.get,但无论如何对于您的示例:
对于任何想知道的人,getch() 可在
conio.h.就我而言,我只是检查字符的 int 表示形式,以及它是否 > > 65 返回 true,否则返回 false。
编辑
Vlad w 和 z 都出现的原因是 w 的十进制表示形式是 119,z 是 123。现在你的 isPrintable 函数有一个允许这样做的 if 条件:
这将评估为 TRUE 所以如果您不想要 aw,则需要限制该范围。
You may want to change your cout statement to
cout << "You just typed: " << c;
That way you can actually see if you've hit the if condition successfully. Also post printable().
Here is a sample of just grabbing a char, not sure why you are using getch() you should use cin.get, but anyhow for your example:
For anyone wondering, getch() is available in
conio.h
. In my case I am just checking the int representation of the character and if it is > 65 returning true else false.EDIT
Vlad the reason why w and z both show up is their decimal representation of w is 119 and z is 123. Now your isPrintable function has an if condition which allows for this:
This will evaluate to TRUE so if you do not want a w you need to restrict that range.
你想做这样的事情吗?
这只会打印出字母并在按回车键时结束程序
Are you trying to do something like this?
This would print out only letters and ends the program on pressing return key
有很多方法可以检查字符是否可打印:
isprint()
(库例程)if
)库例程
isprint
此函数随 C 和 C++ 语言一起提供。阅读参考页: isprint 函数
逐个字符比较
在您尝试的函数中类似:
返回c == 65;
但更易读的语法是:
return c == 'a';
搜索已知字符的字符串
创建一个可打印字符的常量字符串并搜索它:
表查找:
There are many methods to check if a character is printable:
isprint()
(library routine)if
)Library routine
isprint
This function comes with both the C and C++ language. Read a reference page: isprint function
Comparing character by character
In your function you try something like:
return c == 65;
But a more readable syntax is:
return c == 'a';
Search a string of known characters
Create a const string of printable characters and search it:
Table lookup:
它不会打印所有字符,您的终端窗口会回显您键入的字符。如果您将其作为
program
运行,您会更清楚地看到这一点。 some_file
代码还有其他缺陷(例如当没有更多字符时它会做什么?)但这些是其他问题。
It isn't printing all the characters, your terminal window is echoing the characters you type. You would see this more clearly if you ran it as
program < some_file
The code has other flaws (e.g. what does it do when there are no more characters?) but those are other questions.