Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] If Statements
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Thuged_Out_G




PostPosted: Tue Mar 23, 2004 5:21 pm   Post subject: [Tutorial] If Statements

all these darn complicated tutorials lol...here is a nice easy one for the newbs(me) lol

here is a simple password program that i think gives a basic understanding of if statements in C++:

code:

#include <iostream>
using namespace std;

char password[9]="password";
char InputPassword[9];

int main()
{
        cout << "Enter the password: ";
        cin >> InputPassword;

        if(InputPassword==password)
                cout << "Access granted";
        if(InputPassword!=password)
                cout << "\nAccess Denied";
        return 0;
       
}


since this is a tutorial for newbs, im going to explain this code line by line.

Quote:

#include <iostream>

this line is a simple include, im not positive as to what 'iostream' is, but the book just told me to put it there Laughing

Quote:

using namespace std;

again, i dont have a clue 8)

Quote:

char password[9]="password";

incase you dont know, this is C++'s way of declaring a string variable. it would be quite similar to 'var password:string' in turing
char tells us that the variable is of char type.
password is the variable name.
[9] is the length of the string.
="password"; initializes the variable to have a value of password.
as far as i know, you cant declare it simply as string password; ...you have to declare it as an array of chars Confused

Quote:
char InputPassword[9];

same as above, declares the variable InputPassword 9 in length.

Quote:
int main()

starts the main function

Quote:

{
cout << "Enter the password: ";


outputs 'Enter Password: ' on the screen. same as put "Enter password: " in turing.

Quote:
cin >> InputPassword;

gets the users input, and stores it in the var InputPassword. same as get InputPassword in turing.

NOW THE IF STATEMENTS!!!

Quote:
if(InputPassword==password)
cout << "Access granted";


this line just checks to see if, InputPassword has the same value as password, if it does, it outputs "Access granted"
would be the same as:
if InputPassword=password then
put "Access Granted"
end if
in turing

Quote:

if(InputPassword!=password)
cout << "\nAccess Denied";


this simply says, if InputPassword is not equal to password, then put "Access denied to the screen on a new line(\n)

in turing it would be
if InputPassword not=password then
put "\nAccess Denied"
end if

Quote:

return 0;

}


return 0; gives control back to the operating system.
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Tue Mar 23, 2004 5:49 pm   Post subject: (No subject)

code:

#include <iostream>

This lets you use the nice functions like cout and cin

code:

using namespace std;

This lets you use cin and cout without the namespace, in other words, like this

code:

std::cout<<"Hello World"<<std::endl;
wtd




PostPosted: Tue Mar 23, 2004 7:34 pm   Post subject: (No subject)

While I admire your desire to demonstrate the basics of programming to people who are new to C++, there are a number of bad habits your code is perpetuating (though I'm sure not intentionally).

code:
using namespace std;


Not horrible, but a bad habit to get into. As Catalyst explained, this brings the entire "std" (standard) namespace into the current namespace. This can be a convenience, but it can be a problem too. You don't know everything that's inside the std namespace, or what those things are all called, so it makes sense to keep them, but keep them separated into their own namespace.

code:
char password[9]="password";
char InputPassword[9];


First let me say that it's extremely bad practice to declare variables in the global namespace. Variables should only be declared inside functions, or in class (or struct) declarations.

Second, while C++ certainly evolved from C, they are entirely different languages. C had no concept of strings, since it's a relatively low-level language. Instead an array of characters (char) is used. The string ends when the first null (zero) byte (character) is encountered.

C++ does have a string type. It's also in the std namespace, and you include it in your program with the following line. It's a much better, and easier alternative to using C character arrays.

code:
#include <string>


code:
std::string password = "password";
std::string input_password = "";


The following is valid C++, but misses just one point.

code:
if(InputPassword==password)
   cout << "Access granted";
if(InputPassword!=password)
   cout << "\nAccess Denied";


C++ includes and "else" keyword which could be used quite effectively here. Basically "else" is what gets evaluated if the first condition is false.

code:
if (input_password == password)
   cout << "Access granted";
else
   cout << "\nAccess Denied";


This not only simplifies your code, but turns these lines of code into one conditional, rather than the two independent conditionals they were before.

Soyour program ends up looking like:

code:
#include <iostream>
#include <string>

int main()
{
   std::string password = "password";
   std::string input_password = "";

   std::cout << "Enter the password: ";
   std::cin >> input_password;

   if (input_password == password)
      std::cout << "Access granted." << std::endl;
   else
      std::cout << "Access denied." << std::endl;

   return 0;
}
Thuged_Out_G




PostPosted: Tue Mar 23, 2004 10:30 pm   Post subject: (No subject)

see, im new to C++. so i didnt know there was a string variable...i was told to u se an array of chars. and i tried it with the else instead of 2 if statements. but for some reason it kept saying Access Denied no matter what you put in. so i just made it 2 if statements lol
wtd




PostPosted: Tue Mar 23, 2004 10:53 pm   Post subject: (No subject)

Thuged_Out_G wrote:
see, im new to C++. so i didnt know there was a string variable...


Well, now you do. Glad to help. Smile

Thuged_Out_G wrote:
i was told to u se an array of chars.


In dealing with some specific libraries, sometimes you do have to use character arrays. However, whoever taught you to use them right off the bat should be dragged into the street and shot. They're doing you a horrible disservice.

Thuged_Out_G wrote:
and i tried it with the else instead of 2 if statements. but for some reason it kept saying Access Denied no matter what you put in. so i just made it 2 if statements lol


I've been there before, so I appreciate where you are, but you have to resist the temptation to make arbitrary changes to get the right output. Yes, in theory input and output are the only things that have to be right, but that's theory and the unfortunate reality of C++ is that the way you get from point A to point B is important.

For instance, in your example, consider if the input password is correct. It prints "Access granted." But what if you also did something to change the input password? Now your second conditional ("if statement") is encountered and password is not equal to the input password, so it also prints out "Access Denied."

Not exactly a good thing. Smile
Thuged_Out_G




PostPosted: Thu Mar 25, 2004 3:45 pm   Post subject: (No subject)

the person i asked about strings, was sort of my last result lol. he hasnt programmed in C for 10 years. and i dont think has ever in C++ lol

is there an elsif statement in C++? what is the syntax for it
jonos




PostPosted: Thu Mar 25, 2004 3:47 pm   Post subject: (No subject)

just:
else if (...)
{
...
}
wtd




PostPosted: Thu Mar 25, 2004 4:59 pm   Post subject: (No subject)

Yes, "else if" is kinda funny in C and C++. In reality there's really only "if" and "else", but it gets interesting when you realize that you can leave out brackets if only one thing follows.

So the following two examples are exactly the same.

code:
std::string foo = "hello";
if (foo == "bar") {
   std::cout << "it's bar" << std::endl;
} else if (foo == "world") {
   std::cout << "it's world" << std::endl;
} else if (foo == "hello") {
   std::cout << "it's hello" << std::endl;
} else {
   std::cout << "I have no idea!" << std::endl;
}


code:
std::string foo = "hello";
if (foo == "bar") {
   std::cout << "it's bar" << std::endl;
} else {
   if (foo == "world") {
      std::cout << "it's world" << std::endl;
   } else {
      if (foo == "hello") {
         std::cout << "it's hello" << std::endl;
      } else {
         std::cout << "I have no idea!" << std::endl;
      }
   }
}
Sponsor
Sponsor
Sponsor
sponsor
d2bb




PostPosted: Sat Oct 13, 2007 2:07 pm   Post subject: Re: [Tutorial] If Statements

thx to the first 3 tutorials i learned enough to make simple programs Smile

thx a lot for your time.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: