繁体   English   中英

我不断从cin.get()中获取程序空间

[英]I keep getting a space in my program from cin.get()

这是我的程序:

#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() ,编译程序时都会出现尴尬的间距。 像这样:

在此处输入图片说明

但是我需要使用cin.get()来允许用户为自己和计算机掷骰子。 不知道我是否使用正确。

好吧,如果您按Enter键,它将保留在那里打印(这是此行的来源)。 使用一些无回声功能,ncurses做到了。 哦,关于跳过第一个输入,请在获取之前使用cin.ignore()。

//我需要别的吗?

h

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM