Thread: How to input char variables in the same line?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    How to input char variables in the same line?

    I have to make this program: I have to enter x number of letters in the same line and the program has to count vowels and consonants. So I've made a program that does all that except I don't know how to enter the letters in the same line.
    P.S. I know this is a basic c++ but couldn't find a better place to post this.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        char letter;
        int vow = 0;
        int con = 0;
        cout << "Enter the letters. To end the input insert a: " << endl;
        do
        {
            cin >> letter;
            if (letter=='a'||letter=='e'||letter=='i'||letter=='o'||letter=='u'||letter=='A'||letter=='E'||letter=='I'||letter=='O'||letter=='U')
            {
                vow++;
            }else
            {
                con++;
            }
        }while(letter!='a');
        cout << "Number of vowels is: " << vow << endl;
        cout << "Number of consonants is: " << con << endl;
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So... essentially you want to input a string, say, "Hello World!" and count consonants and vowels?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    No, I have to enter random number of letters and the input ends with little letter a, for example:

    Input: b i G h e K p a (and when I type in that last letter which has to be "a" it guves me a number of vowels and consonants that I typed in)

    Output: Number of vowels is: 3. Number of consonants is: 4.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then what does letters in the same line mean?

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Like I just wrote it.
    RIGHT:
    Input: b [ENTER] i [ENTER] G [ENTER] ....
    WRONG:
    Input:
    b [ENTER]
    i [ENTER]
    G [ENTER] ....

    ... or without having to press ENTER doesn't matter it just has to get letters in the same line and has to end with a letter "a" not "A".

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see why it would be unacceptable.
    ...So, is this unacceptable too:

    Input: "Hello World!"
    Output: "Invalid input! Try again."
    Input "Hello Worlda"
    output: OK
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    You have to input a few letters (not sentences! ... and in the same line) and the input finishes when you enter "a", then you get a number of vowels and consonants as the result.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Have you tried just running what you have, with the input in the format that you expect?

    Even if you are reading input character by character, that in no way implies that the format of the input is one character per line.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    What I need is when I type in first letter for example "b" and press ENTER to stay in the same line, don't want to go to a new line, even though I don't know if that's even possible.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you want is not possible in any portable way. Hence, we are asking if it is possible to just lax the requirements.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    What you want is not possible in any portable way. Hence, we are asking if it is possible to just lax the requirements.
    You're utterly wrong.

    OP, the code already does what you want. To use your example all you have to do is type

    b i G h e K p a

    And then press enter. The loop will still stop at 'a'. Since cin is line buffered, though, you will have to press enter to get things to happen (that's usually what buffered means -- how many keys you have to press before something happens). You can press enter whenever you're ready. This is what I was trying to say in my earlier post. Just because you read one character at a time, it doesn't mean the characters have to be on separate lines.
    Last edited by whiteflags; 01-15-2012 at 07:57 AM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    ...

    OP, the code already does what you want. To use your example all you have to do is type

    b i G h e K p a

    And then press enter. The loop will still stop at 'a'. Since cin is line buffered, though, you will have to press enter to get things to happen (that's usually what buffered means -- how many keys you have to press before something happens). You can press enter whenever you're ready. This is what I was trying to say in my earlier post. Just because you read one character at a time, it doesn't mean the characters hae to be on separate lines.
    That is what I suggested before, which apparently was "wrong."
    A sequence of characters. A sentence is also a sequence of characters.
    But apparently it must end with an 'a', so if you enter a series of characters that do not end with an 'a', then what?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That is what I suggested before, which apparently was "wrong."
    A sequence of characters. A sentence is also a sequence of characters.
    You have to press when you know you're right.

    But apparently it must end with an 'a', so if you enter a series of characters that do not end with an 'a', then what?
    Why don't you know? The loop continues until you do enter an 'a' obviously. If you make several "lines" of input it makes no difference.
    Last edited by whiteflags; 01-15-2012 at 08:05 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Why don't you know? The loop continues until you do enter an 'a' obviously. If you make several "lines" of input it makes no difference.
    That contradicts what the OP wants.
    So my question is still: is it acceptable to just let it wrap around several lines instead of requiring all characters be on a single line?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    You're utterly wrong.

    OP, the code already does what you want. To use your example all you have to do is type

    b i G h e K p a

    And then press enter. The loop will still stop at 'a'. Since cin is line buffered, though, you will have to press enter to get things to happen (that's usually what buffered means -- how many keys you have to press before something happens). You can press enter whenever you're ready. This is what I was trying to say in my earlier post. Just because you read one character at a time, it doesn't mean the characters have to be on separate lines.
    I get what you want to say but when I press "a" the input MUST stop and the program goes to output, in other words, it cannot allow me to input any more letters after "a".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading line by line - input function
    By Lindira-123 in forum C Programming
    Replies: 11
    Last Post: 02-06-2011, 03:34 PM
  2. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM
  3. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  4. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM
  5. follow up with char I/O and line, word, and char counting
    By Led Zeppelin in forum C Programming
    Replies: 1
    Last Post: 03-23-2002, 09:26 PM