toupper with pointers?

Ive read several topics on the inet now that claims that the following should work:

Definition:
1
2
3
4
5
6
void read( char* in1, char* in2 ){
	cout << "\nYour choice: ";
	cin  >> in1 >> in2;
	*in1 = toupper(*in1);
	*in2 = toupper(*in2);
};


place its called:
1
2
	char sv1 = '0', sv2 = '0';
        read( sv1 , sv2 );


Here is a link to an example:
http://www.java2s.com/Tutorial/Cpp/0040__Data-Types/Convertinglowercaseletterstouppercaselettersusingapointer.htm

(I know its a site about java, but the tutorial is about c++)

Could some1 help me figure out what Im doing wrong and how I should fix it?
Last edited on
You must pass a pointer to the function:
1
2
3
4
5
6
        char sv1 = 'a';
        char greeting[] = "hello";
        read(
                &sv1,     // sv1 is a char, so &sv1 is a pointer to the char
                greeting  // greeting is a pointer to an array of char
                );

Hope this helps.
Well, thats what made me confused. Because the tutorial in the link doesnt pass a pointer does it? :S

Also, Ive tried that and no success.
Last edited on
error C2665: 'read' : none of the 2 overloads could convert all the argument types
1> d:\grprog\blablab\some.cpp could be 'void read(char *,char *)'
1> while trying to match the argument list '(char, char)'

this is with:

read( &sv1 , &sv2 );

here is the declaration of the function:
void read ( char* , char* );

and here is the definition:
1
2
3
4
5
6
void read( char* in1, char* in2 ){
	cout << "\nYour choice: ";
	cin  >> *in1 >> *in2;
	*in1 = toupper(*in1);
	*in2 = toupper(*in2);
};


also tried: read( sv1 , sv2 ); with same results. :(
Last edited on
That's a really dangerous function because you have no idea how much data will be written to in1 or in2 from cin, nor how much data those two character buffers can hold.

The example does pass in a pointer -- the variable "phrase" in that example is both a character array and a pointer to the first character in the array.
Ah ok, so a array is automatically a pointer.

The function is for a menu system we have to make for a school project.

The menu choices are to be read by two letters. So if I wanna do one thing: I could write: k t and based on alot of if sentences, I would then find the correct menu choice with this function. Problem is that in order to make sure that the two letters are understood correctly, I will have to make them to upper or to lower. The loop that is used to check these chars have a else for all the wrong inputs aswell.

Best way I could think of was to do this with pointers or by reference so that I dont have to return anything(cause then I would have to extract the data in order to get two variables from it afterwards).

reason why Im using only two chars is thats the best way I could think of to get two letters only.

I do like to use string objects mostly, but I couldnt think of a way to limit a string to only be 1 letter(or having to modify the returned data so that I get 2 variables from it). If you have any tips on how I could do what Im after in a better way, feel free to give me some tips :)
Last edited on
The safest thing to do is to read the entire response into a string.

1
2
std::string response;
std::getline(std::cin, response);


And then parse the input.

1
2
3
4
5
6
7
if (response.size() != 2)
{
    // bad input
}

in1 = response[0];
in2 = response[1];


Also, pass in1 and in2 as references in the method signature. This is C++after all; we try to avoid pointers.

Does that help?

Yeah, it helped. :)

I ended up reading into a string inside the function, then add the 2 first characters inside the string that wasnt a whitespace into the char[] in the parameter, then toupper them all and going out of the function :)
Topic archived. No new replies allowed.