hi, guys, I'm having trouble in running my code. my first problem was that the winner was not always showing so I try to add if statement to declare the winner. then, I added a do while in order to restart the game and the return 0 shows an error.
//System Libraries
#include <iostream> //Input/Output Library
#include <string> //
#include<fstream> //
#include <cstdlib> // Random Function
#include <ctime> // Time function
#include <iomanip> // format function
usingnamespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
//Function Prototypes
//Execution Begins Here!
int main(int argc, char** argv) {
do {
srand(static_cast<unsignedint>(time(0)));//seed random number generator
//The main menu of the game
char ans;
string P1,P2; // the names of players
float bet; // the bet for the game
cout<<"Welcome to street craps\n"<<endl;
cout<<"Enter player 1 and players 2 a.k.a the shooter Names"<<endl;
cin>>P1; // the name of player 1
cin>>P2; // the name of player 2
cout<<"\n"; // add a new line
cout<<"shooter make bet"<<endl;
cin>>bet; // the money that is being bet
while(cin && bet < 1.0f) // checks if the money being bet is more than a dollar
{
cout<< "The minimum bet is $1.00 Please place a higher bet." <<endl;
cin >> bet;
} if(!cin){ // make sure for a valid input
cout<< "input invalid."<<endl;
}
// declare vaules
char die1=rand()%6+1;//[1,6] dice 1
char die2=rand()%6+1;//[1,6] dice 2
char sum=die1+die2; // adds both dice to get sum
// the logic of the game
if(sum==7 || sum==11){ // checks the sum of dice roll 7 or 11
cout<<P2<<" wins $"<<bet<<endl;
}
elseif(sum==2){ // checks if the sum of the eyes roll 2
cout<<P1<<" wins $"<<bet<<endl;
}
elseif(sum==3){ // checks if the sum of the eyes roll 2
cout<<P1<<" wins $"<<bet<<endl;
}
elseif(sum==12){ // checks if the sum of the eyes roll 2
cout<<P1<<" wins $"<<bet<<endl;
}
else{ // checks if the dice roll any other number
bool rollAgn;
do{
die1=rand()%6+1;//[1,6] dice 1
die2=rand()%6+1;//[1,6] dice 2
char sumAgn=die1+die2; // the sum of the dice being roll again
if(sumAgn==7){ // checks if sum of dice if 7
P2;rollAgn=false;
cout<<P1<<" wins $"<<bet<<endl;
}
elseif(sum==sumAgn){ // checks if the sum and the roll again is the same
P2;rollAgn=false;
cout<<P2<<" wins $"<<bet<<endl;
}
else rollAgn=true; // makes the dice roll again if the sum of the dice roll any other dice
} while (rollAgn);
cout<<"Another game? Y(es) or N(o)" << endl;
cin >> ans;
while ((ans == 'N') && ( ans == 'n')){
cout<<"thank for playing"<<endl;
cin>> ans;
}
}
}
//Exit stage right or left!
return 0;
}
my error
main.cpp: In function 'int main(int, char**)':
main.cpp:93:8: error: expected 'while' before numeric constant
return 0;
^
main.cpp:93:8: error: expected '(' before numeric constant
main.cpp:93:9: error: expected ')' before ';' token
return 0;
^
//System Libraries
#include <iostream> //Input/Output Library
#include <string> //
#include<fstream> //
#include <cstdlib> // Random Function
#include <ctime> // Time function
#include <iomanip> // format function
usingnamespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
//Function Prototypes
//Execution Begins Here!
int main(int argc, char **argv)
{
do {
srand(static_cast < unsignedint >(time(0))); //seed random number generator
//The main menu of the game
char ans;
string P1, P2; // the names of players
float bet; // the bet for the game
cout << "Welcome to street craps\n" << endl;
cout << "Enter player 1 and players 2 a.k.a the shooter Names" << endl;
cin >> P1; // the name of player 1
cin >> P2; // the name of player 2
cout << "\n"; // add a new line
cout << "shooter make bet" << endl;
cin >> bet; // the money that is being bet
while (cin && bet < 1.0f) // checks if the money being bet is more than a dollar
{
cout << "The minimum bet is $1.00 Please place a higher bet." << endl;
cin >> bet;
}
if (!cin) { // make sure for a valid input
cout << "input invalid." << endl;
}
// declare vaules
char die1 = rand() % 6 + 1; //[1,6] dice 1
char die2 = rand() % 6 + 1; //[1,6] dice 2
char sum = die1 + die2; // adds both dice to get sum
// the logic of the game
if (sum == 7 || sum == 11) { // checks the sum of dice roll 7 or 11
cout << P2 << " wins $" << bet << endl;
} elseif (sum == 2) { // checks if the sum of the eyes roll 2
cout << P1 << " wins $" << bet << endl;
} elseif (sum == 3) { // checks if the sum of the eyes roll 2
cout << P1 << " wins $" << bet << endl;
} elseif (sum == 12) { // checks if the sum of the eyes roll 2
cout << P1 << " wins $" << bet << endl;
} else { // checks if the dice roll any other number
bool rollAgn;
do {
die1 = rand() % 6 + 1; //[1,6] dice 1
die2 = rand() % 6 + 1; //[1,6] dice 2
char sumAgn = die1 + die2; // the sum of the dice being roll again
if (sumAgn == 7) { // checks if sum of dice if 7
P2;
rollAgn = false;
cout << P1 << " wins $" << bet << endl;
} elseif (sum == sumAgn) { // checks if the sum and the roll again is the same
P2;
rollAgn = false;
cout << P2 << " wins $" << bet << endl;
} else
rollAgn = true; // makes the dice roll again if the sum of the dice roll any other dice
} while (rollAgn);
cout << "Another game? Y(es) or N(o)" << endl;
cin >> ans;
while ((ans == 'N') && (ans == 'n')) {
cout << "thank for playing" << endl;
cin >> ans;
}
}
}
//Exit stage right or left!
return 0;
}
$ indent -kr -nut -ts2 -i2 -l120 -cli0 foo.cpp
indent: foo.cpp:87: Error:Stmt nesting error.
indent: foo.cpp:88: Error:Unexpected end of file
Yes, you really are missing at least one closing brace.
And since the very first thing in main is a 'do', then the compiler complaining about 'expected while' is entirely appropriate.