How do you compare parts of a string with another string?
Nov 5, 2015 at 6:37pm UTC
I've just begun programmimg and I am trying to do a Morse encrypting program but I don't know how to compare part of a string with another one(in this case a letter) if I dont, the programm just jumps all the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <string>
using namespace std;
int main()
{
int count = 0, length;
string input;
cout << "changes text to morse and morse to text; \n" ;
cout << "Between letters: |; Between words: ~~ \n" ;
getline (cin, input);
length = (int )input.length();
for (count = 0; count < length; count++)
{
input[count] = tolower(input[count]);
//Letters
if (input == "a" ) {cout << ".-" ; cout << "|" ;}
if (input == "b" ) {cout << "-..." ; cout << "|" ;}
if (input == "c" ) {cout << "-.-." ; cout << "|" ;}
if (input == "d" ) {cout << "-.." ; cout << "|" ;}
if (input == "e" ) {cout << "." ; cout << "|" ;}
if (input == "f" ) {cout << "..-." ; cout << "|" ;}
if (input == "g" ) {cout << "--." ; cout << "|" ;}
if (input == "h" ) {cout << "...." ; cout << "|" ;}
if (input == "i" ) {cout << ".." ; cout << "|" ;}
if (input == "j" ) {cout << ".---" ; cout << "|" ;}
if (input == "k" ) {cout << "-.-" ; cout << "|" ;}
if (input == "l" ) {cout << ".-.." ; cout << "|" ;}
if (input == "m" ) {cout << "--" ; cout << "|" ;}
if (input == "n" ) {cout << "-." ; cout << "|" ;}
if (input == "o" ) {cout << "---" ; cout << "|" ;}
if (input == "p" ) {cout << ".--." ; cout << "|" ;}
if (input == "q" ) {cout << "--.-" ; cout << "|" ;}
if (input == "r" ) {cout << ".-." ; cout << "|" ;}
if (input == "s" ) {cout << "..." ; cout << "|" ;}
if (input == "t" ) {cout << "-" ; cout << "|" ;}
if (input == "u" ) {cout << "..-" ; cout << "|" ;}
if (input == "v" ) {cout << "...-" ; cout << "|" ;}
if (input == "w" ) {cout << ".--" ; cout << "|" ;}
if (input == "x" ) {cout << "-..-" ; cout << "|" ;}
if (input == "y" ) {cout << "-.--" ; cout << "|" ;}
if (input == "z" ) {cout << "--." ; cout << "|" ;}
//Spaces between letters
if (input == " " ) {cout << "~~" ; cout << "|" ;}
//Numbers
if (input == "1" ) {cout << ".----" ; cout << "|" ;}
if (input == "2" ) {cout << "..---" ; cout << "|" ;}
if (input == "3" ) {cout << "...--" ; cout << "|" ;}
if (input == "4" ) {cout << "....-" ; cout << "|" ;}
if (input == "5" ) {cout << "....." ; cout << "|" ;}
if (input == "6" ) {cout << "-...." ; cout << "|" ;}
if (input == "7" ) {cout << "--..." ; cout << "|" ;}
if (input == "8" ) {cout << "---.." ; cout << "|" ;}
if (input == "9" ) {cout << "----." ; cout << "|" ;}
if (input == "0" ) {cout << "-----" ; cout << "|" ;}
}
cout << "Done" ;
}
Nov 5, 2015 at 7:20pm UTC
Try:
1 2
if (input[count] == 'a' ) { ... }
...
Nov 7, 2015 at 11:52am UTC
Thanks megatron 0
Topic archived. No new replies allowed.