C++ How to make a variable true if another variable WAS true - c++

I was wonder how could I make a bool true if another bool was true for example with this program :
for(int i=1;i<5;i++){
if (i == 3){
x = false;
} else{
x = true;
}
if(x){
cout << "true";
}else{
cout << "false";
}
}
output :
true
true
false
true
true
how would I make it so that the output is this:
output:
true
true
false
false
false
sorry if this is hard to understand I'm not the best at explaining

int flag = 0;
for(int i=1;i<5;i++){
if (i == 3){
x = false;
flag = 1;
} else{
x = true;
}
if(x && flag == 0){
cout << "true";
}else{
cout << "false";
}
}
If you specifically wish to switch your output upon getting 3, you can flag up the case where there is a change in x. See the code above. This is in accordance to your code pattern.

Try this code
x = true;
for(int i=1;i<5;i++){
if (i == 3){
x = false;
}
if(x){
cout << "true";
}else{
cout << "false";
}
}

Related

Trying to find all prime numbers in a list

the output
The program is checking every number in the list and should output if is prime or not. Probably I just don't get the prime algorithm right.
Currently outputs that no number is prime.
void primList(struct node* stnode)
{
struct node* pr = stnode;
int i;
bool primNum = true;
if (stnode != NULL)
{
while (pr != NULL)
{
if ((pr->num) == 0 || (pr->num) == 1)
primNum = false;
else
{
for (i = 2; i <= ((pr->num) / 2); ++i) //The mistake
if (((pr->num) % i) == 0) //is here?
{
primNum = false;
break;
}
}
if (primNum)
cout << "\n" << pr->num << " is a prime number.";
else
cout << "\n" << pr->num << " is not a prime number.";
pr = pr->nextptr;
bool primNum = true;
}
}
}
In general such questions (seeking debugging help) are discouraged, see on-topic.
In this case the minimal change to fix this code would be:
void primList(struct node* stnode)
{
struct node* pr = stnode;
int i;
bool primNum = true;
if (stnode != NULL)
{
while (pr != NULL)
{
if ((pr->num) == 0 || (pr->num) == 1)
primNum = false;
else
{
for (i = 2; i <= ((pr->num) / 2); ++i) //The mistake
if (((pr->num) % i) == 0) //is here?
{
primNum = false;
break;
}
}
if (primNum)
cout << "\n" << pr->num << " is a prime number.";
else
cout << "\n" << pr->num << " is not a prime number.";
pr = pr->nextptr;
primNum = true; // <-- notice I erased bool here
}
}
}
The problem here is that the primNum variable is not really changed in line bool primNum = true, but a new variable with the same name is created (and then immediately destroyed).
There are many other ways on how your code could be improved. See for example The Definitive C++ Book Guide and List as it has a lot of great sources.

Error C4716: recordDownLog: must return a value

void *recordDownLog(void *args) {
while (!isFinished && !isWaitFinished) {
isFinished = true;
int i = 0;
Sleep(sleepTime);
while (i <= downNum) {
if (downEndSize[i]<maxSize[i]) {
isFinished = false;
}
i++;
}
if (isFinished || isEnded) {
if (isFinished) {
writeDownFile(1);
}
else {
writeDownFile(2);
}
}
else {
writeDownFile(0);
}
}
cout << "end" << endl;
isWaitFinished = true;
}
what should i return the value.
If you don't need to return any value, then change the return type to void. But if there is a missing return statement, then you'd know better than me where that should go. Maybe you want to return args, because that isn't used anywhere else and is the same type as the current return type.

C++ Tic Tac Toe Game cannot change private members

I am creating an OOP tic tac toe game. I cannot understand why certain things are not working properly. First of all, I cannot change my gameBoard array by passing in a number for column and another one for row and changing it in that function. Second, the checkWinner function should change the player if there is no one, but it does not seem to do that either. Here is my code:
TicTacToe.h:
#ifndef TICTACTOE_H
#define TICTACTOE_H
using namespace std;
class TicTacToe{
private:
int gameBoard[3][3];
int playerTurn = 1;
public:
TicTacToe();
void printBoard();
int printPlayer();
bool changeBoard(int, int);
bool checkWinner();
};
#endif
TicTacToe.cpp:
#include "TicTacToe.h"
#include <iostream>
using namespace std;
TicTacToe::TicTacToe(){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
gameBoard[i][j] = 0;
}
}
}
void TicTacToe::printBoard(){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
cout << gameBoard[i][j];
}
cout << endl;
}
}
int TicTacToe::printPlayer(){
return playerTurn;
}
bool TicTacToe::changeBoard(int r, int s){
if (gameBoard[r][s] == 0){
gameBoard[r][s] == playerTurn;
return true;
}
else{
return false;
}
}
bool TicTacToe::checkWinner(){
if (gameBoard[0][0] && gameBoard[1][0] && gameBoard[2][0] == playerTurn){
return true;
}
else if (gameBoard[0][1] && gameBoard[1][1] && gameBoard[2][1] == playerTurn){
return true;
}
else if (gameBoard[0][2] && gameBoard[1][2] && gameBoard[2][2] == playerTurn){
return true;
}
/*********************************/
if (gameBoard[0][0] && gameBoard[0][1] && gameBoard[0][2] == playerTurn){
return true;
}
else if (gameBoard[1][0] && gameBoard[1][1] && gameBoard[1][2] == playerTurn){
return true;
}
else if (gameBoard[2][0] && gameBoard[2][1] && gameBoard[2][2] == playerTurn){
return true;
}
/*********************************/
else if (gameBoard[0][0] && gameBoard[1][1] && gameBoard[2][2] == playerTurn){
return true;
}
else if (gameBoard[0][2] && gameBoard[1][1] && gameBoard[2][0] == playerTurn){
return true;
}
/*********************************/
else{
if (playerTurn == 1){
playerTurn == 2;
}
else{
playerTurn == 1;
}
return false;
}
}
TicTacToeApp.cpp:
#include "TicTacToe.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
const int boardNumber = 3;
int selection;
int row;
int col;
TicTacToe gameBoard;
do{
cout << "Welcome to TicTacToe!" << endl
<< "1) Play Game" << endl
<< "2) Quit" << endl;
cin >> selection;
if (selection == 1){
do{
gameBoard.printBoard();
cout << "Player #" << gameBoard.printPlayer() << ":" << endl
<< "Row: ";
cin >> row;
row = row - 1;
cout << "\n";
cout << "Column: ";
cin >> col;
col = col - 1;
if (gameBoard.changeBoard(row, col) == false){
cout << "Invalid Selection, try again!" << endl;
}
gameBoard.checkWinner();
} while (gameBoard.checkWinner() == false);
if (gameBoard.checkWinner() == true){
cout << "Player " << gameBoard.printPlayer() << " wins!" << endl;
}
}
} while (selection != 2);
system("PAUSE");
return 0;
}
EDIT: I have changed my stupid errors now, but I still would not change the player to 2 when there is no winner. Here is my current code for that function:
bool TicTacToe::checkWinner(){
if (gameBoard[0][0] && gameBoard[1][0] && gameBoard[2][0] == playerTurn){
return true;
}
else if (gameBoard[0][1] && gameBoard[1][1] && gameBoard[2][1] == playerTurn){
return true;
}
else if (gameBoard[0][2] && gameBoard[1][2] && gameBoard[2][2] == playerTurn){
return true;
}
/*********************************/
else if (gameBoard[0][0] && gameBoard[0][1] && gameBoard[0][2] == playerTurn){
return true;
}
else if (gameBoard[1][0] && gameBoard[1][1] && gameBoard[1][2] == playerTurn){
return true;
}
else if (gameBoard[2][0] && gameBoard[2][1] && gameBoard[2][2] == playerTurn){
return true;
}
/*********************************/
else if (gameBoard[0][0] && gameBoard[1][1] && gameBoard[2][2] == playerTurn){
return true;
}
else if (gameBoard[0][2] && gameBoard[1][1] && gameBoard[2][0] == playerTurn){
return true;
}
/*********************************/
else{
if (playerTurn == 1){
playerTurn = 2;
}
else{
playerTurn = 1;
}
return false;
}
}
You cannot assign using == operator (unless you use operator overload).
Use = operator instead to assign (update values).

STUCK! Validation with integers, character to integer conversion, ASCII and MIDI

Ok so my C++ knowledge is so little, i've been slowly piecing together a code but in all honesty i'm surprised i've got this far.
Just to outline my task. The user is asked to enter in several notes (musical notes, C-B including Sharps, across 9 octaves) to create a melody line and then again but a bass line. After a note has been entered, a note length must also be
#‎include‬ <iostream>
#include <string>
#include <vector>
using namespace std;
int notenumber;
struct noteStorage
{
string noteName;
int midiNumber;
int noteLength;
};
///////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE NAME
bool ValidateNote(string note)
{
// Step 1: If note name length is less than 2 OR more than 3, return false
if (note.length() <2 || note.length() >3)
{
cout<<"Note length must be 2 or 3 characters\n";
return false;
}
//Step 2: If true, the note must be/(or be) between A and G
else if(tolower(note[0])<'a' || tolower(note[0]) >'g')
{
cout<<"Note must be A-G\n";
return false;
}
//Step 3: If true, the last character must be a digit
else if(isdigit(note[note.length()-1]) == false)
{
cout<<"Last character must be a digit\n";
return false;
}
//Step 4: If note length is 3 note[1] (character 2) must be '#'.
else if(note.length() == 3 && note[1] != '#')
{
"Invalid sharp note\n";
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE LENGTH
bool ValidateNoteLength (int length)
//Step 1 - If notelength is not a digit, return FALSE
{
if (length == false)
{
cout<<"Note length must be a digit/number, please re-enter";
return false;
}
//Step 2 - If notelength is less than or equal to 0 or more than 16, return FALSE
if (length <= 0 || length > 16)
{
cout<<"Note length value cannot be less than 1 or more than 16, please re-enter";
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateNoteNumber(string tempName)
{
int Octave;
int Note;
tempName[0] = toupper(tempName[0]);
Octave = ((tempName[tempName.length()-1]) -48) * 12;
if (tempName.length() == 2)
{
if(tempName[0] == 'C')
{
return notenumber = 0;
}
else if(tempName[0] == 'D')
{
return notenumber = 2;
}
else if(tempName[0] == 'E')
{
return notenumber = 4;
}
else if(tempName[0] == 'F')
{
return notenumber = 5;
}
else if(tempName[0] == 'G')
{
return notenumber = 7;
}
else if(tempName[0] == 'A')
{
return notenumber = 9;
}
else
{
return notenumber = 11;
}
}
else if (tempName.length() == 3)
{
if(tempName[0] == 'C')
{
return notenumber = 1;
}
else if(tempName[0] == 'D')
{
return notenumber = 3;
}
else if(tempName[0] == 'F')
{
return notenumber = 6;
}
else if(tempName[0] == 'G')
{
return notenumber = 8;
}
else
{
return notenumber = 10;
}
}
int main();
{
noteStorage noteData[8];
//string note;
for (int i = 0; i < 8; i++)
{
cout<<"Please enter note: " << i << ": ";
while (1)
{
string tempName;
cin>>tempName;
int noteNumber = CalculateNoteNumber(tempName);
if (ValidateNote(tempName) == true)
{
noteData[i].noteName = tempName;
break;
}
else
{
cout << "Please enter correctly: ";
}
} //end first while
cout<<"Please enter note length: ";
while (1)
{
int tempLength;
cin>>tempLength;
if (ValidateNoteLength(tempLength) == true)
{
noteData[i].noteLength = tempLength;
break;
}
else
{
cout << "Please enter correctly: ";
}
}//end while 2
cout<<"Thank you\n";
} //end for
cout<<"Your note and note lengths are: "<<endl;
for (int i = 0; i < 8; i++)
{
cout<<noteData[i].noteName<<"Length: ";
cout<<noteData[i].noteLength<<endl;
}
system("pause");
return 0;
}
entered (with a value in milliseconds). Once the note names and note lengths have been entered, the console then converts the notenames to the corresponding midi numbers, and outputs said midi numbers, note length and note names back to the user.
I've been having the same problem for two days now; everytime I build the solution it comes back with the same error:
"Fatal error C1075, end of file found before last brace '{' was
matched".
If anyone could point me the right way to solving this it would be much appreciated!!
You missed a } at the end of int CalculateNoteNumber(string tempName).
You will also have to remove the ; after int main() for your program to compile.
If you would have formatted your code properly you could have fixed these errors on your own.

I cannot figure out why this while loop is endless [duplicate]

This question already has answers here:
Can anyone tell me why these functions are not giving me a result in a reasonable spectrum?
(2 answers)
Closed 9 years ago.
Here is the code I am currently working with:
bool playCraps(int currentGame, bool detailPrint, char isBetting, int startingBet)
{
bool crapsResult = NULL;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
crapsResult = true;
}
else if(point == 2 || point == 3 || point == 12)
{
crapsResult = false;
}
else
{
crapsResult = NULL;
}
while(crapsResult != true || crapsResult != false)
{
currentGameStorage[currentRoll] = roll2Dice();
if(currentGameStorage[currentRoll] == point)
{
crapsResult = true;
}
else if(currentGameStorage[currentRoll] == 7)
{
crapsResult = false;
}
currentRoll += 1;
}
currentRoll -= 1;
if(detailPrint == true)
{
cout << "Game " << currentGame << ": ";
for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
{
cout << currentGameStorage[printingNumber] << " ";
}
if(crapsResult == true)
{
cout << "win";
}
else if(crapsResult == false)
{
cout << "lose";
}
cout << endl;
}
return crapsResult;
}
Whenever I run it it creates am emdless loop with no text appearing in the terminal. The function roll2Dice() simulates the roll of two six sided dice using the rand() function and adds the two results together. Any help would be appreciated.
Your while test, crapsResult != true || crapsResult != false is necessarily true, and therefore does not terminate. The crapsResult value is either true of false, which will make one of the two halves of the expression true, and one false. Both true || false and false || true evaluate to true.
As others have noted it's pretty obvious why the loop never ends.
Your use of NULL makes me think you wish you could have a bool which could hold three values (unset, true and false). You could achieve this with minimal changes by using a pointer to bool, but that's pretty disgusting. Enums are what you really need:
enum CrapsResult
{
unrolled,
true_result,
false_result
};
The pertinent code then becomes*:
CrapsResult crapsResult = unrolled;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
crapsResult = true_result;
}
else if(point == 2 || point == 3 || point == 12)
{
crapsResult = false_result;
}
else
{
crapsResult = unrolled;
}
while(crapsResult == unrolled)
{
currentGameStorage[currentRoll] = roll2Dice();
if(currentGameStorage[currentRoll] == point)
{
crapsResult = true_result;
}
else if(currentGameStorage[currentRoll] == 7)
{
crapsResult = false_result;
}
currentRoll += 1;
}
*I haven't actually compiled this.
while(crapsResult != true || crapsResult != false)
equals to
while(crapsResult == false || crapsResult == true)
.
//while(crapsResult == (false || true)) // Don´t matter
so as boolean has only false or true
it will always evaluate true and equals to this expression
while(true)
In C and C++, expressions can resolve to true or false based on the following: 0 is false, everything else is true. The formal definition of C's macros FALSE and TRUE is #define FALSE 0 and #define TRUE (!(FALSE)).
A variable of type bool can either be true or false. You initialize your crapsResult with the value of NULL. NULL is a macro defined as 0UL or 0ULL, which evaluates to false.
bool crapsResult = NULL;
is equivalent to
bool crapsResult = false;
So when your code does the following:
while (crapsResult != true || crapsResult != false)
one of these conditions is always met, so the loop always repeats.
You will need a second variable, presumably of type bool, to determine if you have an acceptable answer.
bool crapsResult = false;
bool haveCrapsResult = false;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
crapsResult = true;
haveCrapsResult = true;
}
else if(point == 2 || point == 3 || point == 12)
{
crapsResult = false;
haveCrapsResult = false;
}
while(!haveCrapsResult)
{
currentGameStorage[currentRoll] = roll2Dice();
if(currentGameStorage[currentRoll] == point)
{
crapsResult = true;
haveCrapsResult = true;
}
else if(currentGameStorage[currentRoll] == 7)
{
crapsResult = false;
haveCrapsResult = true;
}
currentRoll += 1;
}
currentRoll -= 1;
if(detailPrint == true)
{
cout << "Game " << currentGame << ": ";
for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
{
cout << currentGameStorage[printingNumber] << " ";
}
if(crapsResult == true)
{
cout << "win";
}
else /*if(crapsResult == false) is redundant */
{
cout << "lose";
}
cout << endl;
}
return crapsResult;