Can anyone help me with the following C++ code?

I need to make a program which translates words from character to numbers, say like a=1, b=2, c=3, etc...

It is working fine but when I type something like "zain", it will output 1 9 14 26 which is the expected output for "ainz"

The expected, correct output should be 26 1 9 14 (without spaces).

Can anyone please correct the following code to get the expected output?

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
 class c1{
 public:
 int i;
    char arr1[10];
    int  f2(char b1 , int i2){


for(i=0;i<11;i++){
        if(arr1[i]==b1){
        cout<<i2;
}
        }
    return 0;
    }
    void nl(){
        cin>>arr1;
f2('a',1);
f2('b',2);
f2('c',3);
f2('d',4);
f2('e',5);
f2('f',6);
f2('g',7);
f2('h',8);
f2('i',9);
f2('j',10);
f2('k',11);
f2('l',12);
f2('m',13);
f2('n',14);
f2('o',15);
f2('p',16);
f2('q',17);
f2('r',18);
f2('s',19);
f2('t',20);
f2('u',21);
f2('v',22);
f2('w',23);
f2('x',24);
f2('y',25);
f2('z',26);
    }
 };
int main (){

cout<<"hello"<<endl;
c1 c1;
c1.nl();


    return 0;
}
}
Write your answer…

Well, first of all, you should form the habit of doing good formatting. It is very hard to read your code like that. There are nice code formatters. I used CLion to get the below style, which is a lot more readable and conforms to some basic agreed rules.

The problem with your logic is that you first search for a in the input, then for b and so on. Have you tried inputting the string aaaaa ? You will see that the output is 1 instead of 1 1 1 1 1 , because you only check for a once in the beginning. Instead of searching for each letter in the alphabet once after each other, you should loop over the input and convert the current character to the according number!

So, I added my remarks to your code and then created a working solution with an explanation of what I did to make it work. Read it, understand it, try to write it yourself without looking, then try to do the follow-up tasks "use an iterator to loop over the string" and "calculate the sum of all numbers you get from the existing code", just as a little practice :)

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class c1 {//why use a class for something so very simple? Do not over-engineer your code, keep it simple and stupid!
public:
    int i;// you do not need i to be available to the whole class; you just use it in that one loop
    char arr1[10];// so the user cannot input strings which are longer than 10 characters?

    int f2(char b1, int i2) {// "f2" is a very bad name. I had to read your code to understand what it does

        for (i = 0; i < 11; i++) {
            if (arr1[i] == b1) {
                cout << i2;
            }
        }
        return 0;
    }

    void nl() {
        cin >> arr1;
        f2('a', 1);
        f2('b', 2);
        f2('c', 3);
        f2('d', 4);
        f2('e', 5);
        f2('f', 6);
        f2('g', 7);
        f2('h', 8);
        f2('i', 9);
        f2('j', 10);
        f2('k', 11);
        f2('l', 12);
        f2('m', 13);
        f2('n', 14);
        f2('o', 15);
        f2('p', 16);
        f2('q', 17);
        f2('r', 18);
        f2('s', 19);
        f2('t', 20);
        f2('u', 21);
        f2('v', 22);
        f2('w', 23);
        f2('x', 24);
        f2('y', 25);
        f2('z', 26);
    }
};

int main() {

    cout << "hello" << endl;
    c1 c1;
    c1.nl();


    return 0;
}

}// ERROR: This bracket is wrong.

Here is my solution:

#include <iostream>
#include <string>

using namespace std;


int main()
{
    // This constant will be the number you want to assign to lower-case a.
    // Starting from a, all following letters of the alphabet will have their number according to their position.
    // For example: `const NUMBER_FOR_A = 1;`   =>   a=1, b=2, c=3,...
    const int NUMBER_FOR_A = 1;

    // This variable will store the user-input
    string input = "";

    // First: Tell your user what they have to do (UX)
    cout << "Please input the string you want to convert:" << endl;

    // Second: Get the user input (I use getline(), because it enables the user to input whitespaces as well)
    getline(cin, input);

    // Third: Loop over all characters in the string
    int currentNumber = 0;
    for (int i = 0; i < input.length(); i++)
    {
        // All characters are stored as numbers, according to the ASCII table.
        // Hence, I can store the character inside an integer and compare it to other characters.
        currentNumber = input[i];

        // Output the number, if it is part of the English alphabet
        if (currentNumber >= 'a' && currentNumber <= 'z')
        {
            // By subtracting 'a', I can reset the ascii number of a lower case 'a' character to 0,
            // for example 'a' - 'a' = 97 - 97 = 0
            // and 'b' - 'a' = 98 - 97 = 1.
            // After that, I have to add the previously configured base number for a so that I get the correct output.
            currentNumber = currentNumber - 'a' + NUMBER_FOR_A;
            cout << currentNumber << " ";
        }
    }

    // Add a newline at the end of your output for separation (optional)
    cout << endl;

    // Last but not least, pass the return-code 0 (=successful) back to the operating system.
    return 0;
}

With the expected output

C:>tmp.exe

Please input the string you want to convert:

zain

26 1 9 14

C:>

Show all replies

💜💜💜💜

Reply to this…