•
•
•
•
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
![]() |
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation:
Rep Power: 3
Solved Threads: 9
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:
here is the full code::
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::
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <list> using namespace std; int main() { list<char> coll; //this is the container for(char c='a'; c<='z'; ++c) coll.push_back(c); list<char>::iterator pos; for(pos=coll.begin(); pos!=coll.end(); ++ipos) { 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<<" "; } cout<<endl; cout<<'A'-0<<endl; }
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"
by Robert Frost the "The Road Not Taken"
your code worked and compiled fine for me (besides a typo in your post)
what exactly are you referring to as not working?
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.
*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:
what exactly are you referring to as not working?
cplusplus Syntax (Toggle Plain Text)
cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
cplusplus Syntax (Toggle Plain Text)
*pos=toupper(*pos); //and this also doesn't work cout<<*pos<<" ";
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!
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!
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation:
Rep Power: 3
Solved Threads: 9
thanks for your answer,
my mistake, yes the code compiles but the 3 statements have different bahaviours....
yes, but according to this 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...
So any ideas why the output is different?
•
•
•
•
your code worked and compiled fine for me (besides a typo in your post)
what exactly are you referring to as not working?
•
•
•
•
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
c++ Syntax (Toggle Plain Text)
//Program creates char d, sets it equal to lowercase letter //Converts it to uppercase and outputs it #include <cctype> #include <iostream> using namespace std; int main() { char d='a'; d=toupper(d); cout<<d; }
the last set of statements {also compiles} but it prints this � character...
c++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); 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"
by Robert Frost the "The Road Not Taken"
•
•
•
•
thanks for your answer,
my mistake, yes the code compiles but the 3 statements have different bahaviours....
yes, but according to thisit should print the character not its ascii value.... I found the code here...c++ Syntax (Toggle Plain Text)
//Program creates char d, sets it equal to lowercase letter //Converts it to uppercase and outputs it #include <cctype> #include <iostream> using namespace std; int main() { char d='a'; d=toupper(d); cout<<d; }
cplusplus Syntax (Toggle Plain Text)
/* toupper returns an INT value toupper(a) will return 65 the ascii value for an uppercase A */ // the following statement will output the ascii value for the character cout << toupper(a); // the following statement will cast the INT value to the equivalent CHAR value 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.
cplusplus Syntax (Toggle Plain Text)
/* for the following examples the compiler will automagically cast the INT values to CHAR without the use of an explicit cast*/ char mychar = 65; cout << mychar; // will output A char bigchar = 299; 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...
c++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); 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!
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!
•
•
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation:
Rep Power: 3
Solved Threads: 9
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}...
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"
by Robert Frost the "The Road Not Taken"
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- iterator deref problem (C++)
- Need help writing my own reverse iterator class (C++)
- Hash Table template implementation help (C++)
- iterator based search algorithm help (C++)
- how to find loop (C)
- "cannot resolve symbol"problem (Java)
- List <char *> Problem (C)
- Problem While Processing A String List (C++)
- Problem with pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: [MSVC++]Error, Trying to convert Letter -> #
- Next Thread: how to use windows.h



Linear Mode