What's new
  • Welcome to PHCorner forums. Take a moment to sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help C++ User input

shiinamashiron

Established
Joined
Jun 28, 2015
Messages
196
Reaction
30
Age
25
Pahelp naman sa c++
Eto kasi yung gawa ko
#include <iostream>
Using namespace std;
int main()
{
Cout<<"Enter Employee Name: ";
getline (cin, name);
Cout<<name;

}
Output example
Juan Dela Cruz


Ang need ko po na code ay yung pag nag input na ng name na Juan Dela Cruz for example magtutuloy na sya pero pag Inulit yung code tapos nag input ng numbers or single character may lalabas na invalid input at babalik po ulit sa umpisa




Sana po may makatulong pasensya na din medyo magulo paliwanag
 
Oo boss ganyan pede patingin nung code?
C++:
#include<iostream>
using namespace std;
int main()
{
    string name;
    
    enter:
    cout << "Enter employee name: ";
    getline(cin, name);
    if (name == "Juan Dela Cruz") {
        cout << name;
        return 0;
    } else {
        cout << "The name you entered is invalid." << endl;
        goto enter;
    }
}
 
F
Napakastatic naman niyan sir, pero A for the effort, mahirap naman turuan ng regular expression si TS if wala pa sila diba?
 
C++:
#include<iostream>
using namespace std;
int main()
{
    string name;
   
    enter:
    cout << "Enter employee name: ";
    getline(cin, name);
    if (name == "Juan Dela Cruz") {
        cout << name;
        return 0;
    } else {
        cout << "The name you entered is invalid." << endl;
        goto enter;
    }
}
ayy bat juan lang

if kung hindi juan nilagay jan ajahahaha
 
1 Comment
S
shiinamashiron
Yun din po napansin ko mali ako pagkakaintindi hahaha
 

shiinamashiron

Established
C++:
#include<iostream>
using namespace std;
int main()
{
    string name;
    
    enter:
    cout << "Enter employee name: ";
    getline(cin, name);
    if (name == "Juan Dela Cruz") {
        cout << name;
        return 0;
    } else {
        cout << "The name you entered is invalid." << endl;
        goto enter;
    }
}

Ayy kaso sir nakafix pala sa juan dela cruz yung name mali pala ko pagkaintindi sa sinend mo sorry po
Ganto po sir yung hinahanap ko na output
Enter employee name : a
Invalid input!
Enter employee name: 12345
Invalid input!
Enter employee name: Juan Dela Cruz
Tas end na po

Bali ang hindi po tatangapin dapat ay yung a at 12345
 
Comment
Ayy kaso sir nakafix pala sa juan dela cruz yung name mali pala ko pagkaintindi sa sinend mo sorry po
Ganto po sir yung hinahanap ko na output
Enter employee name : a
Invalid input!
Enter employee name: 12345
Invalid input!
Enter employee name: Juan Dela Cruz
Tas end na po

Bali ang hindi po tatangapin dapat ay yung a at 12345
C++:
#include <iostream>
#include <sstream>

using namespace std;
int main()
{
    string name;

    do {
        cout << "Enter employee name: ";
        getline(cin, name);

        if (name.length() > 1) {
            istringstream iss(name);
            int i;

            if (!(iss >> i)) {
                cout << name;
                break;
            }
        }
    } while (true);
}
 
1 Comment
S
shiinamashiron
Eto na yung hinahanap ko thankyouuuu sirrr nakumpleto na yung code ko
 

homer_simpson

Eternal Poster
Paano kung yung input eh may number, ie Juan Dela Cruz6 o kaya Juan De3la Cruz? If that is the case, dapat may char-by-char check.
 
Comment
Paano kung yung input eh may number, ie Juan Dela Cruz6 o kaya Juan De3la Cruz? If that is the case, dapat may char-by-char check.
Well in reality lods wala naman name na may kasamang integer, kahit saang form pa online di naman sila nag aaccept ng name na may integer even irl, but if were gonna talk about username then thats another case which is di naman yung hinahanap ni ts.
 
Comment

homer_simpson

Eternal Poster
Well in reality lods wala naman name na may kasamang integer, kahit saang form pa online di naman sila nag aaccept ng name na may integer even irl, but if were gonna talk about username then thats another case which is di naman yung hinahanap ni ts.
In reality bossing, user can and will try all stupid shits, deliberate or not. Paano kung na-typo ng user without noticing? That is why input validation is extremely important.

Quoting the TS, he says "...pero pag Inulit yung code tapos nag input ng numbers or single character may lalabas na invalid input at babalik po ulit sa umpisa".
 
1 Comment
K
kramsuiluj
Ah ok sorry, i thought you mean pano kung yung name na ieenter may integer, yes validation is extremely important and i am well aware of that, and that is what im trying to say, so were on the same side. Well its programming so there's a lot of ways to do that validation like this:
C++:
#include<iostream>
#include<string>
using namespace std;

string getName()
{
   const string notAllowed = "0123456789!?~#@{}|£$%^&*+";
   const string notOnly = " ";
  
   string name;
   cout << "Enter employee name: ";
   getline( cin, name );
  
   bool wrong = name.length() <= 1
             || name.find_first_of( notAllowed ) != string::npos
             || name.find_first_not_of( notOnly ) == string::npos
            
             ;
  
   return wrong ? getName() : name;
}


int main()
{
   string name = getName();
   cout << name << '\n';
}
Im sure there are easier ways to do this, but im sure in his current level he wouldn't be able to absorb all of this.
 

Online statistics

Members online
2,022
Guests online
1,651
Total visitors
3,673

Forum statistics

Threads
825,440
Messages
16,329,668
Members
1,443,651
Top