简体   繁体   English

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

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

This is my program: 这是我的程序:

#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);
}

Where ever I use cin.get() I get awkward spacing when I compile the program. 在任何使用cin.get() ,编译程序时都会出现尴尬的间距。 Like this: 像这样:

在此处输入图片说明

Yet I need to use cin.get() to allow the user to roll the dice for himself/herself and the computer. 但是我需要使用cin.get()来允许用户为自己和计算机掷骰子。 Not sure if I'm using it correctly. 不知道我是否使用正确。

Well, if you press enter then it stays there printed (that's where this line comes from). 好吧,如果您按Enter键,它将保留在那里打印(这是此行的来源)。 Use some no-echoing function, ncurses got it. 使用一些无回声功能,ncurses做到了。 Oh and about skipping first input, use cin.ignore() right before your get. 哦,关于跳过第一个输入,请在获取之前使用cin.ignore()。

//Do I need an else here? //我需要别的吗?

Nah h

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

相关问题 我尝试在 mac os 终端中运行我的 python 程序,但在我成功安装 Beautifulsoup4 后不断收到以下反馈 - I tried running my python program in a mac os terminal but keep getting the feedback below after I have successfully installed Beautifulsoup4 当我尝试在终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它 - I keep getting "ModuleNotFound" error when i try to run code on my terminal even though i installed it 如何让我的 docker 映像继续运行? - How to get my docker image to keep running? 在不禁用cout / cin的情况下获取stdout内容 - Getting stdout contents without disabling cout/cin 在c ++中使用system()时,我的程序被卡住了,如何使它继续进行? - When using system() in c++ my program gets stuck, how do I get it to proceed? 在Ubuntu终端中,如何保持python程序运行? - In an Ubuntu terminal how can I keep a python program going? Java程序未从终端获取输出 - Java program not getting output from terminal 运行程序时终端挂起 - Terminal hangs when I run my program 继续获取:打开终端时没有这样的文件或目录 - Keep getting: No such File or directory when I open the Terminal 如何在一行中从终端运行我的 .c 程序? - How do I run my .c program from terminal in one line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM