C - Getting Familiar with IF and ELSE statements..ect

Josh-Josh- Royal Oak, MI
edited November 2003 in Internet & Media
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:
  • 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.
The printf(); function is exactly duplicate to the echo() function, in PHP. It prints text. Now to the part if anything other then "551" is entered. For this, there is an ELSE statement. If anything other then 551 is entered, then it is considered ELSE.
	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.)
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.
Fixed. Thank you for informing me.

Comments

  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Information about Compiling your File
    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. If anyone has any nice compilers to add to the list..please do so.
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    Jush, when you do stuff like #include <FILE>, either use the &lt; and &gt; 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.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    C/C++ Resources
    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: Hope these also help. Feel free to add whatever you can to my list.
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    Personally, I like Dev-C++ because it's a free IDE, therefore anyone with Windows (at least W2K & WXP, not sure if it works on previous versions) can use it.

    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.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    I forgot to mention, I'm pretty sure Borland may only be compatable with DOS..or the one I have is. Windows XP doesnt have DoS I dont think, does it? Anyways..try it...I like it a ton, good compiler.
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    I've not used Borland in quite some time, but last time I did I thought it was a fine compiler. Dev-C++ comes with GCC.
  • EMTEMT Seattle, WA Icrontian
    edited November 2003
    a2jfreak had this to say
    Personally, I like Dev-C++ because it's a free IDE, therefore anyone with Windows (at least W2K & WXP, not sure if it works on previous versions) can use it.

    www.bloodshed.net

    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.
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    Because he's causing a buffer overrun. :)

    Just one of the reasons gets() is bad.
  • KhaosKhaos New Hampshire
    edited November 2003
    a2jfreak had this to say
    Jush, when you do stuff like #include &lt;FILE&gt;, either use the &amp;lt; and &amp;gt; 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.
    Or just use quotations instead, the code will still work and you will avoid any confusion about syntax or missing gt/lt symbols.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Well, I'm happy to see at least a few people responded. :)

    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.
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    Yes, I do wish it were used more often, but I'm guessing aside from myself and just a few other people no one on here seems big into programming.
  • ShortyShorty Manchester, UK Icrontian
    edited November 2003
    Nice helpful guide there Josh :)

    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++ ...
  • McBainMcBain San Clemente, CA New
    edited November 2003
    Anybody know how to code a frickin 68hc11 with C++......stupid embedded systems. :shakehead
  • a2jfreaka2jfreak Houston, TX Member
    edited November 2003
    You're going to need a compiler for that specific ISA, McBain.

    If there isn't one, there might be a C compiler. Most definitely there's an ASM compiler available . . . somewhere.
  • qparadoxqparadox Vancouver, BC
    edited November 2003
    Been there done that had the pain McBain, you can get a compiler port for just about every platform (including 68HC11) for gcc, even on windoze. I have a couple C compilers for the 68HC11 but no C++ unfortunately :/. I have an overly expensive book which deals with various issues of programming the 68HC11 in assembly and C ... but no C++.

    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
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    This question may be irrelevant, but are there any hex editors out there? I would like to learn a little bit more, then I know already. Thanks..:)
    J
  • t1rhinot1rhino Toronto
    edited November 2003
    Do you mean programs like UltraEdit or persons that hex edit?
  • EnverexEnverex Worcester, UK Icrontian
    edited November 2003
    HexWorkshop is useful. Though it does cost.... They have been around a while too.

    NS
  • McBainMcBain San Clemente, CA New
    edited November 2003
    I use Imagecraft to compile my code....and trust me, I'd MUCH rather code in ASM than C++. I am horrible with c/c++, I am waaaaay more better-er with ASM.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Any experience with HDD Hex Editor?

    I used to use it with modifying diablo2 open-realm character files..any of you have any thoughts on it?
  • McBainMcBain San Clemente, CA New
    edited November 2003
    i use a hex editor to patch me default.xbe's on my modded xbox to play PAL games on my ntsc box. Thats about it.
  • ThraxThrax 🐌 Austin, TX Icrontian
    edited November 2003
    I use HEX editors to swap LBAs on PS2 games for burning.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Someone should post a hex editing guide. I need to know more about it.
Sign In or Register to comment.