Need help shifting the char data type in C++.

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

Need help shifting the char data type in C++.

Postby Stucky101 » Thu Aug 05, 2010 11:37 pm UTC

I'm making a Caesar Cipher program using C++, just for practice with the language. I've run into trouble when I try to shift a character from a file over a number of places on the ASCII table. So say the character is A but I want to shift is 3 spaces so that it is C, I can't figure how to do that. I thought the addition operator would work like so.

Code: Select all

char c='a';
c+3;
However the result is the same letter originally given to c. If anyone knows how to do this I'd appreciate the help. :D
"The earth is the cradle of humankind, but one cannot live in the cradle forever."
~Konstantin Tsiolkovsky, 1895

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Need help shifting the char data type in C++.

Postby Rysto » Thu Aug 05, 2010 11:39 pm UTC

You have to actually modify c by using an assignment operator. i.e.:

Code: Select all

c = c + 3;
//-- or --
c += 3;

User avatar
Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

Re: Need help shifting the char data type in C++.

Postby Stucky101 » Thu Aug 05, 2010 11:58 pm UTC

Oh lol, I always forget the simplest things in my programs. Thanks for clearing that up.
"The earth is the cradle of humankind, but one cannot live in the cradle forever."

~Konstantin Tsiolkovsky, 1895


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 4 guests