I am kind of new creating the A1Z26 cipher generator. But this has a problem.
Only the program generates the last two numbers. What's wrong?

#include <iostream>
#include <ostream>
#include <string>

using namespace std;

int i; // Counter
char str1[101]; // A string of characters for generator.
string str2, str3; // Adds a string
string str4; // Generated number.

int nochar; // Number of characters.

int a1z26()
{
    for (int i = 0; i < nochar; i++)
    {
        if (str1[i] == 'a')
        {
            str2 = "1-";
        }
        if (str1[i] == 'b')
        {
            str2 = "2-";
        }
        if (str1[i] == 'c')
        {
            str2 = "3-";
        }
        if (str1[i] == 'd')
        {
            str2 = "4-";
        }
        if (str1[i] == 'e')
        {
            str2 = "5-";
        }
        if (str1[i] == 'f')
        {
            str2 = "6-";
        }
        if (str1[i] == 'g')
        {
            str2 = "7-";
        }
        if (str1[i] == 'h')
        {
            str2 = "8-";
        }
        if (str1[i] == 'i')
        {
            str2 = "9-";
        }
        if (str1[i] == 'j')
        {
            str2 = "10-";
        }
        if (str1[i] == 'k')
        {
            str2 = "11-";
        }
        if (str1[i] == 'l')
        {
            str2 = "12-";
        }
        if (str1[i] == 'm')
        {
            str2 = "13-";
        }
        if (str1[i] == 'n')
        {
            str2 = "14-";
        }
        if (str1[i] == 'o')
        {
            str2 = "15-";
        }
        if (str1[i] == 'p')
        {
            str2 = "16-";
        }
        if (str1[i] == 'q')
        {
            str2 = "17-";
        }
        if (str1[i] == 'r')
        {
            str2 = "18-";
        }
        if (str1[i] == 's')
        {
            str2 = "19-";
        }
        if (str1[i] == 't')
        {
            str2 = "20-";
        }
        if (str1[i] == 'u')
        {
            str2 = "21-";
        }
        if (str1[i] == 'v')
        {
            str2 = "22-";
        }
        if (str1[i] == 'w')
        {
            str2 = "23-";
        }
        if (str1[i] == 'x')
        {
            str2 = "24-";
        }
        if (str1[i] == 'y')
        {
            str2 = "25-";
        }
        if (str1[i] == 'z')
        {
            str2 = "26-";
        }
        if (str1[i] == ' ')
        {
            str2 = " ";
        }
        str3.swap(str2);
    }
    str4 = str2 + str3;
    return 0;
}

int main()
{
    cout << "A1Z26 cipher generator." << endl;
    cout << "Generates a string of characters into numbers." << endl << endl;

    return1:
    cout << "Enter the string (up to 100 characters, no numbers):\n";
    cin.getline(str1, 101);

    if (str1[101] == '0' && str1[101] == '1' && str1[101] == '2'&& str1[101] == '3'&& str1[101] == '4'&& str1[101] == '5'&& str1[101] == '6'&& str1[101] == '7'&& str1[101] == '8' && str1[101] == '9')
    {
        goto return1;
    }

    cout << endl;

    _strlwr_s(str1); // Transforms the letters in a string to all lower-case letters.
    nochar = strlen(str1);

    a1z26();

    cout << "Your generated number: " << endl;

    cout << str4 << endl <<endl;

    cout << "Share this to your friends to decode your mystery!" << endl;

    system("pause");
    return 0;
}

It appears that your a1z26() method iterates through the string but it keeps overwriting str2 instead of adding to it.

You might be over thinking this a bit. Since char is basically an int, if you cast it to an int, all you need is a simple offset to get the substitution number, and then convert it to a string, your code becomes much simpler:

#include <iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
    string str1 = "", str2 = "";
    cout << "A1Z26 cipher generator." << endl;
    cout << "Generates a string of characters into numbers." << endl << endl;
    cout << "Enter the string (up to 100 characters, no numbers):\n";
    //Get input string.  Use newline as terminator and store the string in str1
    getline(cin,str1,'\n');
    //truncate str1 to 100 characters
    str1 = str1.substr(0,100);
    for(int i=0;i <str1.length();i++)
    {
        stringstream ss;
        string temp = "";
        if(isalpha(str1[i]) || str[i] == ' ')
        {
            //break up  the string with spaces in the same places.  This trim the '-' off the end before adding the space.
            if(str1[i] == ' ')
            {
                str2 = str2.substr(0, str2.length() -1) + str[i];
            }
            else
            {
                //convert the char to int and substract the offset
                //stringstream is an easy way to convert numbers to strings.
                ss << (int)(tolower(str1[i])-96);
                ss >> temp;                
                str2 += temp + "-";
            }
        }
    }
    //Trim the '-' off the final string
    str2= str2.substr(0, str2.length() -1);
    cout << "Your generated number: " << endl;
    cout << str2 << endl <<endl;
    cout << "Share this to your friends to decode your mystery!" << endl;
    system("pause");
    return 0;
}

This code truncates the input string to 100 characters and ignores any non-alphabet characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.