Issues with if string validation loop [closed] - c++

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";
}
}

Related

Getting same output everytime [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 2 years ago.
Improve this question
This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";

Why does my while loop never end in my number guess game in C++? [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 am very new to programming and i am just messing around to try getting this guess a number game working, why once you guess the correct number or run of of lives does it continue?
Thanks!
int rand_number{5};
int guess_number{};
int try_number{};
int player_lifes{5};
bool game_over{ false };
cout << "Enter a number between 1-10" << endl;
while (player_lifes > 0 || game_over == true)
{
cout << "You have " << player_lifes << ((player_lifes == 1)? " live left." : " lifes left.") << endl;
cout << "Your guess is: ";
cin >> guess_number;
cout << endl;
if (guess_number == rand_number)
{
int final_lifes = (5 - player_lifes) + 1;
cout << "well done you solved it in " << final_lifes << (( final_lifes == 1)? " try." : " tries.") << endl << endl;
game_over = true;
}
else
{
player_lifes--;
cout << "wrong try again" << endl;
}
}
cout << "Finished";
while (player_lifes > 0 || game_over == true)
This means you'll continue looping as long as player_lifes > 0 OR game_over == true.
So this loop can only end when player_lifes <= 0 AND game_over == false. That doesn't sound right...
You probably mean
while (player_lifes > 0 && game_over == false)
Or in English, "loop while the player has lives AND the game is not over."
In other words, this loop will stop when the opposite is true--when "the player has 0 or less lives OR the game is over."
while (player_lifes > 0 || game_over == true)
This means that the while will run if the player_lives are more than 0 (which is correct) OR if game_over is true.
What you want is to stop the loop as soon as game_over == true, and continue when it is false. Your code does the opposite thing! Change it to
while (player_lifes > 0 && game_over == false)
Hope this helps :)
The other answers have already pointed out the logic error in your code and how to fix it. I want to suggest changes to make your code more readable and easier to write. I suggest using:
while ( keep_playing(player_lifes, game_over) )
{
...
}
where
bool player_has_lifes(int player_lifes)
{
return (player_lifes > 0);
}
bool game_is_not_over(bool game_over)
{
return (game_over == false);
}
bool keep_playing(int player_lifes, bool game_over)
{
return (player_has_lifes(player_lifes) && game_is_not_over(game_over));
}
You can make some of that still easier if you create a class to hold the data and add suitable member functions to the class.

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
string word;
int l,eFound,xFound;
l = word.size();
cout <<"Enter a word: ";
cin >> word;
for (l>0 ; word.at(l)!='x' || word.at(l)!='e'; l--)
if (word.at(l) == 'e'){
eFound = true;
}
else if (word.at(l) == 'x'){
xFound = true;
}
if (eFound == true && xFound == true){
cout << "Your word, "<<word<<", contains the character 'e'"<<"\n";
cout << "Your word, "<<word<<", contains the character 'x'";
}
if (eFound == true && xFound != true){
cout << "Your word, "<<word<<", contains the character 'e'";
}
if (xFound == true && eFound != true){
cout << "Your word, "<<word<<", contains the character 'x'";
}
I'm not sure what is going on I'm trying to use a for loop to detect either e or x in a input of some word. I've clicked on other pages with the same error but they have different codes and I don't really understand what is explained. So what is causing this error? I'm 2 weeks into my first programming class, sorry if I'm asking a dumb question.
The issue is that indexing of std::string starts from zero. Not from 1. So, word.at(l) will crash if l = word.size();.
You should change the statement to: l = word.size() - 1;.
Also, Change your loop condition to for (; l >= 0 ; l--)
Suggestion:
Please go for library functions:
Like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout <<"Enter a word: ";
cin >> word;
bool eFound = word.find('e') != string::npos;
bool xFound = word.find('x') != string::npos;
if (eFound) {
cout << "Your word, "<<word<<", contains the character 'e'" << "\n";
}
if (xFound) {
cout << "Your word, "<<word<<", contains the character 'x'" << "\n";
}
return 0;
}

How do I get out of this do-while 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 trying to create a program that intakes a string of characters, verifies it, then sorts it and prints it out.
I'm sure there is a glaring logic error in here somewhere, can someone help point it out? I've spent hours staring at my screen. I tried everything I know in my limited knowledge of C++, but I still can't get the thing working.
Anything you can offer will help me in some way, even if it's condescending.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void mySort(string &s);
int main()
{
string str;
char c;
bool invalid = true;
cout<<"Please enter some alphabetical characters:"<<endl;
cout<<"(* to end input): ";
do
{
getline(cin, str, '*');
for(int i = 0; i < str.length(); i++)
{
c = str.at(i);
}
if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )
{
cout<<"Error!"<<endl;
}
else
{
(invalid==false);
cout<<"You entered: "<<str<<endl;
mySort(str);
}
} while(invalid==true);
system("PAUSE");
return(0);
}
void mySort(string &s)
{
sort(s.begin(), s.end());
cout<<"The string after sorting is: "<<s<<endl;
}
I'm almost sure the problem with the verification lies in this line:
if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )
I'm sure my bools are wrong as well.
Anything, anything at all, I've wasted several hours of my life banging my head against the wall because of this.
You never set invalid to anything but true.
This line:
(invalid==false);
should be:
invalid = false;
The former version compares invalid to false, then throws away the result of the comparison. Nothing changes.
(invalid==false); Should be invalid=false;
First change:
(invalid == false);
invalid = false;
As others have said, you are not assigning the invalid variable correctly. You are also not validating the input string correctly, either. You loop through the entire string, and then validate only the last character seen, rather than validating each character while looping.
I would suggest re-writing the loop to get rid of the invalid variable and fix the validation, eg:
int main()
{
string str;
char c;
do
{
cout << "Please enter some alphabetical characters:" << endl;
cout << "(* to end input): ";
if (!getline(cin, str, '*'))
break;
if (str.empty())
cout << "You did not enter anything!" << endl;
else if (str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != string::npos)
cout << "Error! Bad input" << endl;
else
{
cout << "You entered: " << str << endl;
mySort(str);
break;
}
}
}
while (true);
system("PAUSE");
return 0;
}

While loop not working in program with boolean (C++) [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
Hi I was writing a simple program that asks for user input within a certain range. I used a boolean to start the while loop, but when I try declaring the boolean false in the first if statement, it doesn't end the loop and the program keeps asking for a user input. I've thought about using a break but shouldn't negating the condition stop the loop?
int num;
cout << "Enter a number";
bool invNum= true;
while (invNum = true)
{
cin >> num;
if (num >= 0 && num <= 20)
{
cout << "You typed: " << num << endl;
invNum = false;
//break;
}
if (num <= 0 || num >= 20)
{
cout << "Type another number: ";
}
}
return EXIT_SUCCESS;
}
= is not used for comparison. You should use ==. Change while (invNum = true) to while (invNum == true) or better to write while (true == invNum)
while (invNum = true)
Here is the problem.
It should read:
while (invNum == true)
What you're currently doing is assigning true to invNum. The assignment statement invNum = true returns the value of invNum after it has been set to true, in effect resulting in a while(true) loop.