Sign in to follow this  
Punkitten

Checking input info

Recommended Posts

I'm new to C++ and have been studying it for a few weeks now. i'm making a small program where it prompt the user for some input like name, race, ect, nothing big. What I want to do it do a check if a user interes digits instead of characters for thier user name. I curently have this
#include <iostream>
using namespace std;      

void GetCharName()
    {
        char CharName[11];
        cout << " Enter a character name ";
        cin >> CharName;
        if(CharName == 1,2,3,4,5,6,7,8,9,0)
                {
                    cout << " Invalid Character Name! " << endl;
                }
        else
                {    
                    cout << " You have chosen: " << CharName;
                }    
    }    

int main()
    {
        
        GetCharName();
        system("pause");
        return 0;
    }
    
Thanks for any help. P.S I'm using bloodshed Dev C++ and cant figure out how to access the stl.

Share this post


Link to post
Share on other sites
Instead of reading it into a character array read it into a std::string and use the find_first_of() member function with "0123456789" as an argument (and additionally whatever other characters you want to be invalid). If the result is equal to std::string::npos, then none of those characters were found. std::string can be found in the <string> header.

Share this post


Link to post
Share on other sites
why not use the isdigit() function.

If I'm getting this right you want to check that the user just enter's characters a-z?

in that case:


#include <iostream>

using namespace std;

void GetCharName()

{

char CharName[11];
char *chrptr;
int nameErr = 0;

memset(CharName,0,11);

cout << " Enter a character name ";

cin >> CharName;

charptr = CharName;

do
{
if (isdighit(*charptr)
{
nameErr = 1;
cout << " Invalid Character Name! " << endl;

}

++charptr;

}while (charptr != 0)


if (!nameErr)

{

cout << " You have chosen: " << CharName;

}

}






might work?

BUT...SiCrane does have a point. You seem to be limiting the names to 12 chars...with no test's that the user does follow your guidlines...hmmmmm. Using a std::string would be better, AND you get all the functionality that come's with strings..MARVELLOUS!!!

BTW...what errors do you get using a std::string?

Share this post


Link to post
Share on other sites
1) To use std::string requires including the appropriate header. In any case, it is definitely the best option here. Character arrays will cause all manner of headaches when you try to pass them between functions, impose limits on lengths, and are prone to all kinds of weird bugs when those length limits are not heeded.
2) Speaking of which, you are going to want to return a name from getCharName() so that the rest of the code can use it.
3) Confirming the user's name choice doesn't really belong in that function. You probably will want to do prompting in it, though, and probably loop until the user gives you something valid.
4) The originally suggested code is of course nowhere close to correct; what is needed is to iteratively check each character against each invalid character. This cannot be expressed with a single equality; there are two nested loops required (conceptually) for the algorithm.

Fortunately, the std::string::find_first_of() function does all the necessary work - it loops over the characters in the string's text, and for each, checks it against each character in the supplied string literal. (By the way, double-quoted values do NOT create std::strings; they create const char[]'s. An implicit conversion exists from the const char[] - being interpreted as a const char* - to a std::string, but not the other way around.) It will return a value indicating where the first match occurs, or a 'string::npos' value if there are no matches. In our case, no matches means valid input (no illegal chars), so we keep asking for input as long as there are matches, i.e. as long as the result is not the npos.

I will hold your hand for this.


#include <iostream>
#include <string> // for std::string type.
using namespace std;

string getCharName(const string& prompt) {
string result;
do {
cout << prompt; // show the prompt
getline(cin, result); // read in a value (the whole line of text)
} while (result.find_first_of("0123456789") != string::npos);
// i.e. until the characters are *not* found
return result;
}

int main() {
string name = getCharName("What is your name? ");
cout << "Sorry, " << name << ", you lose.\n"
<< "*** GAME OVER ***" << endl;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this