C++ Simple letter to number encryption [closed] - c++
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am very new and horrible at C++.
I was hoping someone could figure out what this error means, I seriously have no idea what to do, I am NOT asking to have you to try to make it more effiecent, I realize my uneffieceny.
I am trying to convert letters to numbers, I understand this is weak encryption.
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
std::string ptxt = "";
string etxt = "";
cin >> ptxt;
std::replace( ptxt.begin(), ptxt.end(), 'a', '1');
std::replace( ptxt.begin(), ptxt.end(), 'b', '2');
std::replace( ptxt.begin(), ptxt.end(), 'c', '3');
std::replace( ptxt.begin(), ptxt.end(), 'd', '4');
std::replace( ptxt.begin(), ptxt.end(), 'e', '5');
std::replace( ptxt.begin(), ptxt.end(), 'f', '6');
std::replace( ptxt.begin(), ptxt.end(), 'g', '7');
std::replace( ptxt.begin(), ptxt.end(), 'h', '8');
std::replace( ptxt.begin(), ptxt.end(), 'i', '9');
std::replace( ptxt.begin(), ptxt.end(), 'j', '10');
std::replace( ptxt.begin(), ptxt.end(), 'k', '11');
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
std::replace( ptxt.begin(), ptxt.end(), 'm', '13');
std::replace( ptxt.begin(), ptxt.end(), 'n', '14');
std::replace( ptxt.begin(), ptxt.end(), 'o', '15');
std::replace( ptxt.begin(), ptxt.end(), 'p', '16');
std::replace( ptxt.begin(), ptxt.end(), 'q', '17');
std::replace( ptxt.begin(), ptxt.end(), 'r', '18');
std::replace( ptxt.begin(), ptxt.end(), 's', '19');
std::replace( ptxt.begin(), ptxt.end(), 't', '20');
std::replace( ptxt.begin(), ptxt.end(), 'u', '21');
std::replace( ptxt.begin(), ptxt.end(), 'v', '22');
std::replace( ptxt.begin(), ptxt.end(), 'w', '23');
std::replace( ptxt.begin(), ptxt.end(), 'x', '24');
std::replace( ptxt.begin(), ptxt.end(), 'y', '25');
std::replace( ptxt.begin(), ptxt.end(), 'z', '26');
return 0;
}
Build Log (Debug?)
Let's start from the beginning of the build log:
main.cpp|25|warning: multi-character character constant [-Wmultichar]|
A character constant is like 'a' or '1'. If we go and look at the code this warning points to we see:
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
Note that '12': there are two characters inside that character constant. This is warning you about the fact that you have more than one. What exactly a 'multi-character' character constant is and what they're good for doesn't matter, you don't need them and should not be using them.
If you really want text like "hello" to be encoded as "85121215" or "8 5 12 12 15" then you're going to have to rethink how you do the replacement. This is for two reasons: a multi-character character constant like '12' will not look like 12 in your output file, and secondly because std::replace will not handle replacing a single character like l with multiple characters.
For now lets replace all the mult-character character constants you've got with single-character versions:
std::replace( ptxt.begin(), ptxt.end(), 'a', '1');
std::replace( ptxt.begin(), ptxt.end(), 'b', '2');
std::replace( ptxt.begin(), ptxt.end(), 'c', '3');
std::replace( ptxt.begin(), ptxt.end(), 'd', '4');
std::replace( ptxt.begin(), ptxt.end(), 'e', '5');
std::replace( ptxt.begin(), ptxt.end(), 'f', '6');
std::replace( ptxt.begin(), ptxt.end(), 'g', '7');
std::replace( ptxt.begin(), ptxt.end(), 'h', '8');
std::replace( ptxt.begin(), ptxt.end(), 'i', '9');
std::replace(ptxt.begin(), ptxt.end(), 'j', 'a');
std::replace(ptxt.begin(), ptxt.end(), 'k', 'b');
std::replace(ptxt.begin(), ptxt.end(), 'l', 'c');
std::replace(ptxt.begin(), ptxt.end(), 'm', 'd');
std::replace(ptxt.begin(), ptxt.end(), 'n', 'e');
std::replace(ptxt.begin(), ptxt.end(), 'o', 'f');
std::replace(ptxt.begin(), ptxt.end(), 'p', 'g');
std::replace(ptxt.begin(), ptxt.end(), 'q', 'h');
std::replace(ptxt.begin(), ptxt.end(), 'r', 'i');
std::replace(ptxt.begin(), ptxt.end(), 's', 'j');
std::replace(ptxt.begin(), ptxt.end(), 't', 'k');
std::replace(ptxt.begin(), ptxt.end(), 'u', 'l');
std::replace(ptxt.begin(), ptxt.end(), 'v', 'm');
std::replace(ptxt.begin(), ptxt.end(), 'w', 'n');
std::replace(ptxt.begin(), ptxt.end(), 'x', 'o');
std::replace(ptxt.begin(), ptxt.end(), 'y', 'p');
std::replace(ptxt.begin(), ptxt.end(), 'z', 'q');
This change addresses all the warnings in the build log, and also happens to fix all the errors as well.
If you're curious what the errors were: they were just a sort of verbose way of telling you that your arguments to std::replace() were not correct. Multi-characters happen to have the type int instead of char, and std::replace() does not accept taking an int for the last argument when the second to last argument is a char.
this has type `int`
|
V
std::replace( ptxt.begin(), ptxt.end(), 'l', '12');
^
|
this has type `char`... mismatch error between `int` and `char`
Taking issues one at a time is a good way to avoid being overwhelmed by all of them at once. Just take them one at a time, starting from the beginning. Often fixing one issue reported earlier will make other warnings and errors go away so you never have to bother with them.
Do not ignore warnings. Even though your programs may compile and work without all the warnings being fixed, it's a bad habit to ignore them. In fact, you should enable more warnings if you can, and pay attention to those too.
Related
How to change type of elements in C++ boost multi array?
I receive a matrix with elements of type unsigned char from another function and I am trying to find its max value. boost::multi_array<unsigned char, 2> matrix; All elements are integers and so I was hoping to recast matrix as type <int, 2> then perform std::max_element() operation, but unsure how to recast type of a boost multi array.
You don't need that to use max_element. char is an integral type just like int: Live On Compiler Explorer #include <boost/multi_array.hpp> #include <fmt/ranges.h> #include <algorithm> int main() { using boost::extents; boost::multi_array<unsigned char, 2> matrix(extents[10][5]); std::iota( // matrix.data(), // matrix.data() + matrix.num_elements(), '\x30'); fmt::print("matrix: {}\n", matrix); auto [a, b] = std::minmax_element(matrix.data(), matrix.data() + matrix.num_elements()); // as integers fmt::print("min: {}, max {}\n", *a, *b); // as characters fmt::print("min: '{:c}', max '{:c}'\n", *a, *b); } Program stdout matrix: {{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}} min: 48, max 97 min: '0', max 'a' Reinterpreting View If you must (for other reasons thatn using max_element) you can use a multi_array_ref: // reinterpreting view: boost::multi_array_ref<const char, 2> view( reinterpret_cast<const char*>(matrix.data()), std::vector(matrix.shape(), matrix.shape() + 2)); fmt::print("view: {}\n", view); Which prints Live On Compiler Explorer view: {{'0', '1', '2', '3', '4'}, {'5', '6', '7', '8', '9'}, {':', ';', '<', '=', '>'}, {'?', '#', 'A', 'B', 'C'}, {'D', 'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L', 'M'}, {'N', 'O', 'P', 'Q', 'R'}, {'S', 'T', 'U', 'V', 'W'}, {'X', 'Y', 'Z', '[', '\'}, {']', '^', '_', '`', 'a'}} You can also reshape it: view.reshape(std::vector{25, 2}); fmt::print("reshaped: {}\n", view); Printing reshaped: {{'0', '1'}, {'2', '3'}, {'4', '5'}, {'6', '7'}, {'8', '9'}, {':', ';'}, {'<', '='}, {'>', '?'}, {'#', 'A'}, {'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I'}, {'J', 'K'}, {'L', 'M'}, {'N', 'O'}, {'P', 'Q'}, {'R', 'S'}, {'T', 'U'}, {'V', 'W'}, {'X', 'Y'}, {'Z', '['}, {'\', ']'}, {'^', '_'}, {'`', 'a'}}
Byte of word returns one less than what it should be
My professor is having us make our own version of the functions mystrcat, mystrlen and mystrcopy. I have a problem where the word is returning the wrong amount of bytes. Or, rather, I think it is. The phrase: "HELLO WORLD!" , if I am not wrong, should return 13 bytes? Like in: const char char_literal[] = { 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0' }; // 1 2 3 4 5 6 7 8 9 10 11 12 13 I wrote the function myStrLen() that returns ({ 'H', 'E', 'L', 'L', 'O', ' '}) as 6, but because it is alone, there should be a '\0' at the end right? So should I be returning i + 1 or is 6 correct? int myStrLen(char stringInput[]){ for(int i = 0; ; i++){ if(stringInput[i] == '\0'){ return i;
If your professor tells you to write a function that behaves exactly as strlen, then it shall not count the string termination character: std::size_t strlen( const char* str ); Returns the length of the given byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character. ... So returning i is correct, besides the fact that the "original" strlen takes a const input parameter and returns a size_t...
Check if something at position X of array is a char
if (selecao > 0 && selecao < 5) { if (assentosP[selecao] == 'O') { printf("Test"); assentosP is a char array, that has a bunch of either D or O letters (D by default) char assentosP[2][4] = { {'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]... {'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]... }; What I need to know is a way to make AssentosP find out if the char in position selecao of the array is either D or O. When I try it, it gives me a error iso c++ forbids comparison between pointer and integer c
You defined a two dimensional array char assentosP[2][4] = { {'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]... {'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]... }; Thus expression assentosP[selecao] has type char[4]. You may not compare such an array with a character (or an integer after the integer promotion) like this if (assentosP[selecao] == 'O') { Take into account that if an array declared as having 4 elements then the valid range of indices is [0, 3]. Thus this statement if (selecao > 0 && selecao < 5) { looks incorrectly.
how to print const char pointer in c++
Hello I'm trying to print const * char string ended with '\0' But when I rying to use cout const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'}; const char * i = command; cout<<i; It give me some squares "[][][]" So I tried to iterate whole chars while(*i != '\0'){ cout << *i; i++; } It give me : S[][][]. Progress? When I print *i it give me 'S' but *(i+1) '[][]'. Is problem with this that I had 8 bit chars, and int is 32 bits? Or if I doing something wrong? Solved: char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values. -Now I need a coffie..
This is the problem const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'}; since the ascii values 11, 12, 13, 14, 15 are not printable characters, try this const char command[14] = {'S', 'T', 'E', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '\0'}; Image from http://www.asciitable.com/
Solved: char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values. -Now I need a coffie.. Sorry I stuck with this.
Generate "characters" based on a probability
I need a little help with my tetris game that I am coding in C++ , here is my problem: The list of tetris blocks are 7 types: {'I', 'J', 'L', 'S', 'Z', 'O', 'T'} and i need to pick ONE of the above character such that S and Z are selected with probability 1/12 each, and the other blocks are selected with probability 1/6 each. What is my best way to generate the blocks according to these probabilities?
Declare and define a twelve-item array with a single occurrence of S and Z; two occurrences each of the rest; and pick a random element from that.
Create an array like so: const char* block_distribution = {'I', 'I', 'J', 'J', 'L', 'L', 'S', 'Z', 'O', 'O', 'T', 'T'}; Then pick one element from that array using a uniform distribution.
put those characters in a character array and generate random values from 0 to 6 with srand() than you can get a random char myArray[] = {'I', 'J', 'L', 'S', 'Z', 'O', 'T'}; and then get values with myArray[ (rand()%5)+1 ]
The easiest solution I can think of is to use numeric ranges and pseudo-random numbers. So, first assign ranges to each letter: I: 0 to 1 J: 1 to 2 L: 2 to 3 S: 3 to 3.5 Z: 3.5 to 4 O: 4 to 5 T: 5 to 6 then generate pseudo-random numbers within the range 0 to 6. Whichever part of the range that the number falls within, that's the letter you choose. Unfortunately I do not know C++ so I cannot provide you with code, but I don't think it should be too difficult to implement.