Current time: 29 May 2024, 04:36 AM



C++ Help #2 xD
Offline Leaf

Registered User
Registered
11 Years of Service
Posts: 397
Threads: 24
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Jun 2012
Reputation: 0
Programmer
#4
Re: C++ Help #2 xD

Yo just remembered this topic and threw this together hopefully its what you were looking for. it just compares each character to see if its a space or letter(upper and lowercase);
Code:
#include <iostream>
#include <string>
using namespace std;

bool ifValidName(string s)
{
    for(i=0;i<s.size();i++)
    {
        int asciicode =s[i];// convert char to ascii int value
        if(asciicode == 32 || (asciicode >=65 && asciicode<=90) || (asciicode >=97 && asciicode<=122));
        else return false;// if its not a space or letter return false
    }
    return true;
}
void main()
{
    string name;
    cout << "Enter your Full Name: " << endl;
    getline(cin, name);
    if(ifValidName(name)) cout << "real name" <<endl;
    else cout << "enter a valid input you noob" << endl;
    cout << endl;
    system("Pause");
}

If you want it to keep asking for the name until you get a valid one you can use goto statements(although some instructors dislike them) or a while loop. Idk if you've gone over functions yet. If not, I could modify the code to handle all of it in the main function.
09 May 2016, 03:36 AM
Find


Messages In This Thread

Forum Jump:


Users browsing this thread: 4 Guest(s)
SmG Gaming, © 2010-2024
Theme By: «SmG» Cloud
Edited by: «SmG» Wires