Login Register






Noob coding question filter_list
Author
Message
Noob coding question #1
I'm having a problem with my code, i'm a noob at programming i'm using c++ as you'll see and i can't get my if statement to work correctly, i'm trying to set my letters equal to numbers, i set the name equal to what they would be via the ASCII thing.  I want to say "what is your name" and they put their name and if its the name i put in the if statement then they will be asked to enter the password. I was going to rework the rest of my code but i figured i'd ask my question here first. Thanks in advance Smile
Code:
#include <iostream> using namespace std; int main() {    int name;    int pass;    cout << "What is your name" << endl;    cin >> name;    if (name == 689711410599107)    {        cout << "Your name is relevant" << endl;        cout << "Enter the password" << endl;        cin >> pass;    }        else if (pass = 456)        {            cout << "Access Granted" << endl;        }        else        {            cout << "Leave Immediately" << endl;        }    }

Reply

RE: Noob coding question #2
As far as I know, reading a char array (what you would use for a string in straight C and what cin does) into an int is undefined behavior (a fancy way of saying we don't know what's supposed to happen). Since you're using C++, however, you can use actual strings.

Code:
#include <iostream> using namespace std; int main(){ // declare the strings string name,pass; cout<<"What is your name"<<endl; // prompt the user cin>>name; // get their input // use the compare method for checking // string equality if(name.compare("name")){ // do your thing here } }

Also, put the else if and else blocks inside the first if block and change the else if to just if, all like so:
Code:
if(name.compare("name"){ // ... if(pass.compare("456")){ // do stuff }else{ // do other stuff } }
(This post was last modified: 10-18-2016, 03:05 AM by Inori.)
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 2 users Like Inori's post
Reply

RE: Noob coding question #3
(10-18-2016, 03:04 AM)Ao- Wrote: As far as I know, reading a char array (what you would use for a string in straight C and what cin does) into an int is undefined behavior (a fancy way of saying we don't know what's supposed to happen). Since you're using C++, however, you can use actual strings.

Code:
#include <iostream> using namespace std; int main(){ // declare the strings string name,pass; cout<<"What is your name"<<endl; // prompt the user cin>>name; // get their input // use the compare method for checking // string equality if(name.compare("name")){ // do your thing here } }

Also, put the else if and else blocks inside the first if block and change the else if to just if, all like so:
Code:
if(name.compare("name"){ // ... if(pass.compare("456")){ // do stuff }else{ // do other stuff } }

C is such a weird fucking language grammatically.
Kind of reminds me of Batch grammar and nomenclature
Scientia potentia est

[Image: inkexplosion.jpg]

Reply

RE: Noob coding question #4
(10-18-2016, 05:33 AM)DarkMuse Wrote:
(10-18-2016, 03:04 AM)Ao- Wrote: As far as I know, reading a char array (what you would use for a string in straight C and what cin does) into an int is undefined behavior (a fancy way of saying we don't know what's supposed to happen). Since you're using C++, however, you can use actual strings.

Code:
#include <iostream> using namespace std; int main(){ // declare the strings string name,pass; cout<<"What is your name"<<endl; // prompt the user cin>>name; // get their input // use the compare method for checking // string equality if(name.compare("name")){ // do your thing here } }

Also, put the else if and else blocks inside the first if block and change the else if to just if, all like so:
Code:
if(name.compare("name"){ // ... if(pass.compare("456")){ // do stuff }else{ // do other stuff } }

C is such a weird fucking language grammatically.
Kind of reminds me of Batch grammar and nomenclature

Batch is the most syntactically anal bullshit ever. Whitespace makes or brakes statements half the time.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 1 user Likes Inori's post
Reply

RE: Noob coding question #5
(10-18-2016, 03:04 AM)Ao- Wrote: Also, put the else if and else blocks inside the first if block and change the else if to just if, all like so:
Code:
if(name.compare("name"){ // ... if(pass.compare("456")){ // do stuff }else{ // do other stuff } }

Spot on conditional statement In this case.
[Image: AD83g1A.png]

Reply

RE: Noob coding question #6
So i took a different approach so it would be easier to add new things in the future, my main goal is to create an in depth login screen, here is the code i used (I used my buddy as a dummy to test it)
since it worked I'm going to change it to more relevant information thank you all Smile
Code:
string baby = "Kaysen"; string name = "Darick"; string input1; string pass = "password"; string input2; string input3; cout << "What is your name?" << endl; cin >> input1; if (input1 == name) { cout << "Your name is relevant" << endl; cout << "Prove it is you, what is your first babies name?" << endl; cin >> input3; if (input3 == baby) { cout << "Correct, what is your password?" << endl; } else { cout << "This is incorrect, Terminating program" << endl; } } else { cout << "Your name is not relevant please leave!" << endl; } cin >> input2; if (input2 == pass) { cout << "Access Granted" << endl; cout << "If this program worked please tell send me a message saying 'Alabama is great'" << endl; } else { cout << "Please leave if your name is not relevant" << endl; }

Reply