帮助 C++ 中的字符输入和打印

发布于 2024-08-25 00:50:57 字数 1164 浏览 2 评论 0原文

我想从控制台读取字符并仅在它们具有特定值时才将它们逐一打印。

好吧,我尝试使用这样的东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

讽刺将军 2024-09-01 00:50:57

您可能需要将 cout 语句更改为 cout << “您刚刚输入:” << c;
这样您就可以实际查看是否成功满足 if 条件。还发布可打印()。

这是一个仅抓取字符的示例,不确定为什么您使用 getch() 您应该使用 cin.get,但无论如何对于您的示例:

bool isPrintable(char c)
 {
     bool isItPrintable=false;

     if ((int)c >= 65)
        isItPrintable=true;

        return isItPrintable;
 }

int main()
{
    char c;

    while (c != '\r')
      {
           c=getch();
           if (isPrintable(c))
             {
                cout << "You just entered: " << c << endl;
             }
      }
    return 0;
}

对于任何想知道的人,getch() 可在 conio.h.就我而言,我只是检查字符的 int 表示形式,以及它是否 > > 65 返回 true,否则返回 false。

编辑

Vlad w 和 z 都出现的原因是 w 的十进制表示形式是 119,z 是 123。现在你的 isPrintable 函数有一个允许这样做的 if 条件:

(int(c) > 42 && int(c) < 123)

这将评估为 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:

bool isPrintable(char c)
 {
     bool isItPrintable=false;

     if ((int)c >= 65)
        isItPrintable=true;

        return isItPrintable;
 }

int main()
{
    char c;

    while (c != '\r')
      {
           c=getch();
           if (isPrintable(c))
             {
                cout << "You just entered: " << c << endl;
             }
      }
    return 0;
}

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:

(int(c) > 42 && int(c) < 123)

This will evaluate to TRUE so if you do not want a w you need to restrict that range.

通知家属抬走 2024-09-01 00:50:57

你想做这样的事情吗?

bool printable(char c)
{
    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
    {
        return true;
    }
    return false;
}

int main()
{
  char c = ' ';

    while (c != '\r') {
           c = _getch();
    if (printable(c)) cout << c; // where printable is a function which checks 
                                        // if the character is of a certain value
    }  
}

这只会打印出字母并在按回车键时结束程序

Are you trying to do something like this?

bool printable(char c)
{
    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
    {
        return true;
    }
    return false;
}

int main()
{
  char c = ' ';

    while (c != '\r') {
           c = _getch();
    if (printable(c)) cout << c; // where printable is a function which checks 
                                        // if the character is of a certain value
    }  
}

This would print out only letters and ends the program on pressing return key

甜心 2024-09-01 00:50:57

有很多方法可以检查字符是否可打印:

  1. isprint() (库例程)
  2. 逐个字符比较(通过
    if)
  3. 搜索已知字符的字符串
  4. 查表

库例程 isprint

此函数随 C 和 C++ 语言一起提供。阅读参考页: isprint 函数

逐个字符比较

在您尝试的函数中类似:
返回c == 65;
但更易读的语法是:
return c == 'a';

搜索已知字符的字符串

创建一个可打印字符的常量字符串并搜索它:

bool is_print(char c)
{
    static const std::string    printable_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    return printable_chars.find(c) != std::string::npos;
}

表查找:

bool is_print(char c)
{
    static const char printable_chars[] = {'1', '2', '3', '4', '5', '6'};
    return std::binary_search(printable_chars,
                              printable_chars + sizeof(printable_chars),
                              c);
}

There are many methods to check if a character is printable:

  1. isprint() (library routine)
  2. Compare character by character (via
    if)
  3. Search a string of known characters
  4. Table lookup

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:

bool is_print(char c)
{
    static const std::string    printable_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    return printable_chars.find(c) != std::string::npos;
}

Table lookup:

bool is_print(char c)
{
    static const char printable_chars[] = {'1', '2', '3', '4', '5', '6'};
    return std::binary_search(printable_chars,
                              printable_chars + sizeof(printable_chars),
                              c);
}
天气好吗我好吗 2024-09-01 00:50:57

它不会打印所有字符,您的终端窗口会回显您键入的字符。如果您将其作为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文