Cin.getline() truncates a letter

This is a discussion on Cin.getline() truncates a letter within the C++ Programming forums, part of the General Programming Boards category; When I use cin.getline() on the same variable for the second time it truncates the first char, when i type ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Cin.getline() truncates a letter

    When I use cin.getline() on the same variable for the second time it truncates the first char, when i type "hello" as the first command, it receives "hello", but when I type it as the second command, it receives only "ello". Why?
    Sorry for my bad english :P

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It works fine with cin>>variable but why doesnt it work with cin.getline? I like cin.getline, because i can use spaces there.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    if you are getting string's
    which i hope you are, because
    you are posting in c++, you should use


    getline( cin, string, '\n' );

    where string is your string varaible
    and '\n' being newline is your delimiter
    these require you include <string> header
    of course.


    an example

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         string mystring;
         cout << "enter your full name" << endl;
         getline( cin, mystring, '\n' );
    
         cout << mystring << endl;
         cin.get();
         return 0;
    }

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I used <cstdlib>,<iostream>,<cstring>,<windows.h> headers

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    then you should be posting in C, because you are not
    using C++ in term of what C++ has to offer.


    and what are you using the header <cstdlib>
    for? system("pause"); ?
    you shouldnt be doing that either.

    you should take it upon yourself to use the advantage of the
    language. if C is your langauge then you should take it upon
    yourself to post in the right forum.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Where the hell should I know that those are c include files? I just started c++ 2 days ago

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    exactly, not all of those header are C, but you can usually
    account for it being C when you see a C infront of the header
    name. anyways my point in all this is post some code next
    time. specially if your unsure, because without somethign to look
    at people will assume that you want c++ since you posted here.


    your next step then would be, post your code, dont go to the c
    section and repost this you will just further upset people.

    im not trying to be hard on you but when your not helping
    its kinda hard to get point across without sounding mean.

    i want to help and i will help you when i have somethign to help
    you with.

    since you said "i started c++ 2 days ago" then it would be best
    that you take my post of code udner consideration, test it, apply
    it to your problem, if it does what you want then you will have
    more information in your coding arsenal.
    Last edited by ILoveVectors; 08-22-2005 at 01:24 AM.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by ILoveVectors
    exactly, not all of those header are C, but you can usually
    account for it being C when you see a C infront of the header
    name. anyways my point in all this is post some code next
    time. specially if your unsure, because without somethign to look
    at people will assume that you want c++ since you posted here.
    http://www.cplusplus.com/doc/ansi/hfiles.html

    <iostream> is standard C++, and if you wanted to use any functions from the C headers, you would use the ANSI-C++ versions. <cstdlib><cstring> are meant for C++ and valid to use, the .h's being possibly deprecated. <windows.h> is the only choice for C or C++, and has features both C and C++ commonly use, less common in <cstdlib><cstring>, but still has uses.

    Its pretty obvious maxorator is using C++ from the first post (using cin), and even more by the second (using iostream, not iostream.h, etc.). Headers without .h's are C++, so yeah I'd say this is a C++ post. It may be possible that hes using c-style strings instead of the string variable, but from my tests it hasnt given me problems either way with cin.getline().

    Anyway, although string's are C++ specific, c-style strings arent just for C, so using them in C++ is fine, and I'm only assuming its necessary later when working with ports and buffers.. but thats a just a guess. I don't know that part yet.

    maxorator: I've tried c-style strings and string using cin.getline() using the same variable twice and such, but unable to mirror the effects you had, both versions were fine. It sounds like a buffer problems. I agree with ILoveVectors though that you should post the code, its not a lot of code, and itd help solve the problem quickly.. especially since you arent very good at English.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
        #define PORT 3490 // the port client will be connecting to 
    
        #define MAXDATASIZE 100 // max number of bytes we can get at once 
    
    using namespace std;
    int zoneaddr(){
    return 0;
    }
    int info(){
    return 0;
    }
    int access(){
    }
    int file(){
    }
    int main(int argc, char *argv[])
    {
        cout<<"This program is made by MaXoRaToR!\nEnter the correct password:\n";
        char pow[200];
        char now[200];
        int bow=0;
        cin.getline ( pow, 200, '\n' );
        if ( strcmp ( pow, "thepassword" ) == 0 ){
        cout<<"Welcome! To see, what this program offers, use info command.\n";
        while(strcmp ( now, "end" ) != 0){
        if(bow=1){
        }
        cin.getline(now,200,'\n');
        if (strcmp(now,"zone") == 0){
        zoneaddr();
    }
    else if(strcmp(now,"info") == 0){
         info();
         }
    else if(strcmp(now,"access") ==0){
    access();
    }
    else if(strcmp(now,"file") ==0){
    file();
    }
    
    else if(strcmp(now,"end") != 0 ){
    cout<<"Maxorator does not know a command named "<< now <<"\n";
    }
    if( strcmp ( now, "end" ) == 0 ){
    cout<<"Bye-bye!";
    Sleep(1000);
    }
    else{
        cin.get();
    } 
    bow=1;
        }}
        else{
        cout<<"Sorry, wrong password!";
        Sleep(1000);
        }
        return EXIT_SUCCESS;
    }
    Now when I enter a command right after logging in, it gets it correctly, after that it doesnt count the first char. Test it... I removed the function content, because they were long and contained only cout and sleep.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Just wondering, are you by any chance using the Microsoft Visual C++ 6 compiler?

    There is a getline Template Function Reads Extra Character bug that might be the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I am using Dev-C++ Compiler

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So how can I fix that extra character problem?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Oh, sorry about that. Your mistake is quite simple really.
    Code:
    if( strcmp ( now, "end" ) == 0 ){
    cout<<"Bye-bye!";
    Sleep(1000);
    }
    else{
        cin.get();
    }
    Why do you use cin.get()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oops!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I ...
    By geekrockergal in forum C Programming
    Replies: 21
    Last Post: 02-07-2009, 10:40 AM
  2. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 10:18 PM
  3. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  4. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 04:01 PM
  5. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM

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