Vlad
Vlad

Reputation: 618

Help with char input and printing in C++

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
                ); 
    }

Upvotes: 0

Views: 1546

Answers (4)

JonH
JonH

Reputation: 33163

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.

Upvotes: 2

Thomas Matthews
Thomas Matthews

Reputation: 57743

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);
}

Upvotes: 1

cpx
cpx

Reputation: 17577

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

Upvotes: 1

msw
msw

Reputation: 43507

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.

Upvotes: 0

Related Questions