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::

#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;

}

Recommended Answers

All 5 Replies

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)<<" "; //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.

*pos=toupper(*pos); //and this also doesn't work
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

thanks for your answer,

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

//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;
}

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...

*pos=toupper(*pos); 
cout<<*pos<<" ";

So any ideas why the output is different?

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.

thanks for your answer,


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


yes, but according to this

//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;
}

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.

/* 
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.

/* 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...

*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.

commented: thanks! +1

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}...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.