little help :(

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

Join Date: Nov 2006
Posts: 11
Reputation: d1e9v85 is an unknown quantity at this point 
Solved Threads: 0
d1e9v85 d1e9v85 is offline Offline
Newbie Poster

little help :(

 
0
  #1
Jan 25th, 2007
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. #include <string>
  5. using std::string;
  6. using std::getline;
  7.  
  8.  
  9. int main ()
  10. {
  11. char string1[256];
  12. char string2[256] ;
  13.  
  14. cout << "\nPlease enter the first array string: \n"<<endl;
  15. gets (string1);
  16. cout << "\nPlease enter the second array string: \n"<<endl;
  17. gets (string2);
  18. int length1 = strlen( string1 );
  19. int length2 = strlen( string2 );
  20.  
  21. if ( ( strlen( string1 )) > ( strlen( string2 )) )
  22.  
  23. {
  24. for ( int i = 0; i < length2; i++ )
  25. {
  26. cout<<" "<< *(string1 + i)<< " " << *( string2 +i);
  27. }
  28.  
  29. }
  30.  
  31. if ( ( strlen( string1 )) < ( strlen( string2 )) )
  32.  
  33. {
  34. for ( int j = 0; j < length2; j++ )
  35. {
  36. cout<<" "<< *(string1 + j) << " " << *( string2 +j);
  37.  
  38.  
  39. }
  40. }
  41.  
  42. if ( ( strlen( string1 )) == ( strlen( string2 )) )
  43.  
  44. {
  45. for ( int k = 0; k < length2; k++ )
  46. {
  47. cout<<" "<< *(string1 + k) << " " << *( string2 +k);
  48.  
  49.  
  50. }
  51. }
  52. return 0;
  53. }


basically what i want as output is

for example if string1 is : hello

and string2 is : bye

the output should be : h b e y l e l o


this program basically does what i want it to do
but.

but the thing is that say the string1 is longer than string 2

fir the same example

it prints something like this

: h b e y l e l & o &

where & is a garbage value



how can i get rid of those garbage values
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: little help :(

 
0
  #2
Jan 25th, 2007
I see some bad programming habits.
http://www.cprogramming.com/tips/sho...ount=30&page=1
(one thing that this tip doesn't mention: if you're using C++, you should be using getline().)

A clearer idea of what the program is trying to do would also be helpful. I'm only guessing, but it sort of looks like you're trying to merge strings with spaces in between. So what are you trying to do when a string is shorter than the other? Of course you're going to get junk filled in. What did you expect?

You might also want to use string instead of c-strings, as you're using C++.
Last edited by John A; Jan 25th, 2007 at 12:29 am.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,826
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: little help :(

 
0
  #3
Jan 25th, 2007
First of all: I agree with Joe, you should use C++ strings. But if you want to use char[], I would rewrite the program like this:

(pseudocode)

  1.  
  2. int length1= strlen(string1) - 1, length2=strlen(string2) - 1;
  3. int i1 = 0, i2 =0;
  4. Loop while counter <= length1+length2
  5. {
  6. if (i1 <= length1)
  7. {
  8. cout << string1[i1];
  9. i1++;
  10. }
  11. if (i2 <= length2)
  12. {
  13. cout << string2[i2] ;
  14. i2++;
  15. }
  16. else break;
  17.  
  18. }

First I get the number of characters in the string with strlen(). The '-1' is there to discard the '\0' character.
Next I loop print all the characters (length1+length2 = characters to display)
In the loop I check if I've reached the end of the char[]. If that's not the case I print the char and raise the indexnumber for the array.
So I have 2 indexnumbers (i1 and i2) which I check with the length of the array. This way you shouldn't get out of bounds and the program wouldn't output random values anymore.
Note: I didn't check to see if this works..That's up to you

Regards Niek
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC