mlstudent
mlstudent

Reputation: 958

Userinput doesn't work when I enter "..."

I'm doing a morse to words and backwards. The program works except for one case, when I type "..." in the user input and then press space it becomes extremely small and the program doesn't return anything. So for instance if I try typing ... to return S it works, but if I try typing ... ... for SS it doesn't work. I'm using stanford libraries for getting user input and handling maps, but the same thing happens when I use the standard libraries.

#include <iostream>
#include <string>
#include "console.h"
#include "random.h"
#include "map.h"
#include "simpio.h"

using namespace std;


int main() {
    string input = getLine("Please enter words or morse code");
    Map<string, string> toMorse;
    toMorse.put("A", ".-");
    toMorse.put("B", "-...");
    toMorse.put("C", "-.-.");
    toMorse.put("D", "-..");
    toMorse.put("E", ".");
    toMorse.put("F", "..-.");
    toMorse.put("G", "--.");
    toMorse.put("H", "....");
    toMorse.put("I", "..");
    toMorse.put("J", ".---");
    toMorse.put("K", "-.-");
    toMorse.put("L", ".-..");
    toMorse.put("M", "--");
    toMorse.put("N", "-.");
    toMorse.put("O", "---");
    toMorse.put("P", ".--.");
    toMorse.put("Q", "--.-");
    toMorse.put("R", ".-.");
    toMorse.put("S", "...");
    toMorse.put("T", "-");
    toMorse.put("U", "..-");
    toMorse.put("V", "...-");
    toMorse.put("W", ".--");
    toMorse.put("X", "-..-");
    toMorse.put("Y", "-.--");
    toMorse.put("Z", "--..");

    Map<string, string> toSentence;
    for(char c0='A'; c0<='Z'; c0++)
    {
        string c="";
        c.append(1, c0);
        //cout<<toMorse.get(c)<<endl;
        toSentence.put(toMorse.get(c), c);
    }
    if(input[0]=='.' || input[0]=='-')
    {
        string toLetter;
        for(int i=0; i<input.length(); i++)
        {
            if(input[i] != ' ' && i<input.length()-1)
            {
                toLetter.append(input.substr(i, 1));
            }
            else if(input[i] != ' ' && i==input.length()-1)
            {
                toLetter.append(input.substr(i, 1));
                cout << toSentence.get(toLetter);
            }
            else
            {
                cout << toSentence.get(toLetter);
                toLetter = "";
            }
        }
    }
    else
    {
        for(int i=0; i<input.length(); i++)
        {

            if(toMorse.containsKey(input.substr(i,1)))
            {
                cout << toMorse.get(input.substr(i,1)) << " ";
            }
        }
    }
   return 0;
}

Upvotes: 0

Views: 142

Answers (3)

Mooing Duck
Mooing Duck

Reputation: 66922

Your console is "helpfully" converting three period characters (...) as an ellipsis (…) as allowed by the unicode standard. Since you're using std::string, (and I assume linux, since Windows doesn't do this), it must be converting to UTF-8. The unicode character is codepoint U+2026, which in UTF-8 is 0xE2 0x80 0xA6, or, as a cstring "\xE2\x80\xA6".

Source: "Unicode recognizes a series of three period characters (U+002E) as compatibility equivalent (though not canonical) to the horizontal ellipsis character." -http://en.wikipedia.org/wiki/Ellipsis

Upvotes: 1

Michael Dorgan
Michael Dorgan

Reputation: 12515

Sounds like your console is changing 3 periods to an ellipses run, like a word processing program might do. Not sure how to fix this though except to scan for the Unicode or whatever value the console is creating :)

Upvotes: 2

Grimm The Opiner
Grimm The Opiner

Reputation: 1806

I've run through and replaced your stuff with std:: classes where necessary, and for me hardcoding the input string "... ..." results in the output of "SS" so your actual morse translation is ok (assuming you want it to discard spaces) - but capturing the string with cin truncates the input at the space.

Upvotes: 0

Related Questions