C++

I want to accept numbers from the user into an array and convert into corresponding alphabets. E.g 1 into A, 2 into B, and so on.

This is simple, but the problem is the user is supposed to enter the character # also which I want to display as it is in the output.
How can I do this ?
What type of array should be used - int or char?
number of input?/how many input ?
If you want the user to input numbers, then it makes sense to have an int array.

The value of a char is a number, and it uses ASCII values
http://www.asciitable.com/

For example, the character 'A' has an ASCII value of 65.

1
2
3
4
5
6
#include <iostream>
int main()
{
    int input_num = 1;
    std::cout << static_cast<char>(input_num  + 64) << std::endl;
}

Do you see what's happening?
This casts the number to a character, and then prints it.

A bit off topic, but you could give your title's better descriptions. "C++" in the C++ programming board doesn't give us much :P
Last edited on
@sujit - 20 inputs.
@ganado - yes, I converted the numbers into alphabets. But how do I accept the # characer into a int array?
Huch, I didn't think i had that much problems doing such a simple task...

Since you want to save it as Characters you should go for a char array (but in C++ you usually use std::string instead of char Arrays)

I don't know some things you want to accomplish or how you wanted to handle invalid input so i handeled it like this:
- When a user inputs a number between 1 and 26 the Number gets converted to a Capital Letter(A-Z), otherwise an exception will be thrown;
- If the input is not an integer, an exception will be thrown;
- If the exception is thrown, the current input will be ignored and replaced by '#'
(and the stream get's flushed because we want to read new data)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <exception>
#include <iostream>

int main()
{
    int NumberOfInputs = 20;
    int Number;
    char Letters[21];
    Letters[20] = 0; // End of String character

    for(int i = 0; i < NumberOfInputs; ++i)
    {
        std::cout << "Input " << i + 1 << ": ";
        try
        {
            if(std::cin >> Number)
            {
                if(Number < 1 || Number > 26)
                    throw std::exception();
                else
                    Letters[i] = (char)(Number + 64);
            }
            else
                throw std::exception();
        }
        catch(...)
        {
            Letters[i] = '#';

            std::cin.clear();
            fflush(stdin);
        }
    }
    std::cout << Letters;

    return 0;
}
Last edited on
Thnx @gamer2015, but I feel I did not convey the question properly.
So here is the exact problem..

Chris has been sent secret coded messages from his associates. Given the encoding scheme and the message, print the actual message.

Encoding Scheme:
1. All letters are upper case.
2. A space is indicated by an underscore (_)
3. A number is preceded by the number symbol (#)
4. Each letter is converted as follows: A=1, B=2, C=3, etc.


Output:
All letters are upper case. The only position a space is to be output is where the underscore symbol (_) appears in the encoded message. The output is to be formatted exactly like that for the sample output given below.

Assumptions:
There is 1 space between each item on the data line.
Character set for the message is: {(A-Z) , (0-9) , _ , #} only.

Sample Input:
# 1 13 15 14 20 8 # 3 4 1 25 19

Sample Output:
1MONTH3DAYS (Number after # should be printed as it is !)

Topic archived. No new replies allowed.