C,C++,C#

error check

Post by Desoxena on Sat Oct 16, 2010 1:00 pm
([msg=47639]see error check[/msg])

//this is a password check script I devised; can someone error check it?
//and don't be too harsh on my foolish mistakes should there be any, im still learning
#include <iostream>
using namespace std;
int main()

int pass;
{
cin >> 'pass' >> end1;
if (pass == happy||pass == Happy);
cout << "welcome!" << end1;
}{
else (pass != happy||pass != Happy);
cout << "incorrect user ID" << end1;

return 0;
}
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by Goatboy on Sat Oct 16, 2010 2:18 pm
([msg=47643]see Re: C,C++,C#[/msg])

At a first glance, I'd get rid of everything in the else clause. It's not necessary, and I believe you're thinking of an "else if" clause.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by Desoxena on Sat Oct 16, 2010 8:53 pm
([msg=47660]see Re: C,C++,C#[/msg])

Oh, I thought that you would put else if, if there was more than two executable conditions.
example:
if (condition)
else if (condition2)
else (condition3)
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by Assassian360 on Sat Oct 16, 2010 10:11 pm
([msg=47661]see Re: C,C++,C#[/msg])

Code: Select all
#include <iostream>
using namespace std;

int main()
{
     int happy = 1, HAPPY = 2; // refer to my comments after this code
     int pass;
     cin >> pass >> endl; // no quotes around pass, and it is endl (an L) not a 1
     if (pass == happy || pass == Happy) { 
  // ^ need opening { and you don't put a ; on same line as an if, otherwise the if does nothing
          cout << "welcome!" << endl;
     } else {
          cout << "incorrect user ID" << endl;
    }

    return 0;
}


I have changed the code so that it should compile. At the moment it will allow you to see the message "welcome!" if the user types in a 1 or a 2. This is what the happy = 1, and HAPPY = 2 are doing. You won't be able to do a direct == comparison if what you are actually wanting is to take something like a string as the input and check it against a password. At the moment you have pass defined as an integer, so I used that as the basis for how I altered your code.

I didn't comment on all changes, but compare your code to what I have changed it to and let me know if you have further questions.

Edit:
Also in regard to your:
Oh, I thought that you would put else if, if there was more than two executable conditions.
example:
if (condition)
else if (condition2)
else (condition3)


In this case you have only 2 possible situations, either they have entered a password correctly, or they have entered a password incorrectly. If entering different passwords were to grant different messages, than you could have something like:

Code: Select all
if(pass == happy) {
    cout << "welcome user!" << endl;
} else if(pass == HAPPY) {
    cout << "welcome other user!" << endl;
} else {
    cout << "incorrect password" << endl;
}
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: C,C++,C#

Post by Desoxena on Sun Oct 17, 2010 3:24 pm
([msg=47686]see Re: C,C++,C#[/msg])

Well in that last code you showed, wouldn't you have to put a cin for console input? So the user could even enter a password for the program to even run?
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by thetan on Mon Oct 18, 2010 7:36 am
([msg=47712]see Re: C,C++,C#[/msg])

Desoxena wrote:Oh, I thought that you would put else if, if there was more than two executable conditions.
example:
if (condition)
else if (condition2)
else (condition3)


In the C standard, there is no such thing as an else if statement and instead it only exists by convention.
Code: Select all
if (something)
{
    // look busy
}
else
    if (something else)
    {
        //slave away
    }

would be a more true to picture visualization.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: C,C++,C#

Post by Avery17 on Mon Oct 18, 2010 10:16 am
([msg=47718]see Re: C,C++,C#[/msg])

All bow to Thetan, the all knowledgeable!
Avery17
Experienced User
Experienced User
 
Posts: 81
Joined: Fri Sep 17, 2010 11:28 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by Assassian360 on Mon Oct 18, 2010 5:02 pm
([msg=47753]see Re: C,C++,C#[/msg])

Desoxena wrote:Well in that last code you showed, wouldn't you have to put a cin for console input? So the user could even enter a password for the program to even run?

Yes, you would. That example code was purely to demonstrate that there is the possibility there for the else-if.

thetan wrote:In the C standard, there is no such thing as an else if statement and instead it only exists by convention.

His code that he has shown is in a traditionally C++ approach, so I decided to assume he is using C++ which does include the "else if" natively. It is a valid point though if he should ever decide to learn C as well, or any other language that doesn't include "else if" natively.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: C,C++,C#

Post by thetan on Mon Oct 18, 2010 5:25 pm
([msg=47754]see Re: C,C++,C#[/msg])

Assassian360 wrote:His code that he has shown is in a traditionally C++ approach, so I decided to assume he is using C++ which does include the "else if" natively. It is a valid point though if he should ever decide to learn C as well, or any other language that doesn't include "else if" natively.

You overlooked my prick-dom. I was pointing out that in low level lexical analysis/parsing terms 'else if' doesn't exist. It's only:
Code: Select all
// roughly formatted in Bison style BNF
statement   = conditonal
            | for-loop
            | while-loop
            ......
            ;

conditional = IF condition statement
            | IF condition statement ELSE statement
            ;

else if is only possible due to the recursive definition of a conditonal.

So in reality, else-if exists as much as else-while

NOTE: i trolled the shit out of kiyoura with that one day "hey br0, have you heard the newest gcc extension supports the else-while statement" and i linked him to some code that litterally had an else-while statement that compiled and he was amazed, but i couldn't keep the trolling up, so i told a friend of mine and he lol'd him to hell and back <3
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: C,C++,C#

Post by Assassian360 on Mon Oct 18, 2010 10:57 pm
([msg=47768]see Re: C,C++,C#[/msg])

Yeah, that is true in regard to what you said.

thetan wrote:NOTE: i trolled the shit out of kiyoura with that one day "hey br0, have you heard the newest gcc extension supports the else-while statement" and i linked him to some code that litterally had an else-while statement that compiled and he was amazed, but i couldn't keep the trolling up, so i told a friend of mine and he lol'd him to hell and back <3


Haha, I almost feel sorry for that kiyoura guy.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


PreviousNext

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests