What's new

Help C++ User input

Joined
Jun 28, 2015
Posts
774
Reaction
286
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
 

kramsuiluj

Honorary Poster
Ganito ba ibig mong sabihin?
21.PNG
 

Attachments

kramsuiluj

Honorary Poster
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;
    }
}
 
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
 
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
 

kramsuiluj

Honorary Poster
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);
}
 

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.
 

kramsuiluj

Honorary 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.
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.
 

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".
 

New Topics

Online statistics

Members online
1,497
Guests online
1,304
Total visitors
2,801

Forum statistics

Threads
1,196,206
Posts
21,179,901
Members
1,594,430
Attachments
1,678,946
Profile posts
99,729
Top