How do I make a string false if it counts more than one symbol?

Hi I want to input a name into a program and it can only accept one (') and or one (-)
,if it finds more than one than one of each of these symbols, I can have a name with ' or -, but there cant be -- or '' in a name.

, it should do this after

cout<<"That name is invalid";
Any ideas?
Last edited on
Off the top of my head.

I would have a bool flag and an int counter and let the counter trip the flag to false when needed.
You could write a check_if_valid() function, which checks a string and returns true if it's valid, false otherwise.

1
2
3
#include <string>

bool check_if_valid(std::string name);


As a C++ programmer, you should prefer to use std::string instead of character arrays char[]. The reason is that C++ strings are easier to work with than C strings.

If you don't know how to read an std::string as the name here's a sample main() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
    std::string name;

    std::cout << "Enter your name: ";
    std::getline(std::cin, name);

    if (check_if_valid(name))
        std::cout << "The name is valid.\n";
    else
        std::cout << "The name is invalid.\n";

    std::cout << std::endl;
}


Back to the check_if_valid() function, all you need to do is search the given string for the characters you want, and count how many times they appear in total.

If the total is greater than one, the your function will return false, and true otherwise.

You could use std::string::find() for this purpose.
http://www.cplusplus.com/reference/string/string/find/

You could also use a for() loop, which is a bit easier I guess:

1
2
3
for (std::string::const_iterator ci = name.begin(); ci != name.end(); ++ci)
    if (*ci == '\'' || *ci == '-')
        ++total;
That for loop looks like the answer.thanks, can you explain your for loop more
Actually if a valid name can contain both an apostrophe (') and dash (-) at the same time, then my idea isn't the correct solution.

One way to correct it could be to count apostrophes and dashes independently, not in the same total variable.

ianol wrote:
can you explain your for loop more

It would help me if you could say what you need me to explain.

Anyway, I will assume that you wonder about iterators, and so I'll talk about those.
Normally in a for() loop, when you want to go over all elements of an array in order ("iterate") you use indexes. This is the natural, C way of doing things.

However the C++ style of iterating containers is by using their given iterators.

So:

1
2
3
4
5
6
7
for (std::string::const_iterator ci = name.begin(); ci != name.end(); ++ci)
    std::cout << *ci << '\n';

// kind of equivalent to...

for (std::size_t i=0; i < name.length(); ++i)
    std::cout << name[i] << '\n';


I know that from the example above, iterating doesn't make a lot of sense, but be aware that there are containers which cannot access their elements by an index, for example: std::list, std::set, std::map. So it becomes a reflex to avoid using indexes.

http://www.cplusplus.com/reference/stl/
Last edited on
Topic archived. No new replies allowed.