help in c++ please

Hello,

I have an algorithm that I have to translate into C ++. I understand almost everything except:
If (city contains characters from 0 to 9) OR (city contains "select" or "insert")
Show "Incorrect entry"
how do we check if the character string contains numbers from 0 to 9 or words like select or insert?
and how to connect to a database?
Here is my algorithm
DEPARTMENT PROGRAM
VAR
city: Character string
START
Show 'Enter a city'
If (city contains characters from 0 to 9) OR (city contains "select" or "insert")
Show "Incorrect entry"
If not
Connection to the database
Search the city in the database
If the city is found
Show the name of the corresponding department
End if
End if
END
You can use == to compare two strings.

Then read http://www.cplusplus.com/reference/string/basic_string/ to study methods to help you with the other part.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Some headers

int main()
{
    VAR city: Character string
    Show 'Enter a city'
    If (city contains characters from 0 to 9) OR (city contains "select" or "insert")
    {
       Show "Incorrect entry"
    }
    else
    {
       Connection to the database
       Search the city in the database
       If the city is found
       {
          Show the name of the corresponding department
       }
    }
}


No idea what "database" you want, but a map<string,string> would do what you appear to want.
youll need a third party library to connect to an external database.
> how do we check if the character string contains numbers from 0 to 9 or words like select or insert?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

bool contains_digits( const std::string& str )
{
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    // https://en.cppreference.com/w/cpp/string/byte/isdigit
    for( unsigned char c : str ) if( std::isdigit(c) ) return true ;

    return false ;
}

bool contains_substring( const std::string& str, const std::string& substr )
{
    // https://en.cppreference.com/w/cpp/string/basic_string/find
    return str.find(substr) != std::string::npos ; // note: case sensitive
}

int main()
{

    std::string city ; // VAR city: Character string

    // Show 'Enter a city'
    std::cout << "Enter a city: " ;
    std::getline( std::cin, city ) ;

    // If (city contains characters from 0 to 9) OR (city contains "select" or "insert")
    if( contains_digits(city) || contains_substring( city, "select" ) || contains_substring( city, "insert" ) )
    {
        // Show "Incorrect entry"
        std::cerr << "Incorrect entry: '" << city << "'\n" ;
    }
    else // if not
    {
        // what ever
    }
}
thank you very very much for reply :D
I will also look at the links you gave me. but thank you very much for your help to all
Topic archived. No new replies allowed.