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. Like this:
Yet I need to use cin.get()
to allow the user to roll the dice for himself/herself and the computer. 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). Use some no-echoing function, ncurses got it. Oh and about skipping first input, use cin.ignore() right before your get.
//Do I need an else here?
Nah
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.