Pointer and Cin Problem

Discussion in 'Mac Programming' started by bobber205, Apr 9, 2007.

  1. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #1
    Below is what I use in Visual studio. Using getline I can have names entered that have spaces etc. If there is a space and I use cin, I get an infinite loop. (other code doesn't matter, it's just a function call.

    All variables have been declared ok. What do I use insteasd of clear buffer etc on OS X?

    Code:
    void GetString(char* str) {
    
        cout << "Please Enter a String: " << endl;
        cin >> str;
       // cin.ignore(cin.rdbuf()->in_avail());
        //cin.getline(str, 100);
       // cin.clear();
        //cin.ignore(cin.rdbuf ()->in_avail());
    
        cout << endl;
    
    }
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Not sure I understand exactly, but getline works fine for reading spaces:

    Code:
    void GetString(char* str)
    {
    	cout << "Please Enter a String: " << endl;
    	cin.getline(str, 100);
    	cout << endl;
    }
     
  3. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #3
    But when I try to run that function, it skips completely over my whole function call. The line "Enter your string" is printed, but I am never prompted to enter anything. It skips right along to printing the menu again. :(
     
  4. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    Try clearing the input buffer before you "cout".

    Todd
     
  5. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #5
    Hmmm... If I remember correctly the input needs to be put to a variable.

    Just using cin or it's subfunctions doesn't do anything because it has nowhere to put it. Or at least that's what I'm getting from the code.

    Steve
     
  6. macrumors 65816

    Joined:
    Jan 6, 2004
    #6
    Try:

    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
     

Share This Page