C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need help with Strings Posted by Shkaboinka on 14 Jan 2004 at 8:44 PM
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" ??
Report
Re: Need help with Strings Posted by shaunak on 14 Jan 2004 at 9:50 PM
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
Report
Re: Need help with Strings Posted by stober on 15 Jan 2004 at 4:39 AM
: 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
:

here's an example
char *string = malloc(255);
cin >> string;

or anoter example

char string[255];
cin >> string;

Report
Re: Need help with Strings Posted by Shkaboinka on 15 Jan 2004 at 7:41 AM
: : 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
: :
:
: here's an example
:
: char *string = malloc(255);
: cin >> string;
: 
: or anoter example
: 
: char string[255];
: cin >> string;
: 

:

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)
Report
Re: Need help with Strings Posted by AsmGuru62 on 15 Jan 2004 at 7:51 AM
: : : 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
: : :
: :
: : here's an example
: :
: : char *string = malloc(255);
: : cin >> string;
: : 
: : or anoter example
: : 
: : char string[255];
: : cin >> string;
: : 

: :
:
: 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)
:
malloc(255) will return a pointer to the new memory block of 255 bytes (characters) in size.
Report
Re: Need help with Strings Posted by stober on 15 Jan 2004 at 8:16 AM
: :
: : char* string = ""; //isn't this allocated?
: : cin>>string;
: :

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.
Report
Re: Need help with Strings Posted by Shkaboinka on 15 Jan 2004 at 1:37 PM
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?
Report
Re: Need help with Strings Posted by Extrasolar on 15 Jan 2004 at 1:53 PM
: 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?
:

strlen(a_string)?
a_string.length()?

Report
Re: Need help with Strings Posted by stober on 15 Jan 2004 at 2:15 PM
: 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).



Report
Re: Need help with Strings Posted by Shkaboinka on 15 Jan 2004 at 2:23 PM
: 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...?

Report
Re: Need help with Strings Posted by stober on 15 Jan 2004 at 3:39 PM
: where is that function? string.h? you can't asume I know these things...
yes, they are defined in string.h

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

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

Here is some information that will help explain all that. click on the open link

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

:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.