954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String Input & Output

// 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.

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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 ??

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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?

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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?

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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().

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

thanks. i got this part of my code working.

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

can i post a different question on the same thread?

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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...

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 
:)


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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

how can i break down my STRING into this format?

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

so i can use the qsort function.

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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;
...
...
}
?

nicz888
Light Poster
49 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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;
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

neosomosis
Light Poster
37 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You