// Stopping input with an empty string
#include <iostream>
using namespace std;
#include <cstring>

int main()
{
    int i = 1;
    char temp[80];
	char string[80]; // room for 255 strings
	

    cout << "\n\nEnter some strings - (blank to exit)"
         << " \nfirst string: ";
    cin.getline(temp, 80);
    while (temp[0] != '\0')
    {
        cout << "String " << i << " : " << temp << "\nnext string: ";
        cin.getline(temp, 80);
        string == strcat(string," ");
		string == strcat(string,temp);
		i++;

    }

    cout << "\n\n";
    
	//for (int index = 0; index < i; index++)
    cout << string ;

}

i updated the code

input: bill loves to eat
output: $ loves to eat

$ is a weird character that show up. i don't know what it is.

Recommended Answers

All 15 Replies

:)

i notice that when i change the value in line 10.
string[x]

the output will change.
i input bill loves to eat
my output is @ loves to eat ??

You misunderstand something about pointers.

"temp" is an array of 80 characters. There is now space reserved in memory for 80 characters. The name "temp" by itself is a pointer to the first character in the array.

"array" is an array of 255 pointers to characters. There is now space reserved in memory for 255 pointers (not characters). The name "array" by itself is a pointer to the first pointer in the array. (BTW, don't name things "array".)

So, if you say strcpy( temp, "Hello" ); then the temp[] array contains the string "Hello".
If you then say strcpy( temp, "world" ); then the temp[] array contains the string "world". The string "Hello" is overwritten, and no longer exists.

In your loop, you assign array[ n ] (a pointer to a character) the address of the first char in the temp array. So,

strcpy( temp, "Hello" );  // temp[] contains "Hello"
array[ 0 ] = temp;
// array[ 0 ] points to "Hello" in temp[]

strcpy( temp, "world" );  // temp[] contains "world"
// array[ 0 ] still points to temp[], but temp[] now
// contains "world"

Your loop is failing because "i" starts at one when it should start at zero. In other words, when you increment "i" you are saying that there is one more item in the "array" than there really is.

Hope this helps.

[EDIT] You know, it really is difficult to help when you change things after posting.
I'll check again after you've made up your mind.

sorry about changing my original post

i think i fix the first problem i have and now this weird character that is showing up, how do i fix that?

i notice that i can change both line 20 and 21 to
strcat_s(string, " ");
and
strcat_s(string, temp);
that will get rid of all the warnings.

and the result is the same.
but my "bill" is still not showing up, why?

char string[80]; only creates an array of 80 characters. The array is named "string".

There is a problem with the order in which you are doing things. You get another string from the user before you copy "bill" to the "string" array.

The weird character is because you did not initialize your "string" array properly. You are lucky the program didn't crash.

Somewhere at the top, say line 11 or so, say string[0] = '\0'; Hope this helps. :)

PS. Don't name arrays "string" either. Come up with a real name.

[EDIT] Oh yeah, strcat_s() is non-standard MS crap. If you are only going to use the MS compiler, then fine, else stick with strcat().

thanks. i got this part of my code working.

can i post a different question on the same thread?

You mean another different question? ;) (Kind of like "Can I ask a question?" heh heh heh...)

I don't mind personally, but I'm not sure what the convention is here...

:)

What was that post? A bump? Never bump your messages. Someone will get to you when they log in.

string == strcat(string," ");
		string == strcat(string,temp);

What do you think these lines are doing? I'll bet it's not what you expect....

$ is a weird character that show up. i don't know what it is.

It's called a dollar sign. Hex value 24, decimal 36.

Be more careful with your formatting

1)so, my final string looks like this
bill loves to eat
2)and now i need to qsort them
i have been giving a set of qsort codes
but their initial declaration for the needed to pass argument is like this:

char *stringsB[] = { "This","is","a","test"};

and the qsort function looks like this:
qsort(stringsB, strings_len, sizeof(char *), cstring_cmp);

so how do i make my string into this form(like stringsB[])?

i try it and i can't do this:
char *stringsB[] = string; <--- no good

or is there a way i can break down my string into this
string = {"bill","loves","to","eat"};

i need the char *stringsB[] to point to my string.
so the qsort function will sort my string.

i dont know if this is clear.

sorry about my language.

thanks

how can i break down my STRING into this format?

char *stringsB[] = { "This","is","a","test"}?

so i can use the qsort function.

here is the qsort function and it takes no argument

void sort_cstrings_example()/* sorting C-strings array using qsort() example */
{
    char *strings[] = { "Zorro", "Alex", "Celine", "Bill", "Forest", "Dexter" };
    size_t strings_len = sizeof(strings) / sizeof(char *);

how can i pass mySTRING to this function so (line 3)*strings[] will be mySTRING?

can i do this

void sort_cstrings_example(char inputString)
{
char *strings[];
strings=inputString;
...
...
}
?

Use an istringstream to parse the line into words delimited by whitespace. I suppose you could use strok() or similar protocol as well if you wanted.

Here's one version using STL strings as opposed to C style strings.

WARNING: code snippets posted below not tested

string inputString = "This is a test";
vector<string> words;
istringstream iss(inputString);
string temp;
while(iss >> temp)
  words.push_back(temp);

You can change the appropriate syntax to use C style strings while retaining the basic protocol. Using C Style strings it might look something like this:

char inputString[80];
char words[80][80];  //this is essentially the same of char *words[80]; and can be used whereever you wanted to use char *[];
int index = 0;
istringstream iss(inputString);
while (iss >> words[index])
   ++index;

I don't quite understand your question....You required to break a statement, right?

E.g:
This is just a test
-After break the whole string-
This
is
just
a
test

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.