Question: I'm trying to have my program open my file before displaying the welcome but I can't figure out why it won't display. I have the

I'm trying to have my program open my file before displaying the welcome but I can't figure out why it won't display. I have the following code

/*

Andrew Nester-Bowles

IT-312 Software Development with C++.Net

10/12/2020

7-1 Final Project "Liar's Dice"

Summary:

This file is the main portion of the program. It combines the

different class files to show the instructions as well as getting the number of players

it initilizes each player creating an array that stores player's scores.

The bidding is also initialized and run through this.

*/

#include "pch.h"

#include

#include

#include

#include

#include

#include

#include "Bid.h"

#include "Player.h"

using namespace std;

int main()

{

/******* Introduction *******/

fstream newfile;

newfile.open("rules.txt", ios::in); //open a file to perform read operation using file object

if (newfile.is_open())

{//checking whether the file is open

string tp;

while (getline(newfile, tp))

{ //read data from file object and put it into string.

cout << tp << endl; //print the data of the string

}

newfile.close(); //close the file object.

}

/******* Ask how many players are going to play *******/

int num;

int i;

int j;

cout << " ***********Game Started!***********" << endl;

cout << "How many players are going to play (2 to 6 players): ";

while (!(cin >> num) || num <= 1 || num > 6)

{ // input validation

cout << "Wrong input! Enter an integer (2 to 6): ";

}

/******* Initialize each player *******/

vector names(num); //dynamic memory allocation

string player_name;

// ask for each player's name

for (i = 0; i < num; i++) {

cout << "Enter name of player #" << i + 1 << ": ";

cin >> player_name;

while (cin.get() != ' ') {};

names[i] = player_name;

}

cout << endl;

// create the class array

Player players[7] = {

Player("null"),

Player("null"),

Player("null"),

Player("null"),

Player("null"),

Player("null"),

Player("null"),

};

// create the array that records each player's score

int n;

vector score(num);

for (n = 0; n < num; n++) {

score[n] = 0;

}

int round = 0;

do {

srand((unsigned)time(NULL));

// initialize player's class

for (i = 0; i < num; i++) {

players[i] = Player(names[i]);

players[i].rollDice(); // Each player rolls his/her dice

//players[i].showDice(); // Show each player's own dice

players[i].countNumber(); // count the amount of each die number in one player's hand

}

/*******Show each player's own dice *******/

string temp;

int quit = 0;

int exist;

char ch;

while (quit == 0) {

exist = 0;

cout << " To check what you've rolled. Enter your name (q to quit if all checked): ";

cin >> temp;

// to check whether the name entered is one of names entered at the beginning

for (i = 0; i < num; i++) {

if (temp.compare(players[i].name) == 0) {

players[i].showDice();

exist++; // record that the name entered matches with the name stored.

// hide the previous player's result so that each player can only see his/her own result.

cout << "Press any key to continue: ";

ch = getchar();

}

}

if (temp.compare("q") == 0) {

quit = 1;

}

else if (exist == 0 && temp.compare("q") != 0)

{ // name entered doesn't exist and also no 'q' entered

cout << "Wrong name! " << endl;

}

}

/******* Calculate total number of each die number *******/

int tot_counter[6] = { 0 };

for (j = 0; j < 6; j++) {

for (i = 0; i < num; i++) {

tot_counter[j] += players[i].counter[j];

}

}

// for(i = 0; i < 6; i++){

// cout << tot_counter[i] << " ";

// }

// cout<

/******* Bidding *******/

int x, y; // x is the number the player bids on; y is how many number x the player thinks there are in total.

char callOut;

int validBid; //index: validBid == 0 means current player bids wrong either on the number or on the amount of the number (either one should be bigger than the bid of previous player)

int liar = 0; // index: liar == 0 means no one calls out 'liar', so the bidding keeps going on one by one

int firstOne = 0; //index: fisrtOne == 0 means first bid. firstOne != 0 means later bids.

Bid p; // create the default bidding class

cout << " ***********Let the bidding begin!***********" << endl;

while (liar == 0) {

for (i = 0; i < num; i++) {

cout << endl;

cout << players[i].name << "'s turn: ";

if (firstOne != 0) { // input starting from second player

/* call out or not */

if (i - 1 < 0) {

cout << players[i].name << ", do you think " << players[num - 1].name << " is a liar? ";

}

else {

cout << players[i].name << ", do you think " << players[i - 1].name << " is a liar? ";

}

cin >> callOut;

while (callOut != 'y' && callOut != 'n') { // input validation

cout << "Please enter a valid response ('yes' or 'no'): ";

while (cin.get() != ' ') {} // clear input buffer

cin >> callOut;

}

while (cin.get() != ' ') {} // clear input buffer again

if (callOut == 'y') {

// Current player thinks the previous player was lying and calls out. End bidding and reveal the result

liar = 1;

break;

}

else if (callOut == 'n') {

/* no call out. Continue bidding */

validBid = 0;

while (validBid == 0) {

cout << players[i].name << " bids on the number (1-6): ";

while (!(cin >> x) || x <= 0 || x > 6) { // input validation

cout << "Wrong input! Enter an integer (1-6): ";

cin.clear(); // clear the failbit (error flag)

while (cin.get() != ' ') {} // clear input buffer

}

while (cin.get() != ' ') {}

cout << "How many " << x << "(s) are there: ";

while (!(cin >> y) || y < 0) { // input validation

cout << "Wrong input! Enter a non-negative number: ";

cin.clear(); // clear the failbit (error flag)

while (cin.get() != ' ') {} // clear input buffer

}

while (cin.get() != ' ') {}

// check whether this person is bidding higher

if (x <= p.getNumber() && y <= p.getAmount()) {

//current player violates the rule. Asking his/her to bid again

cout << "Please bid higher than the previous player. Bid again!" << endl;

}

else {

// current player gives correct bid. End his/her bidding turn.

validBid = 1;

p.setNumber(x);

p.setAmount(y);

}

}

}

}

else { // first bid

cout << players[i].name << " bids on the number (1-6): ";

while (!(cin >> x) || x <= 0 || x > 6) { // input validation

cout << "Wrong input! Enter an integer (1-6): ";

cin.clear(); // clear the failbit (error flag)

while (cin.get() != ' ') {} // clear input buffer

}

while (cin.get() != ' ') {}

cout << "How many " << x << "(s) are there: ";

while (!(cin >> y) || y < 0) { // input validation

cout << "Wrong input! Enter a non-negative integer: ";

cin.clear(); // clear the failbit (error flag)

while (cin.get() != ' ') {} // clear input buffer

}

while (cin.get() != ' ') {}

p.setNumber(x);

p.setAmount(y);

firstOne = 1;

}

}

}

/******* Reveal and check *******/

cout << endl;

if (tot_counter[p.getNumber() - 1] >= p.getAmount()) {

// If total amount of number x is >= the previous player's bid, then current player guessed wrong. The current player'll lose one point.

cout << players[i].name << " guessed wrong!" << endl;

score[i]--;

if (i - 1 < 0) {

score[num - 1]++;

}

else {

score[i - 1]++;

}

}

else {

// If total amount of number x is < the previous player's bid, then current player'll win one point.

cout << players[i].name << " guessed right!" << endl;

score[i]++;

if (i - 1 < 0) {

score[num - 1]--;

}

else {

score[i - 1]--;

}

}

/******* Continue the game or not *******/

cout << " One more round? ";

char ans;

cin >> ans;

while (ans != 'y' && ans != 'n') { // input validation

cout << "Please enter a valid response ('yes' or 'no'): ";

while (cin.get() != ' ') {} // clear input buffer

cin >> ans;

}

while (cin.get() != ' ') {} // clear input buffer again

if (ans == 'n') {

round = 1; // End the game.

}

} while (round == 0);

// round == 0 means players will have one more round.

// Restart the game without changing each player's name and order.

/******* Report final scores *******/

cout << " ***********Game Over!***********" << endl;

cout << "Final scores:" << endl;

for (i = 0; i < num; i++) {

cout << players[i].name << ": " << score[i] << " ";

}

return 0;

}

I did not include all the files needed to run the program just the file that I need help with. So why is my program not opening my file for viewing first?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!