Thread: Print only alphabet not integer

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    Print only alphabet not integer

    I am learning c++ and I want to print only alphabet not integer value

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {    
        char alphabet;
    
        cout << "Enter an alphabet: ";
        cin >> alphabet;
    
        cout << "You entered alphabet " << alphabet;    
        return 0;
    }
    Program output

    You entered alphabet b
    You entered alphabet 2

    How to print only alphabet from a to z not integer value

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    There's a standard function called isalpha. It can tell you whether a character is alphabetical or not. Use it in combination with an if statement to produce the output you want.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by GReaper View Post
    There's a standard function called isalpha. It can tell you whether a character is alphabetical or not. Use it in combination with an if statement to produce the output you want.
    If I have to print decimal numbers in the c then I use %d and %c for Character and %s for string

    Is there such an operator in C ++, so that whatever we want to print we can print only that

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    In C++ you use cout << to print anything. In your case it's like this:
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(void)
    {
      char ch;
    
      cout << "Enter an alphabet: ";
      cin >> ch;
    
      if (isalpha(ch))
        cout << "You entered: " << ch << " = " << static_cast<int>(ch) << "\n";
      else
        cout << "Not a character" << "\n";
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Thanks, I am trying to print number from 1 to 10
    Program
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {    
        int i;
        for (i=0; i < 11; i++)
        {
        
          cout << "print number: \n  " << i;
        
        }
            
        return 0;
    }
    Output

    print number:
    0print number:
    1print number:
    2print number:
    3print number:
    4print number:
    5print number:
    6print number:
    7print number:
    8print number:
    9print number:
    10

    Why number's are printing before message

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Why number's are printing before message
    Look at the position
    - of the message
    - of the newline
    - of the variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    mmmm
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        
        char ch;
         int i;
        
        cout << "Enter a letter " <<endl;
        cin>>ch;
        
        //looking for a number
        
        if ( !isalpha(ch) )
            cout << " number --> "<<ch<<endl;
        else
            cout <<" letter ... "<<ch<<endl;
        
        //printing 1 - 10
          for (i = 0; i < 10; i++)
            cout<< i+1 <<endl;
            
        return 0;
    }
    you started with one thing and ended with something else. cool...
    Last edited by userxbw; 12-13-2017 at 11:41 AM.

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    > Why number's are printing before message
    Look at the position
    - of the message
    - of the newline
    - of the variable.
    I didn't understand

    Code:
    #include <iostream>
    using namespace std;
     
    int main(void)
    {    
        int i;
        for (i=0; i < 11; i++)
        {
         
          cout << " print number: " <<i;
         
        }
             
        return 0;
    }
    output

    print number: 0 print number: 1 print number: 2 print number: 3 print number: 4 print number: 5 print number: 6 print number: 7 print number: 8 print number: 9 print number: 10

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, so you removed the "\n" from the program code, and now you have no newlines in the output.

    What have you learnt?

    You need to start thinking along the lines of "If I do this, the following things will happen". Only then will you have any measure of success.

    You can't program simply by trial and error.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    OK, so you removed the "\n" from the program code, and now you have no newlines in the output.

    What have you learnt?

    You need to start thinking along the lines of "If I do this, the following things will happen".
    I understood what was mistake I have just started writing the program . This was my second program in c++

    Code:
    #include <iostream>
    using namespace std;
     
    int main(void)
    {    
        int i;
        for (i=0; i < 11; i++)
        {
         
          cout << " print number: " <<i <<"\n";
         
        }
             
        return 0;
    }
    print number: 0
    print number: 1
    print number: 2
    print number: 3
    print number: 4
    print number: 5
    print number: 6
    print number: 7
    print number: 8
    print number: 9
    print number: 10
    Last edited by abhi143; 12-13-2017 at 01:55 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There you go !
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i scan and print an integer?
    By ziggy786 in forum C Programming
    Replies: 3
    Last Post: 01-28-2013, 06:05 PM
  2. Putchar() to print an integer value?
    By swgh in forum C Programming
    Replies: 2
    Last Post: 05-19-2007, 12:30 PM
  3. How do I print 64 bit unsigned integer in hex?
    By electrolove in forum C Programming
    Replies: 7
    Last Post: 02-11-2003, 12:43 PM
  4. Why does an integer print like that?
    By johnnyd in forum C Programming
    Replies: 6
    Last Post: 03-29-2002, 01:36 AM
  5. Integer to print ASCII
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-04-2001, 11:12 AM

Tags for this Thread