Function is not working - why? - c++

I am writing a tic-tac-toe game and I am finding it hard to make function that checks when a space is filled by either the noughts or crosses
Here is the full code (sorry a bit long):
#include <iostream>
using namespace std;
const int kingsize = 3;
char board[kingsize][kingsize];
bool p1 = false;
int p2 = -1;
char empty = 0;
/*this functions dispays the board*/
void bord()
{
cout <<
" ";
for (int iX = 0; iX < kingsize; iX++)
cout << " " << iX << " ";
cout << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
for (int iY = 0; iY < kingsize; iY++)
{
cout << " " << iY << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "|" << board[iY][iX] << " ";//this is the space to be filled by Os or Xs
cout << "|" << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
}
}
/*when this function is called it will show a blank board*/
void resetBoard()
{
for (int iY = 0; iY<kingsize; iY++)
{
for (int iX = 0; iX<kingsize; iX++)
{
board[iY][iX] = ' ';
}
}
}
/*this funtion is to plot the symbols in the grid based on the column and row*/
bool setboard(int iX, int iY, char cval)
{
if (iX >= kingsize || iY >= kingsize)
return false;
board[iY][iX] = cval;
return true;
}
/*this checks if there space is filled by one of the symbols*/
bool checkbord(int x, int z, char val)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
bool win(int tir)
{
int win = 3;
return (board[0][0] + board[0][1] + board[0][2] == win) // row 0
|| (board[1][0] + board[1][1] + board[1][2] == win) // chicking row 1
|| (board[2][0] + board[2][0] + board[2][2] == win) // checking row 2
|| (board[0][0] + board[1][1] + board[2][0] == win) // checking column 0
|| (board[0][1] + board[1][1] + board[2][1] == win) // column 1
|| (board[0][2] + board[1][2] + board[2][2] == win) // column 2
|| (board[0][0] + board[1][1] + board[2][2] == win) // diagonal
|| (board[2][0] + board[1][1] + board[0][2] == win) ; // checking diagonal
}
int main()
{
const int r = 3, c = 3;
char sym1 = 'X';
char sym2 = 'O';
char sym = 'O' || 'X';
cout << "TIC TAC TOE" << endl;
cout << endl;
cout << "How To Play" << endl;
cout << "to plot noughts and crosses you write the row number first " << endl;
cout << "then the number of the column" << endl;
cout << "Example :" << endl;
cout << endl;
resetBoard();
bord();
cout << endl;
cout << "plotting row 2 column 0" << endl;
cout << endl;
setboard(0, 2, 'X');
bord();
resetBoard();
cout << endl;
cout << "OK LET'S PLAY!!!" << endl;
bord();
int y, z;
int t=0;
do
{
loop:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)//if the player enters numbers higher than too it will show this error message
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop;
}
if (checkbord(y, z, sym) == 1)//if the player plots their symbol in a space that is already filled it will show this error message
{
cout << "spot alreay filled, please try again" << endl;
cout << endl;
t--;
goto loop;
}
setboard(y, z, sym1);//will plot the symbol based on what the player enetered
bord();
loop2:
cout << "second player plot O:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
goto loop2;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop2;
}
setboard(y, z, sym2);
bord();
} while (t <4);
loop3:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop3;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop3;
}
setboard(y, z, sym1);
bord();
return 0;
}
The problem is that checkbord function works sometimes or not at all. if the player plots the noughts/cross in a place that is already taken then it will replace it and its not supposed to do that
edit: I am not getting an error but feel the problem is with the checkbord function and I am out of ideas

Look at these functions:
bool setboard(int iX, int iY, char cval)
{
...
board[iY][iX] = cval;
...
}
bool checkbord(int x, int z, ...)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
So setboard(1,2) sets the value of board[2][1], but checkbord(1,2,...) checks the value of board[1][2].
There are other problems in this code, but you can start with this one.

Related

How can i save every move that was made "a, w, s, or d" to an array?

I want to save every move 'char' to an array, and then call back all arrays to see the history
cout << "=== Chose ===" << endl;
cout << "Choose were to GO" << endl;
cout << "a, w, s, or d" << endl;
cout << endl;
cin >> Сhoice_1;
if (tolower(Сhoice_1) == 'a')
{
cout << "You made a step to the left" << endl;
value1 = -1;
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 's')
{
cout << "You made a step back" << endl;
value1 = -1;
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'w')
{
cout << "You made a step foward" << endl;
value1 = -1;
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'd')
{
cout << "You made a step to the right" << endl;
value1 = -1;
if (value1 < 0) break;
}
if (LifeOptionMain <= 0)
{
value1 = -1;
if (value1 < 0) break;
}
I tried for loop
for (int i = 0; i < 10; i++)
{
move[i + 1];
move[i] = Сhoice_1;
}
but all elements are only one symbol, last input symbol, if last was a then output will be
a
a
a
....
what am I doing wrong??? please help.
You could run into trouble with the length of your array, but this would work.
char moveArray[1024];
int moveArrayIndex = 0;
...
cin >> Сhoice_1;
moveArray[moveArrayIndex++] = Choice_1;
moveArray[moveArrayIndex] = 0; // This makes it a printable string.
...
The problem with this is that you're flow off the end of your array if there are more moves than you made space for. A different data structure would be safer.
std::vector<char> moveArray;
cin >> Choice_1;
moveArray.push_back(Choice_1);
However, this is probably using techniques you're not ready for yet.
I found a solution thanks a lot, if anyone needs a solution to a similar problem, I'll leave the code. I apologize in advance since this is just a study task.
function #1
do
{
cout << "=== Make a move ===" << endl;
cout << "=== Chose ===" << endl;
cout << "Choose were to GO" << endl;
cout << "a, w, s, or d" << endl;
cout << endl;
cin >> Сhoice_1;
if (tolower(Сhoice_1) == 'a')
{
cout << "You made a step to the left" << endl;
value1 = -1;
move.push_back(Сhoice_1);
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 's')
{
cout << "You made a step back" << endl;
value1 = -1;
move.push_back(Сhoice_1);
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'w')
{
cout << "You made a step foward" << endl;
value1 = -1;
move.push_back(Сhoice_1);
if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'd')
{
cout << "You made a step to the right" << endl;
value1 = -1;
move.push_back(Сhoice_1);
if (value1 < 0) break;
}
if (LifeOptionMain <= 0)
{
value1 = -1;
if (value1 < 0) break;
}
return Сhoice_1;
function #2
void Move_History()
{
for (char i : move)
{
cout << "You made a move - : " << i << ":";
cout << endl;
}
}

How to score yahtzee in c++

I am writing a yahtzee game for my c++ programming class. One of my difficulties I have ran into is the scoring system for different categories. I think I have figured out how to do it for adding 1s, 2s etc but I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled. Here is my code so far.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
int players;
int turn = 1;
vector<string> names;
string playerName;
int dice[5];
int finalScore = 0;
char reroll[5];
char rollDice;
int tries = 1;
const int DICE = 5;
int roll[DICE];
int scorecard;
int scoreThisTurn(int scorecard);
int turnScore = 0;
//Introduction, get number of players.
cout << "Hello, welcome to Yahtzee! How many players are there?" << endl;
cin >> players;
if (players > 4) {
cout << "Sorry, the maximum number of players is 4." << endl;
cout << "How many players are there?" << endl;
cin >> players;
}
//Get player names
string getNames();
for (int i = 0; i < players; i++) {
cout << "Hello player " << i + 1 << ", please enter your name" << endl;
cin >> playerName;
names.push_back(playerName);
}
srand(time(NULL)); //random seed
cout << "Welcome to Yahtzee!\n";
while (turn <= 13) { //roll dice
cout << "Press 'r' to roll" << endl;
cin >> rollDice;
if (rollDice == 'r') {
for (int i = 0; i < DICE; i++) {
roll[i] = rand() % 6 + 1;
}
}
cout << "You rolled: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your second roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your third roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
//displays scorecard categories
cout << "Which category would you like to score this in" << endl;
cout << "1 - ones: " << endl;
cout << "2 - twos: " << endl;
cout << "3 - threes: " << endl;
cout << "4 - fours: " << endl;
cout << "5 - fives: " << endl;
cout << "6 - sixes: " << endl;
cout << "7 - 3 of a kind: " << endl;
cout << "8 - 4 of a kind: " << endl;
cout << "9 - small straight: " << endl;
cout << "10 - large straight: " << endl;
cout << "11 - full house: " << endl;
cout << "12 - yahtzee: " << endl;
cout << "13 - chance: " << endl;
//asks player to choose where to score
cout << "\nEnter 1-14 to choose a category." << endl;
cin >> scorecard;
//assigns points
for (int i = 0; i < DICE; i++) {
turnScore = 0;
if (scorecard == 1) {
if (roll[i] == 1) {
turnScore = turnScore + 1;
}
}
if (scorecard == 2) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
if (scorecard == 3) {
if (roll[i] == 3) {
turnScore = turnScore + 3;
}
}
if (scorecard == 4) {
if (roll[i] == 4) {
turnScore = turnScore + 4;
}
}
if (scorecard == 5) {
if (roll[i] == 5) {
turnScore = turnScore + 5;
}
}
if (scorecard == 6) {
if (roll[i] == 6) {
turnScore = turnScore + 6;
}
if (scorecard == 7) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
}
cout << scorecard << endl;
turn++;
}
system("pause");
return 0;
}
As you can see I've set up the scoring for the first 6 categories but don't know how to proceed.
I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled.
Create a variable to help you keep track of the number of dice that have a given number.
int diceCount[DICE] = {0};
and fill up the array with:
for (int i = 0; i < DICE; i++) {
diceCount[roll[i-1]]++
}
Create helper functions to determine whether five, four, or three of a kind have been thrown.
int getNOfAKind(int diceCount[], int N)
{
// This will need moving DICE out of `main`
// making it a global variable.
for ( int i = 0; i < DICE; ++i )
{
if (diceCount[i] == N )
{
return i+1;
}
}
return -1;
}
int getFiveOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 5);
}
int getFourOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 4);
}
int getThreeOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 3);
}
and use it as:
int fiveCount = getFiveOfAKind(diceCount);
if ( fiveCount != -1 )
{
}
int fourCount = getFourOfAKind(diceCount);
if ( fourCount != -1 )
{
}
int threeCount = getThreeOfAKind(diceCount);
if ( threeCount != -1 )
{
}

How to get rid of goto and flip in the following program

How to get rid of goto statement in this program and end the flip:>>>?
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
do
{
flip1:
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
flip2:
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
else if (option == 2)
{
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
else if (option == 3)
{
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
} while (option != 4);
return 0;
}
You can refer the below code.
Before entering the loop.
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do {
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
/* All text as same */
At the end: change
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
To.
if (q == 'e')
{
return 0;
}
system("cls");
if (q == 'm')
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
}
PS: You can also create a function that prints the menu, get input from user and return the same.
If you divide your code into chunks of cohesive functionality, you can put them into their own functions and the main function can be much simpler. Here's my recommendation:
#include <iostream>
#include <cstdlib>
using namespace std;
int getOption1()
{
int option;
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
return option;
}
char getOption2()
{
char option;
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
cin >> option;
return option;
}
void doArrayBasics()
{
int array[10];
int sum = 0;
float avg = 10;
int mul = 1;
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
void doArraySearching()
{
int array[10];
int o;
int index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
void doArraySorting()
{
system("cls");
int a[10];
int r;
int t;
int temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
int main()
{
int option1;
char option2 = 'r';
do
{
if ( option2 == 'r' )
{
option1 = getOption1();
}
switch (option1)
{
case 1:
doArrayBasics();
break;
case 2:
doArraySearching();
break;
case 3:
doArraySorting();
break;
case 4:
break;
default:
cout << "Invalid option " << option1 << endl;
}
if ( option1 != 4 )
{
option2 = getOption2();
}
} while (option1 != 4 && option2 != 'e' );
return 0;
}
You can use an inner loop to remove all gotos.
Remove flip1:, change flip2: to do {. The loop should begin like that:
do
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do
{
if (option == 1)
{
At the end of the loop, change:
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
to :
if (q == 'e')
{
return 0;
}
system("cls");
} while (q == 'r');
Breaking your code into functions will also make it more readable but is not mandatory to remove gotos.
caveat: the following code not tested
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
int done = 0;
int looping = 1;
while( !done )
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
while( looping )
{
looping = 0;
switch( option )
{
case 1:
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
break;
case 2:
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
break;
case 3:
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
{
for (t = 0; t<9; t++)
{
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
}
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
break;
case 4:
done = 1;
break;
default:
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
switch( q )
{
case 'm':
system("cls");
// looping already set = 0
break;
case 'r':
system("cls");
looping = 1;
break;
case 'e':
done = 1;
break;
default:
break;
} // end switch
break;
} // end switch
} // end while looping
} // end while not done
return 0;
} // end function: main

When reaching choice 0, how do I stop looping back to menu

This is a menu driven program for calculations.
#include <iostream>
#include <cmath>
using namespace std;
void Choices();
int x;
int n;
float fact;
float cosh(float x, int n);
float sinh(float x, int n);
float factorial( int n );
int main ()
{ char exit;
char choice;
bool option1hasrun = false;
while (exit != 'y' || exit != 'Y')
{
Choices();
cin >> choice;
if (choice == '1' && option1hasrun == false) {
cout << "Please give a value for x: ";
cin >> x;
cout << "Please give a value for the approximation order n: ";
cin >> n;
cout << endl;
option1hasrun = true;
}
else if (choice == '2' && option1hasrun == true)
{
cout << "The hyperbolic sinh of x is: " << sinh(x) << endl;
cout << "Using Taylor series is it: " << sinh(x, n) << endl;
}
else if (choice == '3' && option1hasrun == true)
{
cout << "The hyperbolic cosh of x is: " << cosh(x) << endl;
cout << "Using Taylor series is it: " << cosh(x, n) << endl;
}
else if (choice == '4' && option1hasrun == true)
{
cout << "old value of x = " << x << endl;
cout << "old approximation = " << n << endl;
cout<< "Please give new value of x: " ;
cin >> x;
cout << endl;
cout << "Please give new n: " ;
cout << endl;
cin >> n;
}
if ((choice=='2' || choice=='3' || choice=='4') && option1hasrun==false)
{
cout << "You have to enter a value first!\n\n";
}
if (choice<'0' || choice > '4')
{
cout << "Wrong choice. Only options 1-4 are available.\n\n";
}
else if (choice == '0' && option1hasrun==true)
{
cout << "Are you sure you want to quit? (Y/N) ";
cin >> exit;
if (exit =='y' || exit == 'Y') {
cout << "bye bye!!";
}
}
/* After entering Y I would like the program to stop, but currently it continues to loop*/
}
return 0;
}
float factorial( int n )
{
float fact = 1;
for ( int i = 1; i <= n; i++ )
{
fact = fact * i;
}
return fact;
}
float sinh(float x, int n)
{
float sum = 0;
for ( int i = 0; i <= n; i++ )
{
sum = sum + pow(x, 2*i +1)/factorial(2*i +1);
}
return sum;
}
float cosh(float x, int n)
{
float sum = 0;
for ( int i = 0; i <= n; i++ )
{
sum = sum + pow(x, 2*i)/factorial(2*i);
}
return sum;
}
void Choices ()
{
cout << "MAIN MENU" << endl;
cout << "1. To enter the data. " << endl;
cout << "2. To calculate and approximate the sinh(x)" << endl;
cout << "3. To calculate and approximate the cosh(x) " << endl;
cout << "4. To modify data. " << endl;
cout << "0. to quit." << endl << endl;
cout << "Please make a choice :";
}
Can someone help me figure out why the program continue to loops after choice 0?
Entered characters cannot be neither 'y' nor 'Y' at the same time.
Try changing the exit condition to:
while (exit != 'y' && exit != 'Y')

Why won't the numbers stay random?

I have been coding a blackjack game that is almost done it's first stage of development. I am almost done, the only problem is that the random numbers that are generated in this part:
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
is not the same as the numbers generated in the final part:
cout << "You had a total of: " << x.userTotal << endl;
as i keep getting different numbers. I will paste my entire code below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void setRand() {
srand(time(0));
}
class Game {
public:
int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int userShowCardOne = userCards[1 + (rand()%11)];
int userShowCardTwo = userCards[1 + (rand()%11)];
int dealerShowCard = dealerCards[1 + (rand()%11)];
int dealerHiddenCard = dealerCards[1 + (rand()%11)];
int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree, userShowCardFour, userShowCardFive};
int userShowCardThree = 0;
int userShowCardFour = 0;
int userShowCardFive = 0;
int fresh = 1;
int beginningInput;
int hit = 1;
int dealerTotal = dealerShowCard + dealerHiddenCard;
int userTotal = userShowCardOne + userShowCardTwo;
int cardCount = 2;
int runGame = 1;
private:
};
// int a = 1 + rand()%11;
/*int b = 1 + rand()%11;
int c = 1 + rand()%11;
int d = 1 + rand()%11;
int e = 1 + rand()%11;
int f = 1 + rand()%11;
int g = 1 + rand()%11;
int h = 1 + rand()%11;
*/
int startGame();
int main() {
srand(time(0));
Game x;
cout << "Welcome to BlackJack 1.0.0" << endl;
cout << "Press: " <<endl;
cout << "1 ----- New Game" << endl;
cout << "2 ----- Help" << endl;
cin >> x.beginningInput;
if(x.beginningInput == 1){
startGame();
cout << "The dealer had: " << endl;
cout << x.dealerHiddenCard << endl;
cout << x.dealerShowCard << endl;
while(x.dealerTotal <= 16) {
cout << "The dealer has decided to hit" << endl;
int dealerHit = 1 + (rand()%11);
cout << "The dealer has gotten " << dealerHit << endl;
x.dealerTotal += dealerHit;
}
cout << "The dealer has decided to stay" << endl;
cout << "The dealer had a total of " << x.dealerTotal << endl;
cout << "You had a total of: " << x.userTotal << endl;
if (x.dealerTotal > 21 && x.userTotal <= 21) {
cout << "The dealer busted. You won!" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal <= 21) {
cout << "You busted. The Dealer Won" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal > 21) {
cout << "You and the dealer both busted. Tie" << endl;
}
else {
if (x.dealerTotal > x.userTotal) {
cout << "Sorry, you lost to the dealer" << endl;
}
else if (x.dealerTotal < x.userTotal) {
cout << "Congrats, you won!" << endl;
}
else {
cout << "You and the dealer tied. " << endl;
}
}
cout << "Would you like to play again? 1 to play again, 0 to quit" << endl;
cin >> x.beginningInput;
if (x.beginningInput == 1){
startGame();
}
else if (x.beginningInput == 0){
return 0;
}
}
else if (x.beginningInput == 0) {
cout << "Thanks for playing!" << endl;
}
else {
cout << "Here's the help section" << endl;
}
}
int startGame() {
Game x;
srand(time(0));
if (x.fresh != 1){
cout << "NEW GAME\n \n " << endl;
}
while (x.runGame == 1) {
cout << "Dealer: \n" << endl;
cout << "X" << endl;
cout << x.dealerShowCard << endl;
cout << "You: \n" << endl;
cout << x.userShowCardOne << endl;
cout << x.userShowCardTwo << endl;
x.userTotal = x.userShowCardOne + x.userShowCardTwo;
if (x.userShowCardThree != 0) {
cout << x.userShowCardThree << endl;
}
if (x.userShowCardFour != 0) {
cout << x.userShowCardFour << endl;
cout << "You can only hit one more time!" << endl;
}
if (x.userShowCardFive != 0) {
cout << x.userShowCardFive << endl;
}
if(x.cardCount > 5) {
cout << "sorry, there is a 5 card limit.";
}
cout << "Would you like to hit or stay? (1 for hit or 2 for stay)" << endl;
cin >> x.hit;
x.fresh = 2;
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
}
if (x.hit == 2) {
x.runGame = 2;
}
}
return 0;
}
Looks like you have 2 instances of the Game object and both are named x
int main() {
srand(time(0));
Game x;
...
cout << "You had a total of: " << x.userTotal << endl;
then in the startGame function
int startGame() {
Game x;
...
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
The x in main() is not the same as x in startGame() as both are separate Game objects.