Discussion in 'Mac Help/Tips' started by newmanium, Apr 30, 2002.

  1. newmanium macrumors member

    Jan 18, 2002
    being stumped using project builder for my c++ programming. i need to read strings, and the most i can get from the iostream library are the commands "cin.get('variable')" and "cin.getline('variable', '# of char', 'when to stop')" but cant get the commands to work. not on the project builder nor the terminal. any sugggestions? :(
  2. PCUser macrumors regular

    Mar 1, 2002
    By "string", do you mean a C string (char*), or a C++ string (string)? The iostream library's cin.get() and cin.getline() functions use C strings.

    What do you mean by "cant get the commands to work"? Here's a small sample code that reads in a line (with up to 1000 characters) and displays it. It compiles in Linux (using GCC 3.0.4) and Windows (using GCC 3.0.3). I don't have a mac, so I don't know 100%, but this is using the standard C++ libraries...

    //begin code
    #include <iostream>

    using namespace std;
    int main()
    char bob[1000];

    cin.getline(bob, 1000);
    cout << bob;

    return 0;
    //end code

    Help at all?
  3. newmanium thread starter macrumors member

    Jan 18, 2002
    i declare my variable as:

    char choice[25];

    and the code goes like:

    cout<< "please enter your selection: ";
    cin.getline(choice, 25, '\n')

    /*the '\n' signifies it to stop reading when the user presses <enter> */

    it compiles fine under project builder and the terminal but when i run the code, the prompt doesn't wait for any input and exits the program. i know this code works under MS visual studio but cant seem to make it work here.
  4. newmanium thread starter macrumors member

    Jan 18, 2002

    just tried redeclaring the variable, set that badboy to 1000 and it worked. thanks for your help

Share This Page