[英]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.