LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2004, 10:50 PM   #1
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Rep: Reputation: 15
How do I add character+number to get char in C++?


I need to add items in a character array to a number and the result needs to be a character.

for example, If I type in BILL I want it to display perhaps, ELOO, which is each letter plus 3.

can anybody tell me?
 
Old 02-09-2004, 10:55 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
A character in reality is just a number. For instance the character 'A' = 65, 'B' = 66 (if I am remembering my ascii chart). So if you want to add 3 to each letter then do something like this.

for (int i=0;i < lengthofcharray;i++)
{
charray[i] += 3;
}

Last edited by jtshaw; 02-09-2004 at 11:07 PM.
 
Old 02-09-2004, 10:59 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Just to possibly help you more, I quickly found a link to an ascii chart. So there is the number representation of all the ascii characters.
 
Old 02-09-2004, 11:09 PM   #4
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Original Poster
Rep: Reputation: 15
ok, thanks for the help, but when it's done, it just prints out a number. Like A+3=68 and I need the letter not the number. How can I do that? I have done this before but for the life of me I can't remember how.
 
Old 02-09-2004, 11:20 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
How are you printing it? Let me see your print statement.

Example of printing a char
printf("%c",ch);
Example of printing a string
printf("%s",string);

Last edited by jtshaw; 02-09-2004 at 11:21 PM.
 
Old 02-09-2004, 11:30 PM   #6
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Original Poster
Rep: Reputation: 15
Here's a simple program I was just experimenting with:

#include<iostream.h>

int main()
{
char c1[5], c2;
cout << "Enter 2 chars.";
cin >> c1 >> c2;

for(int i=0; i<5; i++)
{
c1[i]+=3;
}

cout << c1 << endl;
cout << c2+3 << endl;
cout << c1+c2 << endl;
}


Here's the output it's giving me:

Enter 2 chars.A B
DEc
69




Here's the code in the program I am trying to make, method called Encode in class Encrypt:

char Encrypt::Encode(char P[])
{
for(int i=0; i<100; i++)
{
//cout << P[0];
P[i]+=k;
}
return *P;

This is the function calling the encode method:
if(strcmp(Command, "E")==0)
{
Encrypt A;
*P=A.Encode(P);
cout << "\n" << P << " is the encoded message.\n";
}

}



I can post a link to all the files if you want, as well as a link to the homework assignment.
 
Old 02-09-2004, 11:35 PM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Well, since this is hw I will try and point you towards the write direction, without solving it for you (cause you want to learn right?). I suggest you try printing out c1 before you do anything to it and see if it is what you expect it to be in the first place.....
 
Old 02-09-2004, 11:39 PM   #8
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Original Poster
Rep: Reputation: 15
yes, all I was looking for was somebody to point me in the write direction because I did this last semester but I can't find it in any of the notes or projects I did.

I printed c1 and it's printing properly, but adding an int to it turns A+3 into 69.

Try this link for my source code to see all the files http://kansas.no-ip.com/~dustin/258/
 
Old 02-09-2004, 11:42 PM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Cast it to a char in your print statement.
 
Old 02-10-2004, 12:06 AM   #10
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Original Poster
Rep: Reputation: 15
ok, I put (char) in one of the print statements of my experiment program and I still get a number. It may be in char form but I need the letter still. I've been searching google and am not finding much help at all.....and the teaching assistant hasn't responded to my email that I sent about 8 hours ago.

I thought I could say A+1 and get B...am I right or was that some dream I had?

btw, thanks for the help.
 
Old 02-10-2004, 12:16 AM   #11
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Code:
int main(void)
{
   char ch = 'A';
   std::cout << ch << ' ';
   std::cout << ++ch << ' ';
   std::cout << char(ch+1) << ' ';
   return 0;
}
Ok, this is my test code. Running this I get this output:"A B C".

Reasoning:
++ch sets ch=ch+1 before the cout is execute.
char(ch+1) adds 1 to ch and casts it as a character.

Just to show subtle differences. If I changed the 2nd cout to say std::cout << ch++ << ' '; then the output would be "A A C"

Last edited by jtshaw; 02-10-2004 at 12:19 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
How do I add two octal number ? Linh Programming 3 05-20-2004 03:08 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM
convert a character in a box to keycode number Linh Programming 3 10-14-2003 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration