cin.getline problem...

This is a discussion on cin.getline problem... within the C++ Programming forums, part of the General Programming Boards category; Hello everybody, here is one of the function definition in my c++ program... Unfortunately, it doesn't work...(It compile but it ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    cin.getline problem...

    Hello everybody,

    here is one of the function definition in my c++ program...
    Unfortunately, it doesn't work...(It compile but it won't let me to enter the name.)

    void ListMemberName(Member arr[], int length1)
    //Purpose: List member by name
    //Pass by Value: length1
    //Pass by Reference: arr[]
    {
    char target[30];
    int ctr=0;
    int found = 0;

    cout<<"Enter name of the member"<<endl;
    cin.getline(target,30,'\n');

    while ((ctr<length1)&&(found==0))
    {
    if (strcmp(target, arr[ctr].name)==0)
    {
    found=1;
    cout<<arr[ctr].num<<' '<<arr[ctr].gend<<' '<<arr[ctr].name<<endl;
    }
    else
    ctr++;
    }
    if (found==0)
    cout<<"Invalid input"<<endl;
    }

    I'm pretty sure it is the line "cin.getline(target,30,'\n');" that cause the problem.
    Since it required to enter a full name(have space), therefore i use cin.getline instead of cin>>.

    Thanks

  2. #2
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    i think you have to derefrence the name of the array like this

    if (strcmp(*target, arr[ctr].name)==0)

    hope it helps
    Programming is a high logical enjoyable art for both programer and user !!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (strcmp(*target, arr[ctr].name)==0)
    That won't work, strcmp() wants to pointers as it's parameters.

    davidvoyage200: I suggest you do some debugging. If you don't have a "proper" debugger to hand, try just writing out to the screen the strings that are being compared, just to see if that process is working as you think.

    Also, please read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    thanks for replying,

    yes, i am using visual c++ to compile and debug it, there are no syntax error, but the fucntion just not allow me to type the name. (before i enter the name, the program quickly display "Invalid Input".), therefore i don't even have time to type the name...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    sorry people,

    i typed my code with tags, but unfortunately, when i copy paste my code, and then post it, it automatically became "non-tags".

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by davidvoyage200
    thanks for replying,

    yes, i am using visual c++ to compile and debug it, there are no syntax error, but the fucntion just not allow me to type the name. (before i enter the name, the program quickly display "Invalid Input".), therefore i don't even have time to type the name...
    ... then there is probably some data left in the input buffer, maybe just a newline character from a previous input call.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    ok, here is my complete program with the input file.

    the program have trouble when "list member by name"
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    and this is my input file
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    the password of the program is "geek"

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>the program have trouble when "list member by name"
    On my system, the program has trouble even compiling.
    Code:
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    E:\junk1.cpp:
    Error E2015 junk1.cpp 14: Ambiguity between 'string' and 'std::string'
    Error E2015 junk1.cpp 34: Ambiguity between 'string' and 'std::string' in function main()
    Error E2451 junk1.cpp 328: Undefined symbol 'i' in function RemoveMember(Member *,int &)
    Error E2285 junk1.cpp 328: Could not find a match for 'strcpy(undefined,char *)' in function RemoveMember(Member *,int &)
    *** 4 errors in Compile ***
    I'll have a look and post again soon.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    cin >> number;
    When you do this it can leave a newline character in the buffer. I expect this is what is causing your problem.

    In this loop:
    while ((ctr < length1) && (found == 0))
    put this line:
    cout <<"Comparing >" <<target <<"< with >" <<arr[ctr].name <<"<" <<endl;

    This will display exactly what is being compared for each iteration of the loop. You should be able to determine the problem from here. Have fun debugging
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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