I'm just starting with c++ and am trying to write a program, which takes a word and converts the letters to integers matching their position in the alphabet (separated by dots) e.g. hello -> 8.5.12.12.15 (hope I got that one right ;) )
I wrote a little program, but it doesn't seem to work. If I enter a single letter, then the output is correct, but if I enter multiple letters it crashes.
This is the code:
#include "stdafx.h"
#include <iostream>
#include <string>
int convert(std::string* a, int i)
{
int b;
if (a[i] == "a") { b = 1; return b;}
else if (a[i] == "b") { b = 2; return b;}
else if (a[i] == "c") { b = 3; return b;}
else if (a[i] == "d") { b = 4; return b;}
else if (a[i] == "e") { b = 5; return b;}
else if (a[i] == "f") { b = 6; return b;}
else if (a[i] == "g") { b = 7; return b;}
else if (a[i] == "h") { b = 8; return b;}
else if (a[i] == "i") { b = 9; return b;}
else if (a[i] == "j") { b = 10; return b;}
else if (a[i] == "k") { b = 11; return b;}
else if (a[i] == "l") { b = 12; return b;}
else if (a[i] == "m") { b = 13; return b;}
else if (a[i] == "n") { b = 14; return b;}
else if (a[i] == "o") { b = 15; return b;}
else if (a[i] == "p") { b = 16; return b;}
else if (a[i] == "q") { b = 17; return b;}
else if (a[i] == "r") { b = 18; return b;}
else if (a[i] == "s") { b = 19; return b;}
else if (a[i] == "t") { b = 20; return b;}
else if (a[i] == "u") { b = 21; return b;}
else if (a[i] == "v") { b = 22; return b;}
else if (a[i] == "w") { b = 23; return b;}
else if (a[i] == "x") { b = 24; return b;}
else if (a[i] == "y") { b = 25; return b;}
else if (a[i] == "z") { b = 26; return b;}
}
int main()
{
std::string* a = new std::string;
std::string out;
std::cout << "Please enter a word: ";
std::cin >> *a;
int i = 0;
do
{
out += std::to_string(convert(a, i)) + ".";
i++;
} while (i < a->size());
std::cout << "The converted word is: " << out << std::endl;
return 0;
}
I'm at a loss here and hope if you could help me...
Thanks in advance,
The Kaltur
[edit] fixed code