Current time: 18 Apr 2024, 09:48 AM



C++ Help #2 xD
Away SmG FlasH™

Senior Member
Senior Member
******
10 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#1
C++ Help #2 xD

Code:
#include <iostream>
#include <string>
using namespace std;

void main()
{
string name;
cout << "Enter your Full Name: " << endl;
getline(cin, name);
cout << endl;

}

I'd like to display a message saying "Please enter a valid Input" when the user inputs any value other than alphabets.
[Image: SPOILER_FlasH-1.gif]
07 May 2016, 11:00 AM
Find
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
#2
Re: C++ Help #2 xD

Make a function to loop through the input string and check each char to see if it's a valid character or not. If you want I'll post code tomorrow, I'm busy tonight.
07 May 2016, 10:04 PM
Find
Offline «SmG» Cloud

The King
Administrators
************
13 Years of Service
Posts: 6,893
Threads: 2,104
Likes Received: 482 in 172 posts
Likes Given: 107
Joined: Oct 2010
Reputation: 545
Discord: Cloud#2911
Speaker of the HouseDrunkFortniterDrunkDiscord Warrior5K
A Knight of The Round TableGamerTwitterHalloweenPet LoverChristmas 2018
Valentines Day RubyGolden GuardianBoost GangHalloween 2019ScoutBug Hunter
#3
Re: C++ Help #2 xD

SmG Leaf Wrote:Make a function to loop through the input string and check each char to see if it's a valid character or not. If you want I'll post code tomorrow, I'm busy tonight.
LEAF you da best. By the way after this week is over I need to catch up with you man.
[Image: b4eQPAf.gif]
09 May 2016, 01:12 AM
Website Find
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
Away SmG FlasH™

Senior Member
Senior Member
******
10 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#5
Re: C++ Help #2 xD

I've done functions, but only the basic (to my knowledge). Thanks for the code. Is there a way to do it without converting it to an ascii value?
[Image: SPOILER_FlasH-1.gif]
09 May 2016, 05:04 AM
Find
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
#6
Re: C++ Help #2 xD

Have you gone over functions that return a value, or just void functions? If it's just void you can put the cout in the function where the return is and change it to a void. And yea you can do it without ascii but you'd have to have a switch or if statement for every upper and lowercase letter. The ASCII way is just less code but you can do it however you want.
09 May 2016, 01:09 PM
Find
Away SmG FlasH™

Senior Member
Senior Member
******
10 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#7
Re: C++ Help #2 xD

SmG Leaf Wrote:Have you gone over functions that return a value, or just void functions? If it's just void you can put the cout in the function where the return is and change it to a void. And yea you can do it without ascii but you'd have to have a switch or if statement for every upper and lowercase letter. The ASCII way is just less code but you can do it however you want.

Oh k thanx man. I've done functions that return a value, yep. Parameters and stuff. Thanx agin!
[Image: SPOILER_FlasH-1.gif]
10 May 2016, 02:44 AM
Find


Forum Jump:


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