String Input & Output

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

String Input & Output

 
0
  #1
Dec 16th, 2007
  1. // Stopping input with an empty string
  2. #include <iostream>
  3. using namespace std;
  4. #include <cstring>
  5.  
  6. int main()
  7. {
  8. int i = 1;
  9. char temp[80];
  10. char string[80]; // room for 255 strings
  11.  
  12.  
  13. cout << "\n\nEnter some strings - (blank to exit)"
  14. << " \nfirst string: ";
  15. cin.getline(temp, 80);
  16. while (temp[0] != '\0')
  17. {
  18. cout << "String " << i << " : " << temp << "\nnext string: ";
  19. cin.getline(temp, 80);
  20. string == strcat(string," ");
  21. string == strcat(string,temp);
  22. i++;
  23.  
  24. }
  25.  
  26. cout << "\n\n";
  27.  
  28. //for (int index = 0; index < i; index++)
  29. cout << string ;
  30.  
  31. }

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.
Last edited by nicz888; Dec 16th, 2007 at 9:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #2
Dec 16th, 2007
Last edited by nicz888; Dec 16th, 2007 at 10:16 pm. Reason: warning are gone now
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #3
Dec 16th, 2007
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 ??
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: String Input & Output

 
0
  #4
Dec 16th, 2007
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,
  1. strcpy( temp, "Hello" ); // temp[] contains "Hello"
  2. array[ 0 ] = temp;
  3. // array[ 0 ] points to "Hello" in temp[]
  4.  
  5. strcpy( temp, "world" ); // temp[] contains "world"
  6. // array[ 0 ] still points to temp[], but temp[] now
  7. // 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #5
Dec 16th, 2007
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?
Last edited by nicz888; Dec 16th, 2007 at 10:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #6
Dec 16th, 2007
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?
Last edited by nicz888; Dec 16th, 2007 at 10:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: String Input & Output

 
0
  #7
Dec 16th, 2007
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().
Last edited by Duoas; Dec 16th, 2007 at 10:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #8
Dec 16th, 2007
thanks. i got this part of my code working.
Last edited by nicz888; Dec 16th, 2007 at 10:29 pm. Reason: got it
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: nicz888 is an unknown quantity at this point 
Solved Threads: 0
nicz888 nicz888 is offline Offline
Light Poster

Re: String Input & Output

 
0
  #9
Dec 16th, 2007
can i post a different question on the same thread?
Last edited by nicz888; Dec 16th, 2007 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: String Input & Output

 
0
  #10
Dec 17th, 2007
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC