I've been doing a drill from a book entitled Programming: Principles and Practice Using C++ by Bjarne Stroustrup, and this drill (Chapter 4) primarily deals with vectors. When I run the program, it doesn't give me any output at all. There are no compilation errors though. Here's the code:
#include "std_lib_facilities.h"
int main()
{
vector<double> value;
vector<string> units;
double max_val=-100000, min_val=100000;
double temp, sum=0;
int no_of_inputs=0;
string unit;
int i=0;
cout << "\nEnter the first value: " << endl;
// inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
// converts cm, in and ft to m;
for (i==0; i<units.size(); ++i){
if (units[i]== "cm" || units[i]== " cm"){
value[i] = value[i]/100.0;
}else if (units[i]== "in" || units[i]== " in"){
value[i] = value[i]/2.5/100.0;
}else if (units[i]== "ft" || units[i]== " ft"){
value[i] = value[i]*12/2.5/100.0;
}else if (units[i]=="m" || units[i]== " m"){
}else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
// Identifies the max_value and the min_value. Also adds all values.
for (i==0; i<value.size(); ++i){
if (value[i]>max_val){
max_val=value[i];
}
if (value[i]<min_val){
min_val=value[i];
}
sum += value[i];
}
// outputs all values entered (converted to meters)
cout << "\nValues Entered:"<< endl;
sort(value);
for (i==0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;
}
Can anyone tell me why it is not working?
you have 2 mistakes. The main reason for terminating your program is here
else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value."; return 0;
you putting return in the first for loop. I guess you may think return is belong to else but because you don't use scope determiner {} only 1 instruction (cout) is belong to else and return is executed every time.
your second mistake is use i==0 instead of i=0 in for loops.
your correct code is this :
vector<double> value;
vector<string> units;
double max_val = -100000, min_val = 100000;
double temp, sum = 0;
int no_of_inputs = 0;
string unit;
int i = 0;
cout << "\nEnter the first value: " << endl;
//inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
//converts cm, in and ft to m;
for (i = 0; i<units.size(); ++i){
if (units[i] == "cm" || units[i] == " cm"){
value[i] = value[i] / 100.0;
}
else if (units[i] == "in" || units[i] == " in"){
value[i] = value[i] / 2.5 / 100.0;
}
else if (units[i] == "ft" || units[i] == " ft"){
value[i] = value[i] * 12 / 2.5 / 100.0;
}
else if (units[i] == "m" || units[i] == " m")
{
}
else
{
cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
}
//Identifies the max_value and the min_value. Also adds all values.
for (i = 0; i<value.size(); ++i){
if (value[i]>max_val){
max_val = value[i];
}
if (value[i]<min_val){
min_val = value[i];
}
sum += value[i];
}
//outputs all values entered (converted to meters)
cout << "\nValues Entered:" << endl;
//sort(value);
for (i = 0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;
Related
So i have to code the dice game LCR for a final prject but am having some trouble. First off, I know the code is sloppy and really redundant, that's why im looking for help. I couldn't figure out how to connect the 'chips' int vector and 'name' array into the player.h file. I basically need help writing methods for the chip passing to make the code less redundant. But another problem of mine is having the game loop until just one person has chips. Thanks for any help or advice.
LeftCenterRight.cpp
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
#include <vector>
#include <algorithm>
using namespace std;
void Player::gameRules()
{
cout << ("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
cout << ("Left Center Right is a multiplayer dice game");
cout << ("With a minimum of three players.");
cout << ("Dice containing letters L, C, R along\n"
"with dots on the remaining side are rolled each turn");
cout << ("\n-----Each Player starts with three chips-----\n");
cout << (">> For each L rolled, the player must pass one chip to the player to their left\n"
">> For each R rolled, the player must pass one chip to the player to their right\n"
">> For each C rolled, the player must pass a chip into the center pot (out of play)\n"
">> Dots are neutral and require no action");
cout << ("If a player has three or more chips, he/she rolls all three dice\n"
"If a player only has two chips, he/she rolles onlt two dice\n"
"If a player only has one chip, he/she rolls only one die\n"
"If a player is out of chips, he/she is still in the game,\n"
"\tbut does not roll any dice and passes their turn"
"\n\n >>> The last player with chips is the winner <<<");
cout << ("\n\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n");
};
int main()
{
int result = 0;
int currPlayer = 1;
srand((unsigned)time(NULL));
int numPlayers;
cout << "How many players are playing? (Please enter 3 or more): " << endl;
cin >> numPlayers;
if (numPlayers <= 2)
{
while (numPlayers <= 2)
{
cout << "More players needed.";
cout << "How many players are player?: ";
cin >> numPlayers;
}
}
std::string* names = new string[numPlayers]; // getting names and index is seat number
for (int i = 0; i < numPlayers; i++)
{
cout << "Please enter your name player " << i+1 << endl;
cin >> names[i];
std::string playerName = names[i];
}
vector<int>chips[1]; // intial 3 chips
for (int i = 0; i < numPlayers; i++)
{
int InitialChips = 3;
chips->push_back(InitialChips);
}
Player::gameRules();
int sum = 0;
for (int i = 0; i < chips->size(); i++)
{
int num = chips->at(i);
sum+=num;
}
int thePot = 0;
int i = 0;
while (sum > 0)
{
if ( i >=4 )
{
i = 0;
}
string currPlayer = names[i];
int currSeat = i;
cout << "It's " << currPlayer << "'s Turn!" << endl;
cout << "[1] Roll Dice [2] Quit :";
int choice;
cin >> choice;
if (choice == 1)
{
if (chips->at(currSeat) == 0)
{
break;
}
if (chips->at(currSeat) >= 3)
{
for (int k = 0; k <= 3; k++)
{
int outcome = Dice::rollDice();
if (outcome == 1)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == 0)
{
int j = (numPlayers - 1);
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i - 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 2)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == chips->size())
{
int j = chips->at(0);
int currChips2 = chips->at(0);
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i + 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 3)
{
thePot++;
cout << ">> +1 chip to the Center Pot" << endl;
cout << "There are now " << thePot << " chip(s) in the Center Pot " << endl;
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
break;
}
else if ((outcome == 4) || (outcome == 5) || (outcome == 6))
{
break;
}
}
}
// ^^basically copied and pasted most of the while loop for the other two numbers of dice to roll^^
// had redundant code for if the player had 2 chips, to roll two dice only ^^
// also redundant code for only one chip, to roll one die. ^^
}
}
else if (choice == 2)
{
break;
}
else
{
cout << ">> Input Error";
cout << "[1] Roll Dice [2] Quit";
cin >> choice;
}
i++;
}
return 0;
}
Dice.h
#pragma once
using namespace std;
class Dice
{
public:
static int rollDice();
static int diceOutcome;
};
int Dice::rollDice()
{
int diceOutcome;
diceOutcome = (rand() % 6) + 1;
switch (diceOutcome)
{
default:
cout << "Error, retry";
case 1:
if (diceOutcome == 1)
{
cout << " --- " << endl;
cout << "You rolled a | L | Move 1 chip left." << endl;
cout << " --- " << endl;
return 1;
}
break;
case 2:
if (diceOutcome == 2)
{
cout << " --- " << endl;
cout << "You rolled a | R | Move 1 chip right." << endl;
cout << " --- " << endl;
return 2;
}
break;
case 3:
if (diceOutcome == 3)
{
cout << " --- " << endl;
cout << "You rolled a | C | Move 1 chip to the center." << endl;
cout << " --- " << endl;
return 3;
}
break;
case 4:
if (diceOutcome == 4)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 5:
if (diceOutcome == 5)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 6:
if (diceOutcome == 6)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
}
}
To be fair, I'm quite new to programming, but I have been working on this project for days and kept running into conatiner problems and storing the name and chip values independently. So i've tried a lot of different things, probably not correctly though so I'm open to anything.
I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```
I'm working on an assignment for school. The code is supposed to read form a file and create an array, then sort the values of the array to output certain info. It works just fine as long as I have 3+ lines of info in the file. If not, I get the following error:
First-chance exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
Unhandled exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
I can't figure out why, any help would be appreciated. Here's the code:
#include <iostream> //calls the information needed
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std; //sets all unmarked commands to std::
const int ARRSIZE = 1000;
struct Student
{
string firstName;
string lastName;
string id, temp;
double gpa;
};
int readArray(ifstream& ifile, Student arr[]);
void swapElements(Student arr[], int i, int j);
void sortArray(Student arr[], int numberInTheArray);
int main()
{ // Declares the needed variables
double sought, min, max;
int i, ival, returnvar, count = 0, mincount, maxcount;
string filename;
ifstream ifile;
Student arr[ARRSIZE];
cout << "Input File Name: ";//requesting the file name
cin >> filename;
ifile.open(filename.c_str());//opening the file
if (!ifile)//checking if it opened or not
{
cout << endl << "That file does not exist!" << endl;//informing the user it did
return 1;//not open and returning 1
}
cout << "Which number do you want to return? ";//requesting the desired number
cin >> ival;
i = ival - 1;
cout << endl;
returnvar = readArray(ifile, arr);
min = arr[0].gpa;
max = arr[0].gpa;
sought = arr[0].gpa;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
if (count == 0)
{
cout << "The file is empty!" << endl;
return 1;
}
cout << "Before Sort:" << endl;
cout << " Min GPA is " << min << " for " << arr[mincount].lastName << "." << endl;
cout << " Max GPA is " << max << " for " << arr[maxcount].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortArray(arr, returnvar);
count = 0;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
cout << "After Sort:" << endl;
cout << " Array[0] GPA is " << min << " for " << arr[0].lastName << "." << endl;
cout << " Array[" << (returnvar - 1) << "] GPA is " << max << " for " << arr[(returnvar - 1)].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
return 0;
}
int readArray(ifstream& ifile, Student arr[])
{
int counter = 0;
while ((ifile) && (counter <= ARRSIZE))
{
ifile >> arr[counter].firstName;
ifile >> arr[counter].lastName;
ifile >> arr[counter].id;
ifile >> arr[counter].gpa;
counter++;
}
return (counter - 1);
}
void sortArray(Student arr[], int numberInTheArray)
{
for (int i = 0 ; i < numberInTheArray - 1; i++)
{
for (int j = 0 ; j < numberInTheArray - 1; j++)
{
if ( arr[j].gpa > arr[j + 1].gpa)
{
swapElements(arr, j, j+1);
}
}
}
}
void swapElements(Student arr[], int i, int j)
{
Student temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Please ignore the insanity and comments. Like I said, for an entry level course.
Try replacing counter <= ARRSIZE with counter < ARRSIZE (a rule of thumb in C is: never use <= in operations related to container sizes).
EDIT: also in your main(), you must check that i < ARRSIZE (equivalently, return error if i >= ARRSIZE). At present you seem to accept the case i == ARRSIZE, which is also wrong. And finally, readArray should return counter (that is, one more than the last written index).
My assignment is to create a guessing game where the computer guesses my number and one where I guess the computer's number. I have my code written out but It's incomplete. I want to create a menu so the user can choose which game he wants to play, and also i am having trouble joining the two programs into one so they both will run. Can someone please assist me. The first game works fine but from there I really don't know what I'm doing.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
}
srand(time(0));
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
srand(time(0));
systemNum = rand()% 100 + 1;
//Beginning of second game
do
{
if (num == 3)
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
}
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}
while (userGuess != systemNum);
}
system("pause");
return 0;
}
Make the two games two distinct functions (each with its own local variables), create a third one that is a loop that prompts for the games (and break when "quit" is selected), and call the corresponding function, then call the third one from main.
You can use if...else :
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{//game1
}
else if(num==3)
{//game2
}
else if(num==4)
{return 0;
}else
{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
Your whole code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
srand(time(0));
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
}
systemNum = rand()% 100 + 1;
//Beginning of second game
if (num == 3)
{
do
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}}
while (userGuess != systemNum);
}else if(num==4)return 0;
else{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
}
okay here is my code for a game called pig. I having trouble because the code for the computers turn will only got through one time and once the second turn is reached the first line is only printed. is there any soluction to this or am I just screwed?
`int main () {
int humanTurn=0;
int humanTotalScore=0;
int computerTotalScore=0;
int computerTurn=3;
float diceRoll = 0;
int score = 0;
int computerScore = 0;
int answer = 0;
int humanScore = 0;
int pause = 0;
float computerDiceRoll = 0;
srand(time(0));
cout << "Welcome to the game of pig, the first player to reach to 100 wins."<< endl;
cout << "if you roll a one no score will be recorded and your turn will go the computer"<< endl;
cout << " If you choose to hold, the total score accumilated will be recorded" << endl;
while ((humanTotalScore < 100) && (computerTotalScore < 100)) {
cout << endl;
cout << endl;
cout << "press 1 to roll or 2 to save your score: ";
cin >> answer;
for (int i=0; i<1; i++) {
diceRoll = rand() % 6 + 1;
}
if ((diceRoll > 1) && ( answer == 1)){
cout << endl;
cout << endl;
cout << "your current roll is: " << diceRoll << endl;
cout << endl;
cout << endl;
score += diceRoll;
cout << "Score for this turn is: " << score << endl;
cout << "your total score is: " << humanTotalScore << endl;
cout << endl;
cout << endl;
}
else if (answer == 2){
cout << "your score of: " << score << " will be saved" << endl;
humanTotalScore += score;
score = 0;
}
else {
cout << endl;
cout << endl;
cout << "you rolled a ONE. You lose your turn and any points that were with it" <<endl;
cout << endl;
cout << endl;
score = 0;
computerTurn = score;
}
if ((computerTurn == 0) || (answer == 2)){
computerDiceRoll = rand() % 6 + 1;
here is code that is giving me loop problems.
while ((computerDiceRoll > 1)&& (computerScore <= 20)){
if (computerDiceRoll == 1){
computerScore = 0;
computerTurn = 1;
cout << "Computer rolled a ONE, it is now your turn." << endl;
}
computerDiceRoll = rand() % 6 + 1;
cout << endl;
cout << endl;
cout << "computers roll is : " << computerDiceRoll << endl;
cout << endl;
cout << endl;
computerScore += computerDiceRoll;
if (computerScore >20){
computerTotalScore += computerScore;
computerTurn = 1;
}
cout << "computer current score is : " << computerScore << endl;
cout << "computer total score is: " << computerTotalScore << endl;
cout << endl;
cout << endl;
}
}
}
} `