How to use Bool variable [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
Read in a 3-character string from input into variable userCode. Declare a boolean variable isValid and set isValid to true if the second and third characters of userCode are both digits. Otherwise, set isValid to false.
What it is in Bold is what I have to fill in. The rest is apart of the question.
int main() {
string userCode;
bool isValid;
getline(cin, userCode);
if (isdigit(userCode[2])){
isValid = true;
userCode = isValid;
}
if (isdigit(userCode[3])){
isValid = true;
userCode = isValid;
}
else {
bool isValid = false;
}
if (isValid) {
cout << "Valid passcode" << endl;
}
else {
cout << "Invalid passcode" << endl;
}
return 0;
}

The second and third characters are at [1] and [2]. You should start with isvalid equal to true. Then test those two characters if either one is not a digit.
bool isvalid = true;
if(!isdigit(userCode[1]) || ! isdigit(userCode[2]))
isvalid = false;
if(isvalid)
cout << "good\n";
else
cout << "bad\n";
or with the logic the other way round
bool isvalid = false;
if(isdigit(userCode[1]) && isdigit(userCode[2]))
isvalid = true;
if(isvalid)
cout << "good\n";
else
cout << "bad\n";

Related

Is it possible to put 2 option answer in string or char in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is it okay or valid to use this? string answer = "A || B"; than using this if (answer == A || answer == B)?Because in my program I want to use that condition inside a while if statements. I want to put that two option condition in my while statement but it gets error.
// variables
string answerA = "Coin";
string guess;
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
cout << "Question A!\n" << endl;
cout << "A. What has a head and a tail, but no body?\n";
// if the answer is not correct, ask again only 3 times
while (answerA != guess && !outOfGuesses) {
if (guessCount < guessLimit) {
cout << "Answer: ";
cin >> guess;
guessCount++;
} else {
outOfGuesses = true;
}
}
if (outOfGuesses)
{
cout << "Your answers are all wrong! Better luck next time :)";
}
else
{
cout << "Your answer is Correct! ";
}
One std::string can hold only one string. You can use std::unordered_set to hold a set of multiple strings.
#include <iostream>
#include <string>
#include <unordered_set>
int main(void) {
std::unordered_set<std::string> accept_list = {"A", "B"};
std::string answer = "A";
if (accept_list.find(answer) != accept_list.end()) {
std::cout << "accept\n";
} else {
std::cout << "reject\n";
}
return 0;
}
(std::unordered_set is available since C++11. If your compiler doesn't support that, try std::set instead. Also initializer lists like accept_list = {"A", "B"} is since C++11, you may have to add each candidates separately in that case)

Why do i have to reference in this situation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int main(){
while(true){
char input = getchar();
int x, y;
POINT xypos;
if (input == 'S' || input == 's'){
std::cout <<"enter new position" << std::endl;
std::cin >> x >> y;
SetCursorPos(x, y);
} else if (input == 'g' || input == 'G'){
GetCursorPos(&xypos);
std::cout << "X: " << xypos.x << "Y " << xypos.y << std::endl;
}
}
return 0;
}
Can someone please explain why with GetCursorPos, it has to reference the xypos object in the parameters? Why is it not possible to directly utilize it? Thanks
You probably mean why GetCursorPos(from the WinAPI) doesn't just return the position instead of taking a pointer and filling that right?
That's how the WinAPI works, almost all functions return BOOL to indicate success or failure and take information they populate by pointer.

Why won't my "main()" function call a void function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm working on a program to aid me in world-building that randomly generates a settlement (hamlet, village, town, city) based on a nation (German, Latin, Eastern) that the user chooses. Unfortunately, my code halts right at the "main()" function as it won't call the "settlementCreation()" void function I created.
I've tried moving the function I want to call above the "main()" function, or my usual method of creating the function above, defining it's contents below, but neither of these are working. I can't figure out any other solutions with my limited experience coding C++.
Main() Function:
int main() {
char tempChoice{};
bool isMakingSettlement = true;
while (isMakingSettlement = true) {
cout << "Create a settlement? (y/n): ";
cin >> tempChoice;
cout << "\n\n";
if (tempChoice == 'y') {
settlementCreation();
} else {
isMakingSettlement = false;
}
}
return 0;
}
settlementCreation() Function:
void settlementCreation() {
int tempType{};
int tempNation{};
bool isTypeValid = false;
bool isNationValid = false;
while (isTypeValid = false) {
cout << "What type of settlement would you like to create?:";
cout << "\n 1. Hamlet";
cout << "\n 2. Village";
cout << "\n 3. Town";
cout << "\n 4. City\n";
cin >> tempType;
if (tempType >= 1 && tempType <= 4) {
isTypeValid = true;
} else {
cout << " is an invalid choice, please select a valid choice.";
}
cout << "\n\n";
}
while (isNationValid = false) {
cout << "What nation would you like your settlement to be in?: ";
cout << "\n 1. Latin";
cout << "\n 2. German";
cout << "\n 3. Eastern\n";
cin >> tempNation;
if (tempNation >= 1 && tempNation <= 3) {
isNationValid = true;
} else {
cout << " is an invalid choice, please select a valid choice.";
}
cout << "\n\n";
}
Settlement objSettlement(tempType,tempNation);
}
So the program is supposed to allow the user to choose a nation and a settlement type before redirecting to the Settlement object constructor to create the objSettlement instance of the object.
The usual outcome however, is just an infinite loop of:
"Create a settlement? (y/n): "
With no responses I've tried closing the program or going to the "settlementCreation()" function.
while (isMakingSettlement = true) {
This does not check if isMakingSettlement is true. It sets isMakingSettlement to true! This means the check in the while loop always sees true, so never stops going round.
Use while (isMakingSettlement == true).
(Or while (isMakingSettlement), or while (true == isMakingSettlement); all are fine, it's a stylistic choice, though the last would have helped you catch this bug!).
Similarly for all your other while loops.
Assuming you fix the above, your next problem will be here:
bool isTypeValid = false;
bool isNationValid = false;
while (isTypeValid == false) { // once corrected
// ... never get here!
while (isNationValid == false) { // once corrected
// ... never get here!
You always set those bools to false, so these loops are never executed.

Issues with if string validation loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on a validation if loop which checks for pipes at the beginning and end and makes sure there are 32 valid characters (valid chars are : and |)
I'm wondering why my program is not reading the if statement correctly for a 32 character input. Here is what I have so far.
void checkitout(string validate)
{
string check;
check = validate;
if ((check.length() == 31) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") || !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}
I'm new to this but I think I'm on the right track. The couts are to test to see if the loop is working. It goes right to the else state. Here is a sample input
||:|:::|:|:||::::::||:|::|:::|||
As always, any thoughts on how to do this better are greatly appreciated.
Your string has a lenght of 32, thus the if-condition is false because of check.length() == 31.
Also the if-condition in your loop needs an "&&" instead of an "||", since you want it to be neither "|" nor ":" to be an unacceptable barcode.
Changes are marked in bold.
void checkitout(string validate)
{
string check;
check = validate;
string one = check.substr(4,1);
cout << (check.substr(4,1) == one) << endl;
if ((check.length() == **32**) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") **&&** !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}

C++: function wont stop looping [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The purpose of this function is to ask the user to input a quantity to add to an order. The function will ask them to reenter info if they input a value less than 0 and exit the function if they input 0. It accepts user input if the value is a positive integer, modifies the data member, and returns true. If the user inputs 0, the function returns false. Problem is, the program never exits no matter what the user inputs. 0, a value less than 0, and a valid positive integer all cause the function to loop and ask the user to input information again. Can someone point out what is wrong with the logic within this function to explain why this would be happening?
bool Order::add(std::istream& is) {
int quantity;
bool start = true;
bool val = false;
while (start = true) {
std::cout << "Enter quantity (0 to quit): ";
is >> quantity;
if (quantity == 0) {
std::cout << "**No delivery recorded!" << std::endl;
start = false;
}
else if (quantity < 0) {
std::cout << "quantity must be a positive integer" << std::endl;
}
else {
copies += quantity;
start = false;
val = true;
}
}
return val;
}
You are always assigning true to the "start" variable:
Change the while loop (==):
while (start == true) {