Write your question here.
I'm new in c++ and my friend asked me if I can make her a simple game.
it's so simple right now because we are just starting on making the rules.
our goal is to create a secret language. we have already worked on some parts of it but there is a problem.
she wants it to be as if you get a string of characters from the user and then the program replaces each letter with the one on it's left(on keyboard).
we don't want to think about the exceptions yet. we just wanna know if such thing is possible to code and if so how?
PS.we don't have problem getting the string. our problem is with replacement of the letters.
oh and I forgot to say in first row of key is going to be letters from q to p,
second row a to;
and third row z to /
so that there are about even characters in each row
You could use a 2D array to represent the keyboard. Most of the time you would only have to back up one element in the array until the starting column is zero then you would have a little extra work to change both subscripts.
Using a "std::map" might also work in this case. The key value pair would make it easier to have the correct exchange for what you want.
And if you want to use the numbers just add an extra row.
Well that answers my question. If you have to ask about "std::map" you have not studied it yet or used it.
"std::" just means that "map" is in the standard namespace.
To use a map you first need the header file "<map>". After that a map is similar to an array with the difference being that "map" works with a pair of variables. These variables could be of the same type or different types. The first part, lhs, is the key and needs to be unique. The second, rhs, is the value of the key which can have duplicates.
The advantage to the map is if the key is "Q" the value would be "/". Just as the key "W" would have the value "Q" and the key "P" would have the value "O".
This makes the translating of the original input much easier.
To decode what you have done just copy the map, give it a new name and switch the key/value characters.
I have been working on the program using a 2D array. It mostly works, but I am having a problem dealing with the letter "Q".
Thinking of trying a different version using a map.
Let me know what you would like to do or what you can do and I will help you all I can.
#include <iostream>
#include <string>
#include <cctype>
char get_left(char c);///this function does all the work
std::string convert_left(const std::string& input);
int main()
{
std::string input;
std::cout<<"Enter a sentence : ";
std::getline(std::cin,input,'\n');
std::cout<<"\nConverted text : "<<convert_left(input)<<std::endl;
}
char get_left(char c)
{ ///i stored all rows in a string
const std::string lib("1234567890-qwertyuiopasdfghjkl;zxcvbnm,./");/// if c is first on a keyboard row, i'll
bool upper_case = std::isupper(c); /// return the last char on the row above
c = std::tolower(c);
auto pos = lib.find(c);
if(pos == std::string::npos)//if the character isn't found i return it e.g ! is absent
return c;
elseif(pos == 0)
return'/';
else
{
char d = lib[pos-1];
if(upper_case)
d = std::toupper(d);
return d;
}
}
std::string convert_left(const std::string& input)
{
std::string result{};
for(auto _C : input)
result += get_left(_C);
return result;
}
Enter a sentence : we just sold the OLD house at a Half price
Converted text : qw hyar aiks rgw IKS giyaw pr p Gpkd oeuxw