Copying words into an array of char ?!?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #31
Dec 9th, 2006
Hey guys,


I really appreciate the effort and time you take out to help me become a better coder :!: I know I'm not the best and it takes alot of time, but, I'm having so much fun in learning this(even if they are simple programs).

I also, worked out the pseudo code WaltP gave me and turned it into this:

  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. char words[5][10];
  6.  
  7. for (size_t i = 0; i < 5; ++i)
  8. std::cin >> words[i];
  9. std::cout << std::endl;
  10.  
  11.  
  12. for (size_t i = 0; i < 5; i++)
  13. {
  14. bool contr = true;
  15. for (size_t j = i+1; j < 5; j++)
  16. {
  17. if(strcmp(words[i], words[j]) == 0)
  18. {
  19. contr = false;
  20. break;
  21. }
  22. }
  23. if(contr == true)
  24. std::cout << words[i] << '\n';
  25. }
  26. std::cin.ignore(2);
  27.  
  28. return 0;
  29. }

I think it differs abit from the pseudo code he gave me, but, it works perfectly. In his pseudo code, he mentions to loop from first to last minus one, wich I "think" means this:for (size_t i = 0; i < 4; i++). But, that doesn't seem to work for me.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Copying words into an array of char ?!?

 
0
  #32
Dec 9th, 2006
Originally Posted by ~s.o.s~ View Post
Yes I would ask you to compulsorily make the change.

Normally the strcmp( ) returns -1 , 0 or 1.
My output from your program:
The ans is -32
The ans is -1
The ans is -2
using Borland 5.5

So some compilers convert the returns to -1/0/1, others just use the difference between the characters:
'A' - 'a' = 32
'c' - 'd' = -1
'b' - 'd' = -2

So definitely just use <|=|> 0


Originally Posted by JoBe View Post
I also, worked out the pseudo code WaltP gave me and turned it into this:
<snip code>
I think it differs abit from the pseudo code he gave me, but, it works perfectly. In his pseudo code, he mentions to loop from first to last minus one, wich I "think" means this:for (size_t i = 0; i < 4; i++). But, that doesn't seem to work for me.
Good job! Yes it differs from mine, and the main reason is you corrected a problem in my pseudo code My loops were a little off.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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