我从cin.get()继续在我的程序中获得一个空格

时间:2015-07-09 19:21:58

标签: terminal compilation c++

这是我的计划:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#define RAND(a,b) (a + rand()% (b-a+1))


int main()
{
    int num; 
    int computer_total = 0, players_total = 0;
    string name;

    srand(time(NULL));

    for(;;)
    {
        cout <<"What's your name? ";
        cin >> name;
        cin.get();
        if(name == "stop")exit(0);

        for(int i =0; i < 5; i++)
        {
            cout << name <<" roll the dice.\n";
            cin.get();                         //skips the cin.get() the first time aroun
            num = RAND(1,6);                   //Also makes akward spacing 
            players_total += num;
            cout <<"You got: " << num << endl;

            cout <<"Now it's the computers turn.\n";
            cin.get();
            num = RAND(1,6); 
            computer_total += num;
            cout <<"Computer got: " << num << endl;
        }

        cout <<"Total of computers dice: " << computer_total << endl;
        cout <<"Total of your dice: " << players_total << endl;

        if(computer_total == players_total)cout <<"Its a tie! \n"; 
        else if(computer_total > players_total)cout <<"Computer won.\n"; 
        else if(computer_total < players_total)cout <<"You won!\n";  
        //Do I need an else here?

    }

    return(0);
}

我在哪里使用cin.get()编译程序时,我的间距会变得很笨拙。像这样:

enter image description here

然而,我需要使用cin.get()来允许用户为自己和计算机掷骰子。不确定我是否正确使用它。

1 个答案:

答案 0 :(得分:0)

好吧,如果按下回车键,那么它就会一直打印(这就是这条线的来源)。使用一些无回声功能,ncurses得到它。 哦,关于跳过第一个输入,在你得到之前使用cin.ignore()。

  

//我还需要其他吗?