Function to check if string contains a number - c++

I'm working on a project in c++ (which I just started learning) and can't understand why this function is not working. I'm attempting to write a "Person" class with a variable first_name, and use a function set_first_name to set the name. Set_first_name needs to call a function(the one below) to check if the name has any numbers in it. The function always returns false, and I'm wondering why? Also, is this the best way to check for numbers, or is there a better way?
bool Person::contains_number(std::string c){ // checks if a string contains a number
if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos
|| c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos
|| c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number
return false;
}
return true;
}

Change all your || to &&.
Better yet:
return std::find_if(s.begin(), s.end(), ::isdigit) != s.end();
Or, if you have it:
return std::any_of(s.begin(), s.end(), ::isdigit);

C++11:
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
bool has_any_digits(const std::string& s)
{
return std::any_of(s.begin(), s.end(), ::isdigit);
}
int main()
{
std::string query("H311o, W0r1d!");
std::cout << query << ": has digits: "
<< std::boolalpha
<< has_any_digits(query)
<< std::endl;
return 1;
}
Output:
H311o, W0r1d!: has digits: true

It always returns false because your logic is backwards. You are using the || operator with == npos checks. If any one particular digit is missing from the string, == npos evaluates to true and || is satisfied, so you return false. You need to using != npos checks and then return true if any check evaluates to true:
bool Person::contains_number(const std::string &c)
{
if (c.find('0') != std::string::npos ||
c.find('1') != std::string::npos ||
c.find('2') != std::string::npos ||
c.find('3') != std::string::npos ||
c.find('4') != std::string::npos ||
c.find('5') != std::string::npos ||
c.find('6') != std::string::npos ||
c.find('7') != std::string::npos ||
c.find('8') != std::string::npos ||
c.find('9') != std::string::npos)
{
return true;
}
return false;
}
Or:
bool Person::contains_number(const std::string &c)
{
return (
c.find('0') != std::string::npos ||
c.find('1') != std::string::npos ||
c.find('2') != std::string::npos ||
c.find('3') != std::string::npos ||
c.find('4') != std::string::npos ||
c.find('5') != std::string::npos ||
c.find('6') != std::string::npos ||
c.find('7') != std::string::npos ||
c.find('8') != std::string::npos ||
c.find('9') != std::string::npos
);
}
A simplier solution is to use find_first_of() instead of find():
bool Person::contains_number(const std::string &c)
{
return (c.find_first_of("0123456789") != std::string::npos);
}

How to test if a string contains any digits in C++
This should do it!
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}

You are using || (or operator) to check several conditions in an if statement.
The or operator returns true (satisfies the condition) if one of the expression is true.
The or operator evaluates first the expression on its left: if that is true then it doesn't evaluate the expression on its right and returns true.
If the expression on the left is false then the expression on the right is evaluated and the result of it is returned as result of || operator
This is what happen in your function:
does c contains '0' ? if no (because std::string::npos in find() means not found) then return false
does c contains '1' ? if no then return false
...
So, replace the or operators with && (and operator).

Related

Composition of OR statements does not yield the correct boolean value when comparing strings

I have the following code:
void funct(string m){
if (m != "s" || m != "l" || m != "r" || m != "S" || m != "L" || m != "R") {
//Do something
}
}
When passed value "r" the conditional body should not be executed, due to m!="r". Anyways "Do something" is executed.
On the other side when testing the following statement
void funct(string m){
if (m != "r") {
//Do something
}
}
Given m = "r" the function works as intended.
On the other hand, if use the == operator instead of != I get the correct composite value:
if (!(m == "l" || m == "l" || m == "r" || m == "S" || m == "L" || m == "R")) {
//Do something
}
This works just fine.
Did anybody else have similar issues? Or explain the "misbehavior" of the first chunk of code.
Lang standard C++14
Grateful in advance
It's surprising that you can spell out the problem so clearly and yet not see the mistake you are making
if (m != "s" || m != "l" || m != "r" || m != "S" || m != "L" || m != "R") {
//Do something
}
'Do something' will be executed if EITHER m != "s" OR if m != "l" OR ....
Since every string in the world is either not equal to "s" or not equal to "l" then 'do something' will always be executed. Think about it, "s" is not equal to "l" and "l" is not equal to "s" and every other string is not equal to both of them.
The code you really want is this
if (m != "s" && m != "l" && m != "r" && m != "S" && m != "L" && m != "R") {
//Do something
}
To be fair this is an extremely common mistake to make in logic.
You can use the C++ standard library functions to reduce mistakes. std::none_of from the algorithm header works great in this situation. If your string matches none of the other strings, it will return true.
#include <algorithm>
void funct(const string& m) {
const string tokens[] = {"s", "l", "r", "S", "L", "R"};
auto compare = [&m](const string& token) { return m == token; };
if (none_of(begin(tokens), end(tokens), compare)) {
//Do something
}
}

C++ Functions run no matter input

The problem with the code below is, no matter what what the input is in the console, all the functions run as if they were true. As an example, I could input "vvvvvvvvvvvvvvvv" and both "hello" and "good thank you" would output. Its as if the parameters of the functions don't matter.
#include <windows.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
void main()
{
void confirmChk(bool &confirm);
bool confirm = false;
void greetingChk(bool &greeting);
bool greeting = false;
void questionChk(bool &question);
bool question = false;
void youChk(bool &you);
bool you = false;
std::string text;
std::getline(std::cin, text);
for (int read = 0; read < text.length(); read++)
{
greetingChk(greeting);
if (greeting = true);
{
youChk(you);
if (you = true);
{
std::cout<<"hello" <<std::endl;
}
}
questionChk(question);
if (question = true);
{
youChk(you);
if (you = true);
{
std::cout<<"good thank you" <<std::endl;
}
}
std::chrono::milliseconds dura(2000);
std::this_thread::sleep_for(dura);
system("cls");
}
}
//////////////////Functions//////////////////
void greetingChk(bool &greeting)
{
std::string text;
std::getline(std::cin, text);
if(text.find("hi ") != std::string::npos ||
text.find("hello ") != std::string::npos ||
text.find("hey ") != std::string::npos ||
text.find("yo ") != std::string::npos ||
text.find("sup ") != std::string::npos ||
text.find("howdy ") != std::string::npos ||
text.find("wazzup ") != std::string::npos ||
text.find("hiya ") != std::string::npos)
{
greeting = true;
}
else
{
greeting = false;
}
}
void youChk(bool &you)
{
std::string text;
std::getline(std::cin, text);
if(text.find("ya ") != std::string::npos ||
text.find("you ") != std::string::npos ||
text.find("yah ") != std::string::npos ||
text.find("yall ") != std::string::npos ||
text.find("you're ") != std::string::npos)
{
you = true;
}
else
{
you = false;
}
}
void questionChk(bool &question)
{
std::string text;
std::getline(std::cin, text);
if(text.find("are") != std::string::npos ||
text.find("am") != std::string::npos ||
text.find("can") != std::string::npos ||
text.find("could") != std::string::npos ||
text.find("do") != std::string::npos ||
text.find("does") != std::string::npos ||
text.find("did") != std::string::npos ||
text.find("has") != std::string::npos ||
text.find("had") != std::string::npos ||
text.find("have") != std::string::npos ||
text.find("is") != std::string::npos ||
text.find("may") != std::string::npos ||
text.find("might") != std::string::npos ||
text.find("shall") != std::string::npos ||
text.find("should") != std::string::npos ||
text.find("was") != std::string::npos ||
text.find("would") != std::string::npos ||
text.find("were") != std::string::npos)
{
question = true;
}
else
{
question = false;
}
}
void confirmChk(bool &confirm)
{
std::string text;
std::getline(std::cin, text);
if(text.find("ok") != std::string::npos ||
text.find("yup") != std::string::npos ||
text.find("yes") != std::string::npos ||
text.find("affirm") != std::string::npos ||
text.find("affirmative") != std::string::npos ||
text.find("confirm") != std::string::npos ||
text.find("confirmed") != std::string::npos ||
text.find("confirming") != std::string::npos ||
text.find("endorse") != std::string::npos ||
text.find("endorsed") != std::string::npos ||
text.find("approve") != std::string::npos ||
text.find("approved") != std::string::npos ||
text.find("approving") != std::string::npos ||
text.find("of course") != std::string::npos ||
text.find("got it") != std::string::npos ||
text.find("will do") != std::string::npos ||
text.find("alright") != std::string::npos ||
text.find("fine") != std::string::npos ||
text.find("varify") != std::string::npos ||
text.find("ratify") != std::string::npos ||
text.find("validate") != std::string::npos ||
text.find("understood") != std::string::npos ||
text.find("justify") != std::string::npos)
{
confirm = true;
}
else
{
confirm = false;
}
}
Your if statements have more entries then a teenage girls phone. :-)
You should invest in some structures, containers and loops.
For example:
const static std::string question_words[] =
{"am", "are", "can", "did", "could", "do", "does"};
const static unsigned int word_quantity =
sizeof(question_words) / sizeof(question_words[0]);
//...
bool is_question = false;
for (unsigned int i = 0; i < word_quantity; ++i)
{
if (text == question_word[i])
{
is_question = true;
break;
}
}
You can put this logic into a function, and pass it the word array and the word. That way, you can call the function for the different word containers and not write extra code. Your code would like like:
bool is_question = false;
is_question = Is_Word_In_Array(question_words, word_quantity, text);
if (is_question)
{
cout << "sentence is a question\n";
}
if (Is_Word_In_Array(greeting_words, greeting_word_quantity, text))
{
cout << "sentence has a greeting.\n";
}
If you sort the arrays, you can use existing search functions like std::binary_search to find your word.
I hardly know where to begin. Your main loop executes as many times as there are characters in your input string.
All of your if statements are true, because you set them that way.
Saying
if (greeting = true);
first sets greeting to true, and then does nothing (because of the semicolon right after the conditional clause).
The following statements within braces will be executed because they are not part of the if. Again, because of the semicolon after the conditional clause.
Use something more like
if (greeting)
{
// conditional code here
}
I think you really meant if (greeting == true) which is the same as if (greeting).
Here's one of your problems: You're using the assignment operator = instead of the equality operator ==. So, when you use:
if (greeting = true)
...you're saying "set greeting to true. if the results of this operation return true, then ..." What you want to use is this:
if (greeting == true)
This says "compare greeting to true. if the results of this operation return true (i.e. if they are equal), then..." However, you can actually make this a little more concise with this:
if (greeting)

C++ using char in an if statement

I'm building a standard four-function calculator and I've come across a confusing bug.
char Engine::AskUser(){
char type;
std::cout << "'a'dd, 'm'ulitply, 's'ubract, or 'd'ivide ?\n";
std::cin >> type;
CheckUser(type);
return type;
}
void Engine::CheckUser(char uType){
if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd'){
std::cout << "Type 'a', 'm', 's', or 'd'\n";
AskUser();
}
else
return;
}
What happens is, even if I enter a, s, m, or d, the if statement still iterates as if those conditions were true, which is clearly not the case. I don't get it. Is uType not carrying the value of type from AskUser(), or something similar?
if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd')
A char is either not equal to 'a', or it is equal to 'a', in which case it's not equal to 's', so this condition is always true.
It should be logical AND:
if(uType != 'a' && uType != 's' && uType != 'm' && uType != 'd')
your if conditional is doing the following check:
if value is not equal to a --
or
if value is not equal to s --
or
if value is not equal to m --
or
if value is not equal to d --
regardless of what char variable uType is, it will always NOT BE at least 3 of the previous 4 variables, hence the if conditional will always result in a TRUE value.
Edited: What I believe you wanted to do was the following:
if ( (uType == 'a') || (uType == 's') || ......)
{
return;
}
else
{
...//The code you previously had if it were true
}
In this case, you would return whenever uType was 1 of the 4 values, otherwise you would implement your code.
Change this line -
if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd')
to this -
if(uType != 'a' && uType != 's' && uType != 'm' && uType != 'd')
It will surely work.

Assign a string from an iterator

Here is a problem I have , I have a vector of filenames and I want to check if they end by .jpg or by .png so I made some code with iterators and the STL, this i also for creating a std::map with those names as keys and with value a texture, so here is my code, that does a Segmentation Fault error at the line 11:
#include "TextureManager.h"
std::map<std::string,sf::Texture> createTextureArray(){
std::map<std::string,sf::Texture> textureArray;
std::vector<std::string>::iterator strIt;
std::string fName;
for(strIt = textureFileList().begin(); strIt != textureFileList().end(); strIt++){
sf::Texture texture;
fName = *strIt;
if(fName[fName.size()] == 'g' && fName[fName.size()-1] == 'p' && fName[fName.size()-2] == 'j' && fName[fName.size()-3] == '.'){
texture.loadFromFile(fName);
textureArray[fName] = texture;
}
else if(fName[fName.size()] == 'g' && fName[fName.size()-1] == 'n' && fName[fName.size()-2] == 'p' && fName[fName.size()-3] == '.'){
texture.loadFromFile(fName);
textureArray[fName] = texture;
}
}
return textureArray;
}
I think this is the only code needed to try to understand the problem , but if anyone wants more of this code here is the Github repo
This is not shown in your question, but textureFileList returns by value, which means that you get a copy of the std::vector<std::string> it returns. You're calling this function twice, once for begin() and then once for end(), which means you're calling those functions on different copies of the vector. Obviously the beginning of one copy has no relation to the end of another copy. Not only this, but those copies are being destroyed immediately afterwards, because they are temporaries. Instead, you should store a single copy and call begin and end on that:
std::vector<std::string> fileList = textureFileList();
for(strIt = fileList.begin(); strIt != fileList.end(); strIt++){
And fName[fName.size()] is always '\0'.
You should use
if(fName[fName.size()-1] == 'g' && fName[fName.size()-2] == 'p' && fName[fName.size()-3] == 'j' && fName[fName.size()-4] == '.'){
and
else if(fName[fName.size()-1] == 'g' && fName[fName.size()-2] == 'n' && fName[fName.size()-3] == 'p' && fName[fName.size()-4] == '.'){
I suppose that function textureFileList() returns an object of type std::vector<std::string> by reference.
The segmentation fault can occur due to accessing non-existent character of a string.
if(fName[fName.size()] == 'g' && fName[fName.size()-1] == 'p' && fName[fName.size()-2] == 'j' && fName[fName.size()-3] == '.'){
There is no such character of the string with index fName.size(). The valid range of indexes is 0, size() -1 provided that size() is not equal to zero.
You should use another approach. You have to find the period using member function rfind() and then compare the substring with a given extension.
Here is an example
#include <iostream>
#include <string>
int main()
{
std::string s( "SomeFile.jpg" );
std::string::size_type n = s.rfind( '.' );
if ( n != std::string::npos && s.substr( n ) == ".jpg" )
{
std::cout << "It is a JPEG file" << std::endl;
}
return 0;
}
The output is
It is a JPEG file
Your for loop could be written the following way
for ( const std::string &fName : textureFileList() )
{
const char *ext[] = { "jpg", "png" };
std::string::size_type n = s.rfind( '.' );
if ( n != std::string::npos &&
( fName.substr( n + 1 ) == ext[0] || fName.substr( n + 1 ) == ext[1] ) )
{
sf::Texture texture;
texture.loadFromFile( fName );
textureArray[fName] = texture;
}
}
If you need to erase the extension then you can write simply
fName.erase( n );
In this case fName has to be defined as a non-const reference in the for statement.

c++: string::insert(string::iterator _where, char _Ch) suddenly not working

I'm doing some string manipulation, and am looping through a string with a string iterator, and under certain conditions insert a character into the string. Here is the code:
string * const Expression::process(char * const s)
{
if(s == NULL)
{
printf("(from Expression::process())\n > NULL data");
return NULL;
}
string *rtrn = new string(s);
string garbage;
//EDIT
rtrn->erase(remove(rtrn->begin(), rtrn->end(), ' '), rtrn->end());
for(string::iterator j = rtrn->begin(); (j+2) != rtrn->end(); j++)
{
if(Operator::isValid(&*j, garbage) != Operator::SYM && *(j+1) == '-' && (Operator::isValid(&(*(j+2)), garbage) != Operator::INVALID))
rtrn->replace(j+1, j+2, "+-");
}
rtrn->insert(rtrn->begin(), '(');
rtrn->append(")");
for(string::iterator k = rtrn->begin(); k+1 != rtrn->end(); k++)
{
if(*k == '-' && !Operator::isValidNum(*(k+1)))
rtrn->replace(k, k+1, "-1*");
if((Operator::isValid(&*(k+1), garbage) != Operator::INVALID && (Operator::isValid(&*(k+1), garbage) != Operator::SYM || *(k+1)=='(')) &&
(Operator::isValid(&*k, garbage) == Operator::VAR || Operator::isValidNum(*k) || *k==')') &&
!(Operator::isValid(&*k, garbage) == Operator::NUM && Operator::isValid(&*(k+1), garbage) == Operator::NUM))
{
if(Operator::isValid(&*k, garbage) == Operator::SYM)
{
if(opSymb::valid[garbage]->getArguments())
rtrn->insert(k+1, '*');
}
else
{
rtrn->insert(k+1, '*');
}
}
}
return rtrn;
}
When s is equal to "20x(5x+3)-6x(5x^2+11/2)", I get a runtime error at rtrn->insert(k+1, '*'); under the else statement when it gets to "5x^2" in the string. Basically, when it makes the 6th insertion, it crashes on me and complains about the iterator + operator being out of range. Although, when I'm debugging, it does pass the correct offset. And it does successfully insert the char into the string, but after the function executes, the iterator is pointing to corrupt data.
for(string::iterator i = rtrn->begin(); i != rtrn->end(); i++)
{
if(*i == ' ')
rtrn->erase(i);
}
There are errors in this and all code snippets like this: for loop can`t be used for deleting element from a container, becase erase() - invalidates all iterators related to the container,
I offer you to use while loop instead, here is a short example from another question I answered:
string::iterator it = input.begin();
while (it != input.end())
{
while( it != input.end() && isdigit(*it))
{
it = input.erase(it);
}
if (it != input.end())
++it;
}
So after research and help from you guys, it seems I have to refine my code so that any string functions such as erase, insert, or replace writes over the iterator passed to the function. So I need to change my code to something like this
for(string::iterator k = rtrn->begin(), m=k+1; m != rtrn->end(); k=m, m=k+1)
{
if(*k == '-' && !Operator::isValidNum(*m))
rtrn->replace(k, m, "-1*");
if((Operator::isValid(&*m, garbage) != Operator::INVALID && (Operator::isValid(&*m, garbage) != Operator::SYM || *m=='(')) &&
(Operator::isValid(&*k, garbage) == Operator::VAR || Operator::isValidNum(*k) || *k==')') &&
!(Operator::isValid(&*k, garbage) == Operator::NUM && Operator::isValid(&*m, garbage) == Operator::NUM))
{
if(Operator::isValid(&*k, garbage) == Operator::SYM)
{
if(opSymb::valid[garbage]->getArguments())
rtrn->insert(m, '*');
}
else
{
m=rtrn->insert(m, '*');
}
}
}