Need help with Strings

What's the best way to use a string in C++? I use char*;
the problem is that I always get a run-time error when
trying to input a string:

char* string;
cout<<"...";
cin>>string; //ERROR: "can't load into some mem address" ??

Comments

  • Hi !!

    U need to allocate memory before using cin. Since memory has not been allocated to the variable therefore when cin in used it gives an error.

    Regards
    Shaunak
  • : Hi !!
    :
    : U need to allocate memory before using cin. Since memory has not been allocated to the variable therefore when cin in used it gives an error.
    :
    : Regards
    : Shaunak
    :

    [blue]here's an example[/blue]
    [code]
    char *string = malloc(255);
    cin >> string;

    or anoter example

    char string[255];
    cin >> string;
    [/code]
  • : : Hi !!
    : :
    : : U need to allocate memory before using cin. Since memory has not been allocated to the variable therefore when cin in used it gives an error.
    : :
    : : Regards
    : : Shaunak
    : :
    :
    : [blue]here's an example[/blue]
    : [code]
    : char *string = malloc(255);
    : cin >> string;
    :
    : or anoter example
    :
    : char string[255];
    : cin >> string;
    : [/code]
    :

    What's "Malloc"? I Forgot to say that I also did this:

    char* string = ""; //isn't this allocated?
    cin>>string;

    I saw that you could also define it as a static array, but that's dumb; you'd only be able to enter so many chars in a string!
    Isn't there a way to make it as many as you want? what is that "malloc" thing, are you giving it an address? isn't that the job of the compiler?

    Do you know what are some good includes for string manipulation?
    (I used to know C++, but we used the apclasses and I didn't have to deal with real C++ strings; now that I'm all used to java, some of this C++ stuff is weird to me)
  • : : : Hi !!
    : : :
    : : : U need to allocate memory before using cin. Since memory has not been allocated to the variable therefore when cin in used it gives an error.
    : : :
    : : : Regards
    : : : Shaunak
    : : :
    : :
    : : [blue]here's an example[/blue]
    : : [code]
    : : char *string = malloc(255);
    : : cin >> string;
    : :
    : : or anoter example
    : :
    : : char string[255];
    : : cin >> string;
    : : [/code]
    : :
    :
    : What's "Malloc"? I Forgot to say that I also did this:
    :
    : char* string = ""; //isn't this allocated?
    : cin>>string;
    :
    : I saw that you could also define it as a static array, but that's dumb; you'd only be able to enter so many chars in a string!
    : Isn't there a way to make it as many as you want? what is that "malloc" thing, are you giving it an address? isn't that the job of the compiler?
    :
    : Do you know what are some good includes for string manipulation?
    : (I used to know C++, but we used the apclasses and I didn't have to deal with real C++ strings; now that I'm all used to java, some of this C++ stuff is weird to me)
    :
    [blue][b]malloc(255)[/b] will return a pointer to the new memory block of 255 bytes (characters) in size.[/blue]
  • : :
    : : char* string = ""; //isn't this allocated?
    : : cin>>string;
    : :

    [blue]Well, yes and no. It sets the pointer string to point to some memory location of one byte. That is not what you want for cin. You need string to point to some memory block that is big enough to hold all the characters that you will enter at the keyboard. [/blue]
  • Okay, thanks; I will try that.

    I am writing a compiler...it makes my head hurt...a few months of thinking, discussing, writing algorithms, arguing over dumb things...
    I think that it will turn out well; a high-level programming language for z80 calcs will be great, even before we get the OOP working :)

    by the way, where are some good string manipulation libs?
    how can you tell the LENGTH of a string?
  • : Okay, thanks; I will try that.
    :
    : I am writing a compiler...it makes my head hurt...a few months of thinking, discussing, writing algorithms, arguing over dumb things...
    : I think that it will turn out well; a high-level programming language for z80 calcs will be great, even before we get the OOP working :)
    :
    : by the way, where are some good string manipulation libs?
    : how can you tell the LENGTH of a string?
    :
    [purple]
    strlen(a_string)?
    a_string.length()?
    [/purple]
  • : Okay, thanks; I will try that.
    :
    : I am writing a compiler...it makes my head hurt...a few months of thinking, discussing, writing algorithms, arguing over dumb things...
    : I think that it will turn out well; a high-level programming language for z80 calcs will be great, even before we get the OOP working :)
    :
    : by the way, where are some good string manipulation libs?
    : how can you tell the LENGTH of a string?
    :

    all strings in c and c++ terminate with a 0 byte. So if you have a string "Hello world", the byte immediately following the last letter 'd' will be 0. Now all you have to do is count the number of characters up to the byte with a 0.

    This is not the case in VB or some other languages, nor are UNICODE strings (BSTRs) treated that way.

    Better way is to use strlen() (as previously posted by Shkaboinka).



  • : all strings in c and c++ terminate with a 0 byte. So if you have a string "Hello world", the byte immediately following the last letter 'd' will be 0. Now all you have to do is count the number of characters up to the byte with a 0.
    :
    : This is not the case in VB or some other languages, nor are UNICODE strings (BSTRs) treated that way.
    :
    : Better way is to use strlen() (as previously posted by Shkaboinka).
    :
    :

    No, I asked the question, as previously posted by that other person...

    where is that function? string.h? you can't asume I know these things...
    also, how can you tell if an ifstream has successfully opened a file? I thought it was ifstream.fail(); the problem is that it CREATES files that it can't find..how do I prevent this / delete files?
    The same for ofstreams...?

  • : where is that function? string.h? you can't asume I know these things...
    [blue]yes, they are defined in string.h[/blue]

    : also, how can you tell if an ifstream has successfully opened a file? I thought it was ifstream.fail();
    [blue]use ifstream.is_open(), which returns either true or false[/blue]

    the problem is that it CREATES files that it can't find..how do I prevent this / delete files?
    : The same for ofstreams...?

    [blue]Here is some information that will help explain all that. click on the open link [/blue]

    http://www.cplusplus.com/ref/iostream/fstream/

    :
    :

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