pointers to strings

I have a character array of size 80 and I am trying to read an input with using cin.get() and a pointer like this:

SOMECLASS:
..

char string[80];

..

MAIN:

..

cin.get( myPointer->string, 80 );

..

Why wont this work? I know there is nothing wrong with the way i am using my pointer as it is used succesfully for other data.

Comments

  • :
    : cin.getline( myPointer->string, 80 );

  • : :
    : : cin.getline( myPointer->string, 80 );
    :
    :
    I am sorry, did you mean to write something? because I don't think it has worked.
  • : : :
    : : : cin.getline( myPointer->string, 80 );
    : :
    : :
    : I am sorry, did you mean to write something? because I don't think it has worked.
    :

    maybe your compiler doesn't like the variable name "string". That is the name of a c++ STL class and may be getting confused. Rename the variable to something else. Here is an example of using getline()
    [code]
    #include
    using namespace std;


    int main()
    {
    char strng[80];
    cout << "Enter something ";
    cin.getline(strng,80);
    cout << endl << strng << endl;
    cin.ignore();
    return 0;
    }
    [/code]
  • : maybe your compiler doesn't like the variable name "string". That is the name of a c++ STL class and may be getting confused. Rename the variable to something else. Here is an example of using getline()
    : [code]
    : #include
    : using namespace std;
    :
    :
    : int main()
    : {
    : char strng[80];
    : cout << "Enter something ";
    : cin.getline(strng,80);
    : cout << endl << strng << endl;
    : cin.ignore();
    : return 0;
    : }
    : [/code]
    :
    I still cant get to work, I changed the char string[80] to char myString[80] and then wrote cin.getline( myPointer->myString ) etc. I know there is nothing wrong with the pointer because if i was a integer or single char value it works fine. Is there another way to define a array of chars? maybe one which works better?
  • : : maybe your compiler doesn't like the variable name "string". That is the name of a c++ STL class and may be getting confused. Rename the variable to something else. Here is an example of using getline()
    : : [code]
    : : #include
    : : using namespace std;
    : :
    : :
    : : int main()
    : : {
    : : char strng[80];
    : : cout << "Enter something ";
    : : cin.getline(strng,80);
    : : cout << endl << strng << endl;
    : : cin.ignore();
    : : return 0;
    : : }
    : : [/code]
    : :
    : I still cant get to work, I changed the char string[80] to char myString[80] and then wrote cin.getline( myPointer->myString ) etc. I know there is nothing wrong with the pointer because if i was a integer or single char value it works fine. Is there another way to define a array of chars? maybe one which works better?
    :
    I do not use C++, only C, so I do not know how cin.getline works exactly; but Stober's code is certainly correct so you should post more of your code because the error must be elsewhere.

    Take care, Steph.
  • I have a class called myClass which just holds some variable like int myInt and char myString[ 80 ]. Then in another file I have my main function and here I create a pointer to myClass called myPointer. I read in a value for myInt like this cin >> myPointer->myInt; then I try to read in a string for myString like this cin.getline( myPointer->myString, 80 ); but it wont work. It works for strings like this "jndjnsdjfksjdnf" but not for strings like this "jher jhejrh jher" with spaces. What have I done wrong?
  • : I have a class called myClass which just holds some variable like int myInt and char myString[ 80 ]. Then in another file I have my main function and here I create a pointer to myClass called myPointer. I read in a value for myInt like this cin >> myPointer->myInt; then I try to read in a string for myString like this cin.getline( myPointer->myString, 80 ); but it wont work. It works for strings like this "jndjnsdjfksjdnf" but not for strings like this "jher jhejrh jher" with spaces. What have I done wrong?
    :

    we don't have our crystle ball today, so you will have to POST SOME CODE!
  • : I have a class called myClass which just holds some variable like int myInt and char myString[ 80 ]. Then in another file I have my main function and here I create a pointer to myClass called myPointer. I read in a value for myInt like this cin >> myPointer->myInt; then I try to read in a string for myString like this cin.getline( myPointer->myString, 80 ); but it wont work. It works for strings like this "jndjnsdjfksjdnf" but not for strings like this "jher jhejrh jher" with spaces. What have I done wrong?
    :

    If you are going to use C++, why not just use the string class? This way you don't have to worry about size and over running the array.

    cin.getline doesn't read whitespace afair, look up getline(cin,str)

    ie
    string str;
    getline(cin, str);
  • :
    : cin.getline doesn't read whitespace afair, look up getline(cin,str)
    :

    although I agree about using stl::string class, the example program I posted above reads the entire line, spaces and all. It's the cin.get() function that doesn't read past the white space.
  • Here is part of the main function:

    int main() {

    class myClass* myPointer;
    myPointer = new class myClass;

    cout << "Enter integer: ";
    cin >> myPointer->myInt;

    cout << "Enter string: ";
    cin.getline( myPointer->myString, 100 );
    .
    .
    .

    Here is my class

    class myClass {

    public:

    int myInt;
    char myString[100];

    };

    Do you see what I try to do? I test it again and the cin.getline is ok for any string unless there is a space BETWEEN the words. For example f then a space is ok, a space then r is ok but r space r will not work.

    What is the string class? I do not know this
  • The problem is that the key is still in the keyboard buffer after you enter the integer. You need to clear that out.
    [code]
    int main(int argc, char* argv[])
    {
    class myClass* myPointer;
    myPointer = new class myClass;

    cout << "Enter integer: ";
    cin >> myPointer->myInt;
    [red] cin.ignore();[/red]
    cout << endl;
    cout << "Enter string: ";
    cin.getline( myPointer->myString, 100 );
    cout << myPointer->myString << endl;
    cout << "Press <Return> to continue: ";
    cin.ignore();

    return 0;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories