c++ - Why is this if statement not running when the values are equal to each other? -
i trying implement code takes morse code 3 spaces between each letter , gets first morse translation , compares array containing morse code translations. goes buffer == morsem[j]
, buffer should equal .-
, morsem[j]
should equal .-
; if j = 0
, both do, not executing if
's block. idea why?
#include <iostream> using namespace std; int main(){ char morsem[26][5] = {{".-"},{"-..."},{"-.-."},{"-.."},{"."},{"..-."},{"--."},{"...."},{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},{"---"},{".--."},{"--.-"},{".-."},{"..."},{"-"},{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}}; char alpha[27] = "abcdefghijklmnopqrstuvwxyz"; char morse[] = ".- ...."; int length; int first_counter = 0; while(morse[first_counter] != '\0'){ first_counter++; } char *new_morse = new char [first_counter]; int counter = 0; char *buffer = new char [5]; for(int x = 0; x < first_counter; x++){ if(morse[x] != ' '){ buffer[counter] = morse[x]; counter++; } if(morse[x] == ' '&& morse[x+1] == ' ' && morse[x+2] == ' '){ for(int j=0;j<27;j++){ if(buffer == morsem[j]){ cout << alpha[j] << endl; break; } } for(int = 0; < first_counter; i++){ new_morse[i] = morse[i+x+3]; } } } delete[] buffer; cout << morse << endl; cout << new_morse << endl; }
if(buffer == morsem[j]){
comparing pointer values , not strings. need compare "what pointers pointing at".
since in c++, using std::string
may easier char char
buffers...
Comments
Post a Comment