User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,533 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,886 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 816 | Replies: 5 | Solved
Reply
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

problem with iterator

  #1  
Oct 4th, 2007
I just started learning about stl.... the problem is that 3 cases that i consider "equivelant", don't do the same thing...so the question is, why they are not equivalent?

here are the statements:
cout<<(char)toupper(*pos)<<" "; //this works!
cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
*pos=toupper(*pos);   //and this also doesn't work
cout<<*pos<<" ";

here is the full code::
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. list<char> coll; //this is the container
  9.  
  10. for(char c='a'; c<='z'; ++c)
  11. coll.push_back(c);
  12.  
  13.  
  14. list<char>::iterator pos;
  15.  
  16. for(pos=coll.begin(); pos!=coll.end(); ++ipos)
  17. {
  18. cout<<(char)toupper(*pos)<<" "; //this works!
  19.  
  20. //cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
  21.  
  22. //*pos=toupper(*pos); //and this also doesn't work
  23. //cout<<*pos<<" ";
  24.  
  25. }
  26. cout<<endl;
  27. cout<<'A'-0<<endl;
  28.  
  29. }
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 744
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: problem with iterator

  #2  
Oct 4th, 2007
your code worked and compiled fine for me (besides a typo in your post)

what exactly are you referring to as not working?


  1. cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
while it does work the output from it may not be what you are expecting. the output from toupper is int not char, so when it is cout'ed the result is going to the ascii equivalent for the character.



  1. *pos=toupper(*pos); //and this also doesn't work
  2. cout<<*pos<<" ";
*pos is pointing to the character that the iterator is on (my wild guess) so it can be cast back to char without an explicit cast.

but it compiles and runs fine on my end

but if you are wondering my output from running this:

A 65 A
B 66 B
C 67 C
. .. .
Y 89 Y
Z 90 Z
Last edited by Killer_Typo : Oct 4th, 2007 at 6:32 pm.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: problem with iterator

  #3  
Oct 5th, 2007
thanks for your answer,

Originally Posted by Killer_Typo View Post
your code worked and compiled fine for me (besides a typo in your post)

what exactly are you referring to as not working?
my mistake, yes the code compiles but the 3 statements have different bahaviours....


cout<<toupper(*pos)<<" ";
while it does work the output from it may not be what you are expecting. the output from toupper is int not char, so when it is cout'ed the result is going to the ascii equivalent for the character.

yes, but according to this
  1. //Program creates char d, sets it equal to lowercase letter
  2. //Converts it to uppercase and outputs it
  3. #include <cctype>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char d='a';
  11. d=toupper(d);
  12. cout<<d;
  13. }
it should print the character not its ascii value.... I found the code here...

the last set of statements {also compiles} but it prints this � character...
  1. *pos=toupper(*pos);
  2. cout<<*pos<<" ";

So any ideas why the output is different?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: problem with iterator

  #4  
Oct 5th, 2007
the code that you posted originally will not compile (line 16 has a typo).
> the last set of statements {also compiles} but it prints this � character...
the most likely cause is that the iterator pos == coll.end() when you dereferenced it.
Last edited by vijayan121 : Oct 5th, 2007 at 4:45 pm.
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 744
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: problem with iterator

  #5  
Oct 5th, 2007
Originally Posted by n.aggel View Post
thanks for your answer,


my mistake, yes the code compiles but the 3 statements have different bahaviours....




yes, but according to this
  1. //Program creates char d, sets it equal to lowercase letter
  2. //Converts it to uppercase and outputs it
  3. #include <cctype>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char d='a';
  11. d=toupper(d);
  12. cout<<d;
  13. }
it should print the character not its ascii value.... I found the code here...
this statement is entirely different from what you have in your code.

  1. /*
  2. toupper returns an INT value
  3. toupper(a) will return 65 the ascii value for an uppercase A
  4. */
  5.  
  6. // the following statement will output the ascii value for the character
  7. cout << toupper(a);
  8.  
  9. // the following statement will cast the INT value to the equivalent CHAR value
  10. cout << (char) toupper(a);

Notice how i had to cast the output from the toupper function to char to get the character output.

By default the return type of toupper is INT not CHAR so to output the results directly will output an integer value not the character equivalent.

The reason your code works (whether or not it throws a warning) is because there exists an implicit cast from INT to CHAR.

So a user can create a CHAR and assign an int value to it.
  1. /* for the following examples the compiler will automagically cast the INT values to CHAR without the use of an explicit cast*/
  2. char mychar = 65;
  3. cout << mychar; // will output A
  4.  
  5. char bigchar = 299;
  6. cout << bigchar; // will output a random char as char can only store a value between 0 and 255, your compiler may throw a warning as well


the last set of statements {also compiles} but it prints this � character...
  1. *pos=toupper(*pos);
  2. cout<<*pos<<" ";

So any ideas why the output is different?

I'm not exactly sure why that is happening. It is working fine on my end, you might want to step through the code with your debugger and see what exactly is being outputted for that one.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: problem with iterator

  #6  
Oct 6th, 2007
thank you all for your help...

vijayan was right... you see in this program i tested both the const_iterator and the iterator... When i copied the for-loop {from the part where i was testing the const_iterator} i changed the pos to ipos, but i stil used the word pos inside the loop{in the part i wanted to test the iterator}, so when i tried to dereference it, i derefernced the pos iterator which was equal[from the previous loop] to coll.end(), this is why it didn't work for me...

It worked for everyone else because when i posted part of the code here i rechanged ipos to pos{it seemed more reasonable as a var name}...
Last edited by n.aggel : Oct 6th, 2007 at 1:29 pm.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC