find - NOTE:(getline was not the issue) C++ getline() stops working in user defined function but works in main function -


note: solved, problem not getline() find function improperly filled array!

i've looked several questions before posting own, not find answer problem. first question posted, did research , tried other solutions other questions before posting own. not entirely sure isn't duplicate. apologies! thank understanding in advance!

i trying use getline() (c++) user input. works fine in main, not in user defined function. thought might have buffer, used cin.ignore() suggested in:

c++ getline method not working

and checked:

how getline work cin?

to make sure understood getline(). program still not work correctly.

my program takes english text string user input (console input) , converts morse code , outputs result string (console output).

basically problem this:

getline works in main function both strings , strings spaces ex: "this" , "this code".

however, in user defined function, works strings without spaces ex: "this".

thanks help! code snippets below!

#include <iostream>; #include <stdio.h>; #include <ctype.h>;  using namespace std;   string texttomorse(const string alphabet, const string morsealphabet[]);  int main() {     const string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789.,?";     const string morsealphabet[39] = {".-","-...","-.-.","-..",".","..-.","--.","....","     ..",".---","-.-",".-..","--","-.","---",".--.",     "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---",     "...--","....-",".....",     "-....","--...","---..","----.",".-.-.-","--..--","..--.."};     int userselection;     string resultstring;       cout << "text morse code or morse code text program" << endl << endl;     cout << "please select option typing integer shown: " << endl << endl;     cout << "type(selects option) 1 decode morse code english text" << endl;     cout << "type(selects option) 2 encode english text morse code" << endl;     cout << "type(select option) other integer not 1 or 2 quit" << endl << endl;     cin >> userselection;     while(userselection == 1 || userselection == 2)    {          if(userselection == 1)         {              resultstring = texttomorse(alphabet, morsealphabet); // function use                                                                   // getline() not work             cout << endl << "this morse code decoded english text: " << endl << endl;             cout << resultstring << endl << endl << endl << endl;         }    }      return 0; }  // not work string texttomorse(const string alphabet, const string morsealphabet[]) {     string userinput;     cout << endl << "enter english text encode morse code,     space between words: " << endl << endl;      cin.ignore();     getline(cin,userinput); //code works strings without spaces,                              //but breaks others. ex: "this" works input                             //but "this code" breaks , console seems freeze                             // crashes out      cin.clear();      // rest of code, program breaks before this.      string encodedenglishtext = "";      for(int = 0; < userinput.length(); i++)     {         userinput[i] = toupper(userinput[i]);     }      for(int = 0; < userinput.length(); i++)     {        encodedenglishtext += morsealphabet[alphabet.find(userinput[i])];        encodedenglishtext += " "; // spacing added output clarity         if(userinput[i] == ' ')        {            encodedenglishtext += "  "; // spacing added output clarity        }     }      return encodedenglishtext; } 

however if edit code , input main , pass in parameter, works.

#include <iostream>; #include <stdio.h>; #include <ctype.h>;  using namespace std;   string texttomorse(const string alphabet, const string morsealphabet[], string userinput); int main() {     const string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789.,?";     const string morsealphabet[39] = {".-","-...","-.-.","-..",".","..-.","--.","....","     ..",".---","-.-",".-..","--","-.","---",".--.",     "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---",     "...--","....-",".....",     "-....","--...","---..","----.",".-.-.-","--..--","..--.."};     int userselection;     string resultstring;       cout << "text morse code or morse code text program" << endl << endl;     cout << "please select option typing integer shown: " << endl << endl;     cout << "type(selects option) 1 decode morse code english text" << endl;     cout << "type(selects option) 2 encode english text morse code" << endl;     cout << "type(select option) other integer not 1 or 2 quit" << endl << endl;     cin >> userselection;     while(userselection == 1 || userselection == 2)    {          if(userselection == 1)         {             string userinput;             cout << endl << "enter english text encode morse code,             space between words: " << endl << endl;              cin.ignore();             getline(cin,userinput); //code works both "this" , "this code"              cin.clear();             resultstring = texttomorse(alphabet, morsealphabet, userinput); //function modified                                                                             //to take 1 more                                                                            //parameter             cout << endl << "this morse code decoded english text: " << endl << endl;             cout << resultstring << endl << endl << endl << endl;         }    }      return 0; }  string texttomorse(const string alphabet, const string morsealphabet[], string userinput) {     //code, program works.      string encodedenglishtext = "";      for(int = 0; < userinput.length(); i++)     {         userinput[i] = toupper(userinput[i]);     }      for(int = 0; < userinput.length(); i++)     {        encodedenglishtext += morsealphabet[alphabet.find(userinput[i])];        encodedenglishtext += " "; // spacing added output clarity         if(userinput[i] == ' ')        {            encodedenglishtext += "  "; // spacing added output clarity        }     }      return encodedenglishtext; } 

i didn't include of code, parts felt relevant question.

by works mean:

getline takes input. getline assigns string such "this" , "this code" variable userinput when used in main function.

it assigns strings without spaces such "this" when used in user defined function. in function, reason not work when enter string "this code" or string space inbetween.

note: program not finished, plan add other methods reverse (as seen in code user options, these not yet implemented or defined, code still runs , compiles problem facing.

the problem there no morse code space.

make verification:

    int n = alphabet.find(userinput[i]);     encodedenglishtext += (n == string::npos) ? "  ":  morsealphabet[n]; 

then work.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -