How can I clear a string? and not just by making all the characters spaces.
Thanks
How can I clear a string? and not just by making all the characters spaces.
Thanks
Do you mean like the whole screen?? If so then clr() will do it!Originally posted by Jperensky
How can I clear a string? and not just by making all the characters spaces.
Thanks
"Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
-Bruce Lee
Or,
This assumes, of course, that your string variable is named 'str'. Now, if it's named 'Rover'...Code:std::string str = "Now is the time..."; std::cout << str << std:endl; str = " ";
Note that your compiler will probably insist that you put a 'space' character between the quotes.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
Assuming this is a std::string you can simply say:
str.erase();
If it's a C string you'll have to define "empty" better, for most uses doing this will cause the string to become empty:
str[0] = '\0';
-Prelude
My best code is written with the delete key.
Hi,
Do you mean a string std::string or an array of char's?
To clear an array of char's so that C sees an empty string just make the first element 0(zero).
char mystr[20] = "this is a test";
printf("\n%s",mystr); //prints this is a test
mystr[0] = 0;
printf("\n%s",mystr); //prints only the '/n'
Hope this helps.
Pappy
You learn something new everyday.
its generally better to assign the element of the string to NULL rather than zero
Monday - what a way to spend a seventh of your life
>its generally better to assign the element of the string to NULL rather than zero
If you mean the nul character '\0' then I agree. If you mean the NULL macro defined in cstdio I would like to know why you think so.
-Prelude
My best code is written with the delete key.
Look at the definition of NULL (_null.h) and you'll be surprised .Originally posted by iain
its generally better to assign the element of the string to NULL rather than zero
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.
I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.
*Example*
The text file contains:
this is line one
and line 2
also 3
I am getting this when I print it to screen:
this is line one
and line 2 one
also 3ne 2 one
Anyone have a solution?
There is a function called strset() defined in string.h.
e.g.#include<string.h>
strset(samplestring,0);
This will set all characters in the string samplestring to the NULL character with the ASCII value 0.
You copy the string into the buffer one char at a time?Originally posted by Jperensky
I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.
I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.
*Example*
The text file contains:
this is line one
and line 2
also 3
I am getting this when I print it to screen:
this is line one
and line 2 one
also 3ne 2 one
Anyone have a solution?
That explains it. After you have copied all characters, add a '\0' character last. This is a NULL terminator, meaning the string ends there (not showing up the rest of the junk from the previous string).
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
When I append a '\0' char to the end of the array all I can see is the last char in the string... it is the correct char though.
Source:
Code:#include <iostream.h> #include <fstream.h> #include <stdio.h> #include <string.h> void main() { ifstream OpenFile("text.txt"); char ch, temp_str[50]; int cord = 0, reset = 0; while(!OpenFile.eof()) { OpenFile.get(ch); if (ch == '\n') { cord++; temp_str[cord] = '\0'; cout<<temp_str<<"\n"; } else { temp_str[cord] = ch; cord++; } reset = 0; cord = 0; } OpenFile.close(); cout<<temp_str; }
You set the index to 0 in every loop, so you will never get further than the first element.
Originally posted by Jperensky
Code:OpenFile.get(ch); if (ch == '\n') { cord++; temp_str[cord] = '\0'; cout<<temp_str<<"\n"; } else { temp_str[cord] = ch; cord++; } reset = 0; cord = 0;
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
You said I set the index to '\0'. I set the last char in the string to '\0' as I was instructed to do in a previous post. How should I do it?
You should move the cord = 0; to the if statement:
If you read a newline character you display the string and reset the index (cord) to 0. Now you can read the next line starting at the beginning of your string.Code:if (ch == '\n') { cord++; temp_str[cord] = '\0'; cout<<temp_str<<"\n"; cord = 0; } else { temp_str[cord] = ch; cord++; } reset = 0;