Two small problems with while loop and char space
Oct 6, 2015 at 10:42pm UTC
Hello everyone, first time posting on this forum but have used it to help me many times over the last month.
So I'm making a dice game but I can't seem to stop the while loop and I'm not sure how to have my full name (including the space) appear when the game is won. Everything else seems to be functioning perfectly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
ofstream outFile;
outFile.open("game.dat" );
srand(time(0));
outFile << ((rand() % 6) + 1) << endl;
outFile << ((rand() % 6) + 1) << endl;
outFile.close();
cout << "Please print your name \n" << endl;
char name[50];
cin >> name;
cout << "\n" << endl;
cout << "Game Rules:" << endl;
cout << "*******************************************************************************" << endl;
cout << "1. If the total of both dice are 2, 3 or 12 the house wins" << endl;
cout << "2. If the total of both dice is 7 or 11 the player wins" << endl;
cout << "3. If the total of both dice are 4, 5, 6, 8, 9 or 10 then the game continues" << endl;
int number;
int number2;
ifstream inFile;
inFile.open("game.dat" );
inFile >> number;
inFile >> number2;
cout << "\n" ;
cout << "Number 1 is: " << number << endl;
cout << "Number 2 is: " << number2 << endl;
inFile.close();
int total;
total = number + number2;
cout << "The total is: " << total << endl;
if (total == 2 || total == 3 || total == 12)
{
cout << name << endl;
cout << "The House Wins!" << endl;
}
else if (total == 7 || total == 11)
{
cout << name << endl;
cout << "The Player Wins!" << endl;
}
else if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10)
{
int totalToss = 1;
int target = total;
char choice;
cout << target << " is now the target value.\n" ;
while (target == total)
{
cout << "Would you like to roll again? Enter Y for yes and N for no\n" ;
cin >> choice;
totalToss++;
int total2;
if (choice == 'Y' )
{
ofstream outFile;
outFile.open("game.dat" );
srand(time(0));
outFile << ((rand() % 6) + 1) << endl;
outFile << ((rand() % 6) + 1) << endl;
outFile.close();
ifstream inFile;
inFile.open("game.dat" );
inFile >> number;
inFile >> number2;
cout << "\n" ;
cout << "Number 1 is: " << number << endl;
cout << "Number 2 is: " << number2 << endl;
inFile.close();
total2 = number + number2;
cout << "The total is: " << total2 << endl;
if (total2 == target)
{
cout << "Player Wins!\n" ;
cout << "You have rolled 2 dice: " << totalToss << " times" << endl;
}
else if (total2 == 7)
{
cout << "House Wins!\n" ;
cout << "You have rolled 2 dice: " << totalToss << " times" << endl;
}
}
else
{
cout << "You have lost, please try again!\n" ;
}
}
}
return 0;
}
Oct 7, 2015 at 1:28am UTC
I have changed it to a do while loop and the while states
while (target != total2);
and the character was just cin.getline(name, 50).
Topic archived. No new replies allowed.