Stuck with a letter to alphabet assignment last

New to C++

I am writing a program that inputs a string of letters (e.g. 5 8 11 11 20 15 5 13 1 -1) and outputs its assigned capital alphabetic letter (FILLUPFNB) where 0=A, 1=B, 2=C...25=Z. I have crude program that works for 1 to 9 but not double integer numbers (10 to 25). Better codes are welcome but I'd like to understand where I am going wrong also. All help appreciated.

The problem is that the for loop goes over ever single character and can't pick up 10... but reads it as 1 and 0.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
    string str;
    getline (cin,str);
    string output;
    int d=10, e=11, f=12, g=13, h=14, i=15, j=16, k=17, l=18, m=19, n=20, o=21, p=22, q=23, r=24, s=25;
    //  cout << str[0] << str.length() << endl;
    
    for (float c: str){
        if (c != -1){
            if (c == '0'){
			    output = output + "A";
		    }
            if (c == '1'){
                output = output + "B";
            }
            if (c == '2'){
                output = output + "C";
            }
            if (c == '3'){
                output = output + "D";
            }
            if (c == '4'){
                output = output + "E";
            }
            if (c == '5'){
                output = output + "F";
            }
            if (c == '6'){
                output = output + "G";
            }
            if (c == '7'){
                output = output + "H";
            }
            if (c == '8'){
                output = output + "I";
            }
            if (c == '9'){
                output = output + "J";
            }
            if (c == d){
                output = output + "K";
            }
            if (c == e){
                output = output + "L";
            }
            if (c == f){
                output = output + "M";
            }
            if (c == g){
                output = output + "N";
            }
            if (c == h){
                output = output + "O";
            }
            if (c == i){
                output = output + "P";
            }
            if (c == j){
                output = output + "Q";
            }
            if (c == k){
                output = output + "R";
            }
            if (c == l){
                output = output + "S";
            }
            if (c == m){
                output = output + "T";
            }
            if (c == n){
                output = output + "U";
            }
            if (c == o){
                output = output + "V";
            }
            if (c == p){
                output = output + "W";
            }
            if (c == q){
                output = output + "X";
            }
            if (c == r){
                output = output + "Y";
            }
            if (c == s){
                output = output + "Z"; 
            }
        }
        else{
            break;
        }
    }

    cout << output << endl;
    return 0;
}
tokenize on integers instead of characters with the help of stringstream. You've already included the correct header to do so. Also you probably want to append to an ostringstream, since it's slightly less expensive than constantly changing a regular string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() 
{
    string line;
    cout << "Enter a bunch of integers separated by spaces: ";
    getline(cin, line);
    istringstream iss(line);
    
    ostringstream oss;
    int token;
    while (iss >> token)
    {
        // Some conversion logic
        oss << (char)token;
    }
    cout << "Your output is "<< oss.str() << endl;
}


Also, you must've learned a bit about integer and character conversion, right? Hint: realizing the pattern here could turn your conversion logic into a one-liner.
Last edited on
Hello JCar,

The first thing you need to understand is when you put "5 8 11 11 20 15 5 13 1 -1" into a string it looks like: "5 8 11 11 20 15 ...". The problem is that 11 is stored in the string as two separate characters, so when you want "11" you get from the string "1" and "1". not quite what you want.

Instead of using a string put your numbers into an array of "int"s. This way you can output the character that corresponds to that number.

In your code you have the line for (float c: str). First it is better to use "double" over "float", but what is more important "str"is a "std::string" which you are trying to deal with as a "float". This is two different types which do not match. Since there is an easier way to do what you did I did not work on this. And in your if statements you are trying to compare a "float" to a "char". This will never work. I should have checked it out first, but I am thinking that this would be a compiler error.

This should give you an idea of what you could do:

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
38
39
40
41
#include <iostream>
#include <string>  // <--- Added.
#include <limits>  // <--- Added.
#include <sstream>

//using namespace std;  // <--- Best not to use.

int main()
{  // <--- Moved down to here. makes the program easier to read and easier to find the {}s.
	//std::string str;
	//getline(cin, str);
	//int d = 10, e = 11, f = 12, g = 13, h = 14, i = 15, j = 16, k = 17, l = 18, m = 19, n = 20, o = 21, p = 22, q = 23, r = 24, s = 25;
	int num[20]{};  // <--- Best to initialize your variables.
	int index{};
	std::string output;

	while (1)
	{
		std::cout << "\n Enter a number -1 to quit: ";
		std::cin >> num[index++];

		if (num[index - 1] == -1) break;
	}

	for (size_t lc = 0; lc < index-1; lc++)
	{
		//std::cout << "\n " << static_cast<char> (num[lc]) << std::endl;

		// Or could use something like this.
		output += static_cast<char>(num[lc]);
		output += ", ";
	}

	std::cout << output << std::endl;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
	std::cout << "\n\n\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}
@icy1

I've looked into the sstream and I'm using it now (thanks for the reference),
also I know for the conversion logic I should use the ascii table, but I've been trying to figure out how to do that 'one liner' you hinted at for a while now but without success. So I'm trying this:

1
2
3
4
        if(token == 10)
        {
            oss = oss + 'K';
        }


But not it says I can't use the + operator (even though it is a string?).
It's hard to find explanations of the functions and commands since almost every post/forum just references it and tells the learner to to find it out themselves.


Once again all input is appreciated.

Last edited on
The key to using the ASCII table is that you have to realize 2 things. First, the letters in the ASCII table are in sequential order. In other words, the ASCII codes for A, B, C, ... Z are sequential numbers X, X+1, X+2... X+25. Second, in C++, you can do arithmetic with characters, so char ch = 'A' + 5; will set ch to 'F'.

So if you have a number between 0 and 25, how can you get a letter between 'A' and 'Z' using math?

In the real world, this sort of program is called a "stream processor." It reads from cin, writes to cout, and is designed to not require any human interaction, such as prompting a user for the input. After all, you might have the numbers in a file. My point here is that the solutions so far all assume that you need to read all the input before generating any output. If you relax that restriction then the program becomes far simpler:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int
main(void)
{
    int i;
    while ((std::cin >> i) && i > 0) {
	std::cout << expression_to_generates_letter_from_number;
    }
    std::cout << '\n';
}

Since you've had a few days to mull it over, the necessary point to realize is that 'A' appears in the ascii table at int value 65, as dhayden said. 'B' is 66, 'C' is 67, etc. Since you can do some math on characters, either token+65 or token+'A' will get the job done. I also made it clearer that we want user to enter stuff in the range 0-25:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() 
{
    string line;
    cout << "Enter a bunch of integers from 0 to 25, separated by spaces:\n";
    getline(cin, line);
    istringstream iss(line);
    
    ostringstream oss;
    int token;
    while (iss >> token)
    {
        // Some conversion logic
        oss << (char)(token+65);
    }
    cout << "Your output is "<< oss.str() << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main() 
{
   const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int i;
   string line;
   cout << "Enter a sequence of integers from 0 to 25, terminated by -1:\n";
   while ( cin >> i && i != -1 ) line += alphabet[i];
   cout << line << '\n';
}
Topic archived. No new replies allowed.