2017-07-19 54 views
-3
#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
    int x,y; 
    char direction ; 
    char str1[10]; 
    cout<<"Please enter the initial position of the bot"; 
    cin>>x>>y; 
    cout << "\n\n"; 
    cout << "enter initial direction in caps (F-forward,L-left,R-right)"; 
    cin >> direction; 
    //cout << "chosen direction :" << direction ; 
    cout << "\n\n"; 
    cout << "Enter the moves \n(Without space comma seperated in caps eg-F,L,L" ; 
    cin.get(str1,10); 
    cout << "entered moves :" << str1 << endl ; 

    return 0; 
} 

执行之后: $ ./intern 请输入bot1的初始位置2为什么程序在cin后跳过cin.get()?

进入盖(F-向前,L-左,R-右)初始方向F

进入移动 (如果没有盖分隔空间逗号如-F,L,Lentered移动:

KARPAGAVALLI @阿什温〜 $

+1

您是否考虑过使用调试器逐步完成代码? –

+0

我直接编译它在cygwin –

+0

“...编译器退出...” - 不。编译器不会退出,您的*编译*程序。编译器作业在遇到问题之前已经完成很久。 –

回答

0

一个问题在这里:最后

cin >> direction; 

后有在流缓存的额外换行符“\ n”,这是由未来

cin.get(str1,10); 

与“幻觉”,这是阅读没执行。实际上,str1将只是空字符串。

的解决方案是使用

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

只是cin.get(str1,10)之前清除流(请注意,必须#include <limits>)。

+0

谢谢你的代码工程 –