views:

156

answers:

4

Hello guys, 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
                ); 
    }
A: 

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.

msw
I think it ends when he / she enters a carriage return.
JonH
yes, that's right Jon.
Vlad
agreed, edited.
msw
+2  A: 

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.

JonH
I don't want the input to be printed twice. For example if i input two polynomials, i want the screen to show only:P1 = term1 +/- term2... //read until Enter pressed P2 = term1 +/- term2... //read until Enter pressed
Vlad
I'm using getch() because afaik this function doesn't print characters as they are read.Am I wrong?
Vlad
@Vlad - then you shouldn't use getch().
JonH
What should i use instead?
Vlad
@Vlad - you can use it you just need to clearly define the problem. Do you want it so that it only takes x's? I noticed you said in your example 2x^2wez +x +1 it should only come back as 2x^2+x+1. You have to validate this string when it is typed in. Clearly define the inputs and outputs and someone can help you.
JonH
sorry wrong place
Vlad
@Vlad use '\r' instead of '\n' you are looking for a carriage return.
JonH
+1  A: 

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

Dave18
not only letters, but numbers also.If i type 2x^2wez + x + 1, it should show on screen only 2x^2 + x + 1;
Vlad
@Vlad that is your problem, your printable function allows for w as its ascii representation in decimal is 119
JonH
Well i'm thinking on putting a condition so that if the variable name was read it won't print any other letter than the one which coresponds to the variable name(x in my example);
Vlad
+1  A: 

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);
}
Thomas Matthews
nice suggestion, but i won't need those as the solution was to simply change Enter condition from '\n' to '\r'.
Vlad