【发布时间】:2017-07-25 10:22:53
【问题描述】:
我一直在做一些练习来学习 c++,并决定将它们集成到 R 中,因为最终我想为 R 函数编写 c++ 后端。 我无法找到从 R 控制台检索用户输入的解决方案。虽然有 Rcpp::Rcout 用于打印和返回输出,但 std::cin.... 似乎没有类似的功能。
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::String cola() {
Rcpp::Rcout << "Pick a drink:" << std::endl << "1 - Espresso" << std::endl << "2 - Americano" << std::endl << "3 - Latte" << std::endl << "4 - Cafe dopio" <<
std::endl << "5 - Tea" << std::endl;
int drink;
std::cin >> drink;
std::string out;
switch(drink) {
case 1: out = "Here is your Espresso";
case 2: out = "Here is your Americano";
case 3: out = "Here is your Latte";
case 4: out = "Here is your Cafe dopio";
case 5: out = "Here is your Tea";
case 0: out = "Error. Choice was not valid, here is your money back.";
break;
default:
if(drink > 5) {out = "Error. Choice was not valid, here is your money back.";}
}
return out;
}
【问题讨论】: