2
votes

I'm developing a project on my arduino board, for my projection of the idea I wrote the code in C++. But certain library files and functions weren't found on the arduino IDE which are found in C++ to my knowledge.

I'm attaching the code below. I want to convert the whole code into arduino in which only the convertToEnglish will remain as a function in the arduino . I tried replacing the header files and others functions with string library and other Stream.h header file but almost everything ended up in vain. Hence to over come this please quote me a solution. I tried using Standard c++ as quoted but still the getline fucntion reports an error stating that the cin was not declared in scope.

#include <StandardCplusplus.h>
#include <system_configuration.h>
#include <unwind-cxx.h>
#include <utility.h>



#include <iostream>
 #include <string>
#include <sstream>
using namespace std;

 string convertToEnglish(string morse, string const morseCode[]);

int main()
{
string input = "";
cout << "Please enter a string in morse code: ";
getline(cin, input);

string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

cout << convertToEnglish(input, morseCode) << endl;


return 0;
}

 string convertToEnglish(string morse, string const morseCode[])
 {
 string output = "";
 string currentLetter = "";
 istringstream ss(morse);

size_t const characters = 26;

while(ss >> currentLetter)
{
    size_t index = 0;
    while(currentLetter != morseCode[index] && index < characters)
    {
        ++index; //increment here so we don't have to decrement after the loop like if we put in the condition
    }

    output += 'A' + index;
}

return output;
}

error message: Arduino: 1.6.8 (Windows 8.1), Board: "Arduino/Genuino Uno"

E:\brain\arduino\sketch_mar15a\Blink\Blink\Blink.ino: In function 'int main()':

Blink:19: error: 'cin' was not declared in this scope

 getline(cin, input);

         ^

exit status 1 'cin' was not declared in this scope

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

1
What exactly is the problem?SergeyA
The string and sstream header files cannot be found in arduino ide. If not wat are the other header files to use to produce the same outputsatabios
Possible duplicate of Vectors in Arduinowillll
You shoud switch the order of the tests of currentLetter: before index < characters and after currentLetter != morseCode[index]. Otherwise you can acces morseCode[26]max66

1 Answers

0
votes

It' not clear, for me, what you can use and what you can't.

Supposing that you can't use std::vector, but you can use good old C string (char *) function and input/output functions, I've prepared the following C-style example

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdexcept>


char * convertToEnglish (char *                      output,
                         std::size_t                 dimOut,
                         char const * const          input,
                         char const * const * const  morseCode,
                         std::size_t                 numMorseCodes)
 {
   if ( (nullptr == output) || (0U == dimOut) || (nullptr == input)
       || (nullptr == morseCode) || (0U == numMorseCodes) )
      throw std::runtime_error("invalid input in cTE()");

   std::size_t   posOut = 0U;
   std::size_t   index;
   char const *  ptrI0;
   char const *  ptrI1;
   char          currentLetter[5];

   std::memset(output, 0, dimOut);

   for ( ptrI0 = input ; nullptr != ptrI0 ; ptrI0 = ptrI1 )
    {
      ptrI1 = std::strpbrk(ptrI0, " \n");

      if ( nullptr == ptrI1 )
       {
         // last character if the string isn't cr terminated
         if ( sizeof(currentLetter) <= strlen(ptrI0) )
            throw std::runtime_error("to length last char in cTE()");

         std::strcpy(currentLetter, ptrI0);
       }
      else
       {
         if ( sizeof(currentLetter) <= (ptrI1 - ptrI0) )
            throw std::runtime_error("to length char in cTE()");

         std::memset(currentLetter, 0, sizeof(currentLetter));
         std::strncpy(currentLetter, ptrI0, (ptrI1 - ptrI0));

         if ( '\n' == *ptrI1 )
             ptrI1 = nullptr;
         else 
            ++ptrI1;
       }

      for ( index = 0U
           ;    (index < numMorseCodes)
             && strcmp(currentLetter, morseCode[index])
           ; ++index )
       ; 

      if ( numMorseCodes <= index )
         throw std::runtime_error("no morse code in cTE()");

      output[posOut] = 'A' + index;

      if ( ++posOut >= dimOut )
         throw std::runtime_error("small out buffer in cTE()");
    }

   return output;
 }


int main ()
 {
   constexpr char const * morseCode[] = {".-", "-...", "-.-.",
      "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
      "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
      "...-", ".--", "-..-", "-.--", "--.."};

   constexpr std::size_t  numMorseCodes
      = sizeof(morseCode)/sizeof(morseCode[0]);

   char *       input = nullptr;
   std::size_t  dim   = 0U;

   if ( getline(&input, &dim, stdin) == -1 )
      throw std::runtime_error("error reading input");

   char  output[1024];

   std::cout << convertToEnglish(output, sizeof(output), input,
                                 morseCode, numMorseCodes) << std::endl;

   return EXIT_SUCCESS;
 }

If you can use std::vector or std::string, can be made really simpler.

p.s.: sorry for my bad English