How can i controle what the user inputs? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i controle what the user inputs?

( example: a password : does the user writes abcdef or 123456. i tried it like this : string password;cin >> password;if ( password == string){cout << ...}else if ( password == int){cout << ...} ...

20th Jul 2016, 9:23 AM
Peter Müller
Peter Müller - avatar
15 Answers
0
Briefly describe what you want to establish!
20th Jul 2016, 9:36 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
i want that the user inputs a password. but i want that he can write 123456 ( numbers ) and abcdef ( letters )
20th Jul 2016, 9:39 AM
Peter Müller
Peter Müller - avatar
0
Then you can declare "password" as an array of characters. Then it can be both numbers as well as alphabets. Hope it helps
20th Jul 2016, 9:42 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
i didnt really understand arrays can you pls explain me how i can do it ?
20th Jul 2016, 9:43 AM
Peter Müller
Peter Müller - avatar
0
char password[30] ="iLoveC++123"; if(passwords match) cout<<"access granted"; else cout<<"try again"; Just a sample.
20th Jul 2016, 9:48 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
how can i output the array completely ? if i have an array : char a [8]; and in a for loop i want the user to input in this a array 8 chars. example : 1 2 3 4 a b c d . the for loop counts this like this: 1: 1 2: 2 3: 3 4: 4 5: a 6: b 7: c 8: d and now here i want this array comepletely outputted like this : cout a = 1 2 3 4 a b c d how can i do it ?
20th Jul 2016, 9:56 AM
Peter Müller
Peter Müller - avatar
0
Don't use endl in your cout statement
20th Jul 2016, 10:01 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
got it: #include <iostream> using namespace std; int main() { char password[8]; cout << "Geben Sie ein 8 - stelliges Passwort ein " << endl; for (int i = 0; i <= 7; i ++){ cin >> password[i]; } cout << password; return 0; }
20th Jul 2016, 10:13 AM
Peter Müller
Peter Müller - avatar
0
That should do the job :)
20th Jul 2016, 10:15 AM
chaitanya guruprasad
chaitanya guruprasad - avatar
0
now i want to write an if loop which 'couts' something if the password is just from letter ( qwertgfs ) and something else if its just from numbers ( 18382273 ). example (fail syntax i want to know the right) if ( password == int ){ cout << //(example)\* "your password isn't really safe"; } else if( password == string ) { cout << //something else } this password == string doesnt work how is the right syntax ?
20th Jul 2016, 10:21 AM
Peter Müller
Peter Müller - avatar
0
You can check every character with isdigit() and when it's all true it's a number if at least one is false it's a string
20th Jul 2016, 12:17 PM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
_Geometry dash_ _Roh_ (AKA NovaRate0315) - avatar
0
dont works like this: (password is a char datatype variable and already decleared) if (password = isdigit()){ cout << "wow" ; }
20th Jul 2016, 1:24 PM
Peter Müller
Peter Müller - avatar
0
No, Peter. isdigit(char value) returns if it is a number or not. apply them to all the characters in the string.
20th Jul 2016, 1:59 PM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
_Geometry dash_ _Roh_ (AKA NovaRate0315) - avatar
0
works #include <iostream> using namespace std; int main() { char x; cin >> x; if(isdigit(x)){ cout << "number is: "<< x << endl; }else if (!isdigit(x)){ cout << "isn't a number fgt"; } return 0; }
20th Jul 2016, 2:52 PM
Peter Müller
Peter Müller - avatar
0
#include <iostream> using namespace std; int main() { char x[8]; cout << "Put a Password with 8 letters/numbers" << endl; // x = Password for (int i = 0; i <= 7; i ++){ cin >> x[i]; } cout << x; if (isdigit(x)){ cout << "Your Password isn't really safe. It includes just numbers." ; // Doesn't matter if just numbers, just an information } if (!isdigit(x)){ cout << "Success: Password has been created"; << endl; } return 0; } what is the problem ? always error : something like unvalid change of char* to int
20th Jul 2016, 3:27 PM
Peter Müller
Peter Müller - avatar