How to input char variables in the same line?

This is a discussion on How to input char variables in the same line? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Lucas525 I get what you want to say but when I press "a" the input MUST stop ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    Quote Originally Posted by Lucas525 View Post
    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".
    Ah... then you have to use unbuffered input, which is not provided by the standard libraries. This FAQ will show you several ways to get an unbuffered character. That way the program ends immediately after pressing 'a'.

    Interestingly enough, with what you showed me, concerning something like "pdfjka ifhf" everything after 'a' isn't going to be processed anyway.

    I hope I helped.

  2. #17
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    It's done! getche() was the command I was looking for. So this is the program:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main()
    {
        char suglasnici [43] = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z','B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z','\0'};
        char slovo;
        int sam = 0;
        int sug = 0;
        int i;
        cout << "Upisite slova. Za kraj upisite malo slovo a: " << endl;
        do
        {
            slovo = getche();
            cout << " ";
            if (slovo=='a'||slovo=='e'||slovo=='i'||slovo=='o'||slovo=='u'||slovo=='A'||slovo=='E'||slovo=='I'||slovo=='O'||slovo=='U')
            {
                sam++;
            }else
            {
                for(i=0;i<strlen(suglasnici);i++)
                {
                    if(slovo == suglasnici[i])
                    {
                        sug++;
                    }
                }
            }
        }while(slovo!='a');
        cout << "\nBroj samoglasnika je: " << sam << endl;
        cout << "Broj suglasnika je: " << sug << endl;
        return 0;
    }
    The variables and texts are all in croatian. I know I could have made the suglasnici(consonants) variable as a string so it would be shorter but it's ok as long as it's working...

Page 2 of 2 FirstFirst 12
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, 02: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, 08: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, 08:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21