How do you clear a string in a single statement ?
for egs. i want to clear name[20] of all its contents and leave it completely empty

Please help :-/

Recommended Answers

All 16 Replies

How do you clear a string in a single statement ?
for egs. i want to clear name[20] of all its contents and leave it completely empty

Please help :-/

const char *str="SpS";
  cout << str;
  str = NULL;

It's a character string? Just set the first character to be a terminator and it'll be treated as "".

name[0] = '\0';

If it's a C++ string you can call the clear method and that truncates the string to be "".

name.clear();

Are you talking about C++ strings or C-style char arrays? (I assume you're talking about char arrays, based on your post)

What do you mean by "completely empty"?

You can make a C++ string blank by doing this

std::string name = "hello";
std::cout << name;

//Assign 'name' a blank string
name = ""; 
std::cout << name;

for a C-style char array, you could do this

char name[20] = "fred";
std::cout << name;

//If the first character is a null terminator, the string is 'empty'.
name[0] = '\0' ;
std::cout << name;

Or do you wish to iterate through every element and set each one to zero? (You can do that with a loop, although there's not much point.)

commented: this is the most helpful one +0

im using turbo c++
and none of the methods that u guys have mentioned seem to work :'(


name[20] is a character array

Member Avatar for iamthwee

Please post the code you have so far...

Okay then, what are you doing exactly?

How do you clear a string in a single statement ?
for egs. i want to clear name[20] of all its contents and leave it completely empty

char name[20];
   memset(name, 0, sizeof name);

Although typically clearing all elements of a string is unnecessary.

thanx a lot !!!:icon_cheesygrin:
memset works .

Member Avatar for iamthwee

You should have posted the code for the others you have tried and the error messages or problems you encountered. The problem is unlikely to be with their code but rather how you typed it in or if you're using the outdated version of turbo c, which is probably very likely :)

where can i get the latest version of turbo c++

Member Avatar for iamthwee

Personally, anything with the word turbo c in makes me cringe, although admittedly the latest version is supposed to confirm to the standard, so it is actually ok to use.

But I'd recommend using dev-cpp. It's a small download at only 10MB and comes with loads of options, open-gl, native win32 GUI support for windows etc.

You can get it here:
http://www.bloodshed.net/devcpp.html

I think the dev-cpp project is dead. If you use it then you won't get any updates or support unless you want to do it all yourself. Code::blocks is supposed to be the successor to it from what I hear.

The problem is unlikely to be with their code but rather ... you're using the outdated version of turbo c, which is probably very likely :)

So you claim that the outdated versions of C/C++, like Turbo, never handled strings correctly? :icon_rolleyes: Or is it that after new versions came out, the older versions simply wore out and developed bugs?

If you're going to continue to slam older compilers, at least use reasons that make sense...

commented: B I N G O! +11

So you claim that the outdated versions of C/C++, like Turbo, never handled strings correctly? :icon_rolleyes: Or is it that after new versions came out, the older versions simply wore out and developed bugs?

If you're going to continue to slam older compilers, at least use reasons that make sense...

A slant I took on this is that the std namespace didn't exist and as such standard C++ is not supported. That is, there was no std::string , so using standard strings in an outdated compiler may have proved problematic. YMMV.

commented: Don't ya just love the pedants :) +11

Dave, at least you have a viable reason. Most other reasons I see given here is simply because it's old. Not a valid reason IMAO.

My preference would be to stop telling people "your compiler's old, so upgrade to [my personal favorite] compiler." If that's what they are using, let them use it unless it's really crap (like Digital Mars).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.