Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 663997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:33:03+00:00 2026-05-13T23:33:03+00:00

i want to read characters from the console and print them one after another

  • 0

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
                ); 
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:33:04+00:00Added an answer on May 13, 2026 at 11:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read data about MMORPG characters from a .txt file and then
I want to read UTF-8 strings from a server that I have control of,
I have a page at URL http://site.com/params. I want to read only the first
I am trying to read Ò ÉÑÎÔÜ Ê characters from XML and want to
I want to read a string from the console. Using scanf or fgets however,
I'm using a DataInputStream to read characters/data from a socket. I want to use
I want to read the count of characters for a cell and set it
I have a very long String and I want to read character by character
I have a text file. I want read that file. But In that if
I have a specific object with image data. And I want read/write images (in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.