【问题标题】:Converting c++ code into arduino with string and stream header files and functions使用字符串和流头文件和函数将 c++ 代码转换为 arduino
【发布时间】:2016-04-18 16:32:11
【问题描述】:

我正在我的 arduino 板上开发一个项目,为了展示我在 C++ 中编写代码的想法。但据我所知,在 C++ 中找到的 arduino IDE 上没有找到某些库文件和函数。

我附上下面的代码。 我想将整个代码转换为 arduino,其中只有 convertToEnglish 将作为函数保留在 arduino 中。 我尝试用字符串库和其他Stream.h 头文件替换头文件和其他函数,但几乎一切都徒劳无功。 因此,要解决这个问题,请引用我的解决方案。 我尝试使用引用的Standard c++,但 getline 函数仍然报告错误,指出 cin 未在范围内声明。

#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;
}

错误信息: Arduino:1.6.8(Windows 8.1),板:“Arduino/Genuino Uno”

E:\brain\arduino\sketch_mar15a\Blink\Blink\Blink.ino:在函数'int main()'中:

Blink:19: 错误:'cin' 未在此范围内声明

 getline(cin, input);

         ^

退出状态 1 'cin' 未在此范围内声明

此报告将包含更多信息 “在编译期间显示详细输出” 文件 -> 首选项中启用的选项。

【问题讨论】:

  • 到底是什么问题?
  • 在arduino ide中找不到string和sstream头文件。如果不是 wat 是用于产生相同输出的其他头文件
  • Vectors in Arduino的可能重复
  • 你应该切换currentLetter的测试顺序:在index &lt; characters之前和currentLetter != morseCode[index]之后。否则你可以访问morseCode[26]

标签: c++ function c++11 arduino header-files


【解决方案1】:

对我来说,不清楚哪些可以使用,哪些不能。

假设你不能使用std::vector,但是你可以使用好的旧C字符串(char *)函数和输入/输出函数,我准备了以下C风格的例子

#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;
 }

如果你可以使用std::vectorstd::string,真的可以变得更简单。

p.s.:对不起我的英语不好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多