RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1751 | Replies: 7
Reply
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

Need help with integers in c++

  #1  
Jul 19th, 2005
Hi,
I am developing a program in c++ and what i want to do is to assign an integer to a character. To become more specific i want to make an alphabet and assign to each letter a number. So far so good. The problem is that i want it with user input. For instance see the code below:
int main () {
   const unsigned int a=1, b=2, c=3, d=4, e=5, f=6, g=7; //and so on..
   int input;
   cout << "Please enter a word: " <<endl;
   cin >> input;
   cout << "The output is: " << ... 
<< moderator edit: added [code][/code] tags, reduced indent >>

For example if the user decides to type the word 'bad' i want the output to be displayed in numbers: in our example that would be: 2 1 4 which stands for b a d...
I know it sound easy but i am kinda new to c++ and i dont know even what to look for in the net to concerning this issue, so any help would be greatly appreciated.

--Thanx in advance--
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,765
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help with integers in c++

  #2  
Jul 19th, 2005
I'd use an array so you could use subtraction to find the letter.
#include <iostream>

int main ( void )
{
   static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
   int input;
   while ( std::cin >> input )
   {
      std::cout << alphabet [ (input - 1) % (sizeof alphabet - 1) ] << '\n';
   }
   return 0;
}

/* my output
2 1 4
b
a
d
^C
*/
The % (sizeof alphabet - 1) is a safety measure.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Need help with integers in c++

  #3  
Jul 19th, 2005
Go to google.com and do an image search for 'ASCII Table'.
The images you'll find will show you the decimal representation that computers generally use (by default with C/C++, I believe) to represent letters. For example, the decimal representation of A is 55 or 56. Can't remember which right now.

So, using that table (And assuming that the user keeps to one case (upper or lower)), you can just do a little math on the characters entered, and get your output without assigning all the values by hand.

It will help you greatly if you put the following in your code:
#include <strings>
or
#include "strings.h"

if the first one doesn't work.
Those let you use the 'string' data class, so you can store a word that's been typed in.
Right now, you're trying to assign a word to a single int (which means integer, as in 1, 2, 3, etc.), which just won't work, on several levels.
Make input into:
string input;
when you declare it. That will let you store words that are typed in.
Alternatively, you could use an array of characters to store the information... Not sure if you have to do anything else to get that working with cin.

After that, you need to look up how to access a string character-by-character, and perform the conversion using the ASCII method.
Or, you can use enum, but that's a bit advanced for the moment.
Good luck.
Reply With Quote  
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

Re: Need help with integers in c++

  #4  
Jul 19th, 2005
ok problem solved! Thank u Dave - i hope one day i will reach the level of you expertice in c++ !!!
Also thank you Drowzee for u time explaining me..
Reply With Quote  
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

Re: Need help with integers in c++

  #5  
Jul 19th, 2005
ok - challenge number 2: What now if i would like to input letters and the letters would be displayed as numbers?
For instace i would input b a d and it would display me the corresponding numbers. The opposite of what dave sinkula did...

--Thanx in advance--
Reply With Quote  
Join Date: Apr 2004
Posts: 3,765
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help with integers in c++

  #6  
Jul 19th, 2005
Input a char, loop through the array and see if it matches a character. If it does, a loop index would give you a numeric value.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

Re: Need help with integers in c++

  #7  
Jul 19th, 2005
Dave please look the new thread i created - i explain it better there on what i want - and please if it is easy and not time wasting for you please be more detailed on how to acheive the things i asked because it sounds a bit hard to me to implement what you say in one line - i'd be greatfull if u could provide examples as u explain - thanx again
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Need help with integers in c++

  #8  
Jul 19th, 2005
If I aint totaly wrong Dave ment somthing like this:
    char ch[] = "abcdefghijklmnopqrstuvwxyz"; // character array
    char testc = 'c'; // wath we are looking for
    int res = -1;
    for (int i = 0; ch[i] != '\0'; i++) // loop thru ch, untill it reach the end.
    {
        if (ch[i] == testc) // if we found the char
        {
            res = i; 
        }
    }
    if (res != -1) // dont print out if you dident find it.
    {
        std::cout << testc << " has the number " << res << "in the array \n";
    }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:06 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC