C - Getting Familiar with IF and ELSE statements..ect
Josh-
Royal Oak, MI
If your a beginner with C...or C++..and would like something to fiddle around with, try password encryption/protection! It can include most of the basics..that you need, necessary to start programming in C.
This guide will include:
First of all, to start, the top 2 lines of the C file MUST include these lines:
The above two lines are absolutely necessary for the login to work, you must paste them at the top. Next, we are going to pretty much define what the variable will do, and your basic login text, ect.
First, the IF statement will determine if the input (the text you enter as the password) is correct.
-J
(I would recommend C or C++ for dummies, for a starter, I do not remember how much the book costs, but it is VERY useful in beginning at these. You will get much knowledge from this book, and self-tests and quizes to make yourself better with becoming familiar with the language. Well, Good luck all who take this seriously and want to start, I'm always reachable here by PM, or by instant message on AOL. Check my profile for a screename.)
This guide will include:
- IF and ELSE statements..
- a variable..
- and some printf() functions.
First of all, to start, the top 2 lines of the C file MUST include these lines:
#include <stdio.h> #include <stdlib.h>
The above two lines are absolutely necessary for the login to work, you must paste them at the top. Next, we are going to pretty much define what the variable will do, and your basic login text, ect.
void main() { char pas[2]; int password; printf("Login Below\n"); printf("Enter Password:"); gets(pas); password=atoi(pas);Those are the final statements before the IF and ELSE statements start..and here they are.
First, the IF statement will determine if the input (the text you enter as the password) is correct.
if(password==551) { printf("You have sucessfully entered a correct password!.\n\n\nType anything to exit.\n"); printf(""); gets(pas); password=atoi(pas); }If the text entered is "551", then it will print the text:
- You have sucessfully entered a correct password!.
- Type anything to exit.
else { printf("Access denied!\n\n\nType anything to exit.\n"); printf(""); gets(pas); password=atoi(pas); } }This would simply deny you access. The point of this "mini-guide" is too get more people involved with C and C++...It can be used for many of things, for instance this, a mini-login. Hopefully this may help some of you, as some of you may not understand it, it simply tries to recognize a variable, and if it is the variable that it is looking for (551), then some print appears. However, if it is anything other then 551, then you would get something else printed to you. This would be the else statement. After working more with C and C++..you may see more statements. Such as elseif, ect. But, I just thought I would post this, as it is something simple to modify, and learn from. I found that learning something is best from your own tinkering and fiddling with things. Good luck with anyone tries to take up on C or C++..it can be a challenge but very useful in the future.
-J
(I would recommend C or C++ for dummies, for a starter, I do not remember how much the book costs, but it is VERY useful in beginning at these. You will get much knowledge from this book, and self-tests and quizes to make yourself better with becoming familiar with the language. Well, Good luck all who take this seriously and want to start, I'm always reachable here by PM, or by instant message on AOL. Check my profile for a screename.)
Fixed. Thank you for informing me.Jush, when you do stuff like #include <FILE>, either use the < and > or put a space after the < and before the > like < FILE >. That way people see which includes you have. It's just a thing with the forums.
0
Comments
The writing of the files is only the first half, well, more then that, its the hard part. However, there is a second part. For everything that you wish to make, you will have to compile them. Compiling the files (mine are usually .C), they are turned into .EXE (application) files. This way, you will be able to open and use the application/files that you just created. There are probably hundreds of C and C++ compilers, but I would like to take the time to recommend a few.
- www.borland.com -- EXCELLENT compiler
- www.digitalmars.com
- www.comeaucomputing.com/
- www.willus.com/ccomp.shtml
If anyone has any nice compilers to add to the list..please do so.Ah..but this post cannot end without resources. It's always good to reference from something, when something confuses you. Also, resources cant hurt anyways So, without further ado, your resource list:
- www.ddj.com/topics/cpp
- www.cplusplus.com
- www.cyberdiem.com/vin/learn.html
- www.aul.fiu.edu/tech/visualc.html
- www.thefreecountry.com/sourcecode/cpp.shtml
- www.gustavo.net/programming/c.shtml
- MUST LOOK HERE
Hope these also help. Feel free to add whatever you can to my list.www.bloodshed.net
Also, don't use gets() the way you did because you're just asking for buffer overruns. Use cgets() or something else when using C. When using C++ you have a number of options to keep strings save.
Second that.
BTW, why is the character size only 2...? To me that says, one character and a null terminator; but the password is 551, 3 characters long +1 null? Maybe I'm missing something.
Just one of the reasons gets() is bad.
I could probably edit it..and make it more correct, I guess, but I was just using it as an example anyways. I really wish this programming forum was used more often. Don't you guys?
I'll try Dev when I get time tomorrow.
If you guys want to garner more interest in programming (all languages).. then we can expand it out a little
We have a few programmers lurking around (myself included = PHP/SQL) .. from PHP to C/C++ ...
If there isn't one, there might be a C compiler. Most definitely there's an ASM compiler available . . . somewhere.
Just be glad you don't have to do it in assembly. When working on an embedded system with a high level language basically you can program the same way as a more general architecture but you just have to make sure you don't do things that the hardware will die trying to do. Of course you'll also have to worry about PITA things just masks for various control registers but its still a lot easier than assembly. Even advanced math functions *can* be done on old school embedded systems, they just tend to be implemented using hundreds of instructions that take weeks to run a single pass :P (maybe a slight exaggeration).
Here's some info about the GCC and the 68HC1x, basically the C port works awesome (used it many times) but from what they say the C++ port sort of works but mostly doesn't.
http://m68hc11.serveftp.org/m68hc11_inst_ptc.php
J
NS
I used to use it with modifying diablo2 open-realm character files..any of you have any thoughts on it?