Please support our C++ advertiser: Programming Forums
Views: 1751 | Replies: 7
![]() |
•
•
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation:
Rep Power: 4
Solved Threads: 0
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:
<< 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--
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: " << ... 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--
I'd use an array so you could use subtraction to find the letter.
The % (sizeof alphabet - 1) is a safety measure.
#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
*/ 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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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:
or
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:
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.
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>
#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;
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.
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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Jul 2005
Location: Greece
Posts: 18
Reputation:
Rep Power: 4
Solved Threads: 0
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
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 5
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";
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode