簡體   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