Thread: getche() with cin.getline() ?

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    getche() with cin.getline() ?

    Hi,

    I have this simple excersize..

    Using the getche() function to read the characters, write your own version of the cin.getline() function. Include the default argument for the terminating character.

    I can't understand how I may use getche() with cin.getline().
    I thought its either one or the other..

    What do you suggest?
    Thanks!!!
    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    The function is getch(), not getche(), unless there's one I don't know. The exercise probably calls for getche()so that the names don't conflict. You create the getche() function, and you don't use getline(). From what you wrote, you're supposed to write a function that does the same thing as getline().
    Anyway, try putting getch()in a while loop that tests for the delimiter character.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>unless there's one I don't know

    Then there is one you don't know.

    getche() stands for get character echo.

    getch() doesn't echo the character to the screen whereas getche() does.

    It is really that simple.

    >>Using the getche() function to read the characters, write your own version of the cin.getline() function. Include the default argument for the terminating character.

    It doesn't say to use getche() with cin.getline(). You are supposed to make a function that acts like cin.getline().

    You need to loop getche(character[counter]) until your term character is found in character[counter].

    while (character[counter] != termchar)
    {
    getche(....); // that is all I am giving you. It isn't my assignment

    ...
    }

    If that doesn't give you ideas, PM me and I will help you.
    Blue

  4. #4
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    i got it to work...
    I hope this is what I had to do..

    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    char ch = 'x';
    char termchar = 'q';
    char character[30];
    int counter = 0;
    while (ch != termchar )
    {
    ch = getche();
    character[counter++] = ch;
    }
    }

    thanks!
    Marc
    No matter how much you know, you NEVER know enough.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> It doesn't say to use getche() with cin.getline().

    Indeed. Using old C type I/O in the same dialog as C++ stream I/O on the same comms channel is fraught with problems due to the different ways they are buffered, (or in some cases not buffered at all). Use one or the other, not both.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    char ch = 'x';
    char termchar = 'q';
    char character[30];
    int counter = 0;
    while (ch != termchar )
    {
    ch = getche();
    character[counter++] = ch;
    }
    }


    You better double check. Try to cout character like

    cout << endl << character << endl;

    before you end main.

    Just because something compiles, doesn't mean that you are doing it right.
    Blue

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    and instead of specifying the termchar, why don't you ask for it. Also, you should consider making this a function that is utilized from main.

    mygetline(*char[], maxnumber, termchar);

    Then you would call mygetline and pass values to it. That would make it work exactly (pretty much) like getline();

    Betazep
    Blue

  8. #8
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Thumbs down

    You were right
    mmm I tried this...

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
    char ch = 'x';
    char termchar = 'q';
    char character[30];
    int counter = 0;
    while (ch != termchar )
    {
    ch = getche();
    character[counter++] = ch;
    }
    int q = strlen(character);
    for (int j=0; j<q; j++)
    cout << character[j];
    }

    And now, I donno what's happening.
    When I press q, the word i wrote comes...butright after it, loadz of funny characters come on the screen

    I donno what I'm doing wrong

    MMM anyway thanks
    Marc
    No matter how much you know, you NEVER know enough.

  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    #include <iostream>
    #include <conio>
    using namespace std;

    int main()
    {
    char character[10];
    char termchar ='#';

    int count=-1;

    while ((character[count] != termchar) && (count < 9))
    {

    count++;
    character[count] = getche();


    }
    if (character[count] == termchar)
    character[count] ='\0'; // overwrite last term char
    else
    character[count+1] = '\0'; // write null after last char
    cout << endl << character;

    return 0;
    }
    Last edited by Betazep; 12-21-2001 at 02:43 AM.
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with login thingy....
    By Huskar in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 06:23 PM
  2. Writing my own getche() function
    By Mavix in forum C Programming
    Replies: 4
    Last Post: 11-08-2006, 05:51 AM
  3. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM