Current and power!! YEA!

Alright so lets create a quick program for what is know as OHM’s law. 

Now I am going to make this one really basic. Perhaps in the near future I will add more functionality.  Here we go.

#include
#include
using namespace std;

int main ()
{
double resistance; /* Stores resistance entered by the user */
double voltage; /* Stores volatage entered by the user */
double current; /* Stores current after calculation */
double power; /* Stores the power ofter calulation */

cout << "Enter the resistance of the resistor: "; cin >> resistance;
cout << "Enter the voltage across the resistor: "; cin >> voltage;

/* Calculate current */
current = voltage/resistance;

/* Calculate power */
power = voltage * current;

/* Output the area */
cout << "For a resister of " << resistance << "ohms and " << voltage << "volts across it," << endl; cout << setprecision(3); /* Sets my precision in decimal places */ cout << fixed; /* This makes it so that my output is not in scientific notation */ cout << "the current is " << current << "amps and the power is " << power << "watts." << endl; system ("PAUSE"); /* Normally not used, for display purposes only */ return (0); }

 

Ok so this a very basic program here. It does what I want it to do. I will add more functionality later so that it can make some kind of decisions later.

You know...like a smart program. One of the first of it's kind lol. Well there you have it. Take that!

The Circle program!

Ok so I had some buddies that had some work and I was like…well….homie lol…let me show you how it is done in C++.

So then I decided to program the program. Now here it is in C++.

#include
#include
using namespace std;

int main ()
{
double radius; /* Stores radius entered by the user */
double area; /* Stores area after the calculation */
double circumference; /* Stores circumference of the inputted radius */
double pi = 3.14159; /* Creates a variable and assigns the value of pi */

cout << "Enter the radius of the circle: "; cin >> radius;

/* Calculate area as PI times r-squared */
area = pi * radius * radius;

/* Calculate the circumference */
circumference = 2 * pi * radius;

/* Output the area */
cout << setprecision(2); /* Sets my precision in decimal places */ cout << fixed; /* This makes it so that my output is not in scientific notation */ cout << "The area of a circle with radius " << radius << " is " << area << "." << endl; cout << "The circumference of this circle " << " is " << circumference << "." << endl; system ("PAUSE"); /* Normally not used, for display purposes only */ return (0); }

Remember that this is in C++, of course.  Here is a screenshot of the output.

1

If and Else!

So I have been out of the game for a little. SO here is something simple yet essential.

#include
using namespace std;

int main ()
{
/* local variable declaration */
int number;
cout << "Input a number: "; cin >> number;
/* check the boolean condition */
if( number < 50 ) { /* if condition is true then print the following */ cout << "That number is less than 50." << endl; } else { /* if condition is false then print the following */ cout << "That number is not less than 50." << endl; } cout << "Value of the number is: " << number << endl; system("pause"); return 0; }

The IF statement checks if the conditions are valid. If it isn't valid then do what's in ELSE.

Simple huh? Yup pretty much. 

OHHH! And just to let you know. Please try to avoid using system("pause") as much as you can. It is a PROBLEM. I only used it to demonstrate the use of the code.

C++ and Databases

Alright now stuff is about to go down. I thought I would like to explain what I am trying to achieve. I am attempting to have a website and a hardware device connected to the same database via a program. I found some things easy and some things on the more complicated side. The easy part was knowing that PHP would be the obvious language for the website, duh lol. The hard part if choosing the right database system and language that both the website and the program can understand in a “simple” fashion. Apart from that I need to find a way to have the hardware be controlled from the program.

Someone is going to say “That’s easy just use a piece of hardware and software that is pre-made.”, that is not what I want. I am building all of my stuff from scratch. That being said here is a program that I have that connects to a database and shows the columns. This is the initial attempt at extracting information from a server onto a program which will eventually use those parameters to control the hardware.

#include
#include
#include

using namespace std;

static char *opt_host_name = "localhost"; /* server host (default=localhost) */
static char *opt_user_name = "root"; /* username (default=login name) */
static char *opt_password = "mysql"; /* password (default=none) */
static unsigned int opt_port_num = 3306; /* port number (use built-in value) */
static char *opt_socket_name = NULL; /* socket name (use built-in value) */
static char *opt_db_name = "test"; /* database name (default=none) */
static unsigned int opt_flags = 0; /* connection flags (none) */

int main (int argc, char *argv[])
{
MYSQL *conn; /* pointer to connection handler */
MYSQL_RES *res; /* holds the result set */
MYSQL_ROW row;

/* initialize connection handler */
conn = mysql_init (NULL);

/* connect to server */
mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
opt_db_name, opt_port_num, opt_socket_name, opt_flags);

/* show tables in the database (test for errors also) */
if(mysql_query(conn, "show columns from users"))
{
cerr << mysql_error(conn) << endl; cout << "Press any key to continue. . . "; cin.get(); exit(1); } /* grab the result */ res = mysql_use_result(conn); cout << "Columns in users\n"; while((row = mysql_fetch_row(res)) != NULL) cout << "\t" << row[0] << endl; /* disconnect from server */ mysql_close (conn); cout << "Press any key to continue . . . "; cin.get(); return 0; } /* end main function */

Ok so this took me a little bit to to whip up. I really tried to stay in the standard namespace and using C++ functions rather than C. Notice I had to get a new library to be able to connect to the MYSQL server. Once I added the library to my compiler and linked it everything worked great. Some of this code was modified from the example of the MYSQL library documentation. 

The Heat Conversion

So here is the second code I probably learned a while ago (Years). My initial code when I first figured it out was to convert fahrenheit to celsius. This was pretty plain so I thought I would add the celsius to fahrenheit too! I know right! Then I polished it up and got the resulting code.

// Convert Temperatures from Celsius to Fahrenheit
// and vice versa.

#include
#include

using namespace std;

int main ()
{
int option;
float ctemp,ftemp;
cout << "1. Celsius to Fahrenheit" << endl; cout << "2. Fahrenheit to Celsius" << endl; cout << "Choose between 1 & 2: "; cin >> option;
if (option==1)
{
cout << "\t Enter temperature in Celsius: "; cin >> ctemp;
ftemp = (1.8*ctemp)+32;
cout << "\t Temperature in Fahrenheit = " << ftemp << endl; } else { cout << "\t Enter temperature in Fahrenheit: "; cin >> ftemp;
ctemp = (ftemp-32)/1.8;
cout << "\t Temperature in Celsius = " << ftemp << endl; } cout << "Press ENTER to continue..." << endl; getch(); return 0; }

So I'm not going to break it down. most of it is pretty obvious and if it isn't this link might help lol.

Thank for reading :)

The Hello World Code

Alright guys so there is always a beginning to learning code. This is probably it, Hello World (HW). Those of you who have wanted to learn know that almost every freaking book and resource usually starts with HW. So let me break this short code down.

-What does it do: Displays the text Hello World

// Hello World (The basics)

#include
using namespace std;

int main()
{
cout <<"Hello World!"; return 0; }

So before I break this down let me explain something. Some features of C++ are part of the language while others are part of a standard library. This standard library is a compliant body of code that comes with your compiler. So when your compiler puts your program together it links it to the standard library. Before you use the library you need to tell your compiler (declare) your going to use them. This is known as a preprocessing directive.

Lines:

1:  This line is a comment.

3:  Brings in the needed libraries for console output.

4: Keeps you from having to type std::cout each time that output is needed.

6: The main function. It is executed first. 

8: A statement that outputs the words "Hello World"

9: The exit status that states the program returned "0" which means it was successful.

You might have looked around and have seen some HW's return void. This is WRONG! Doing this is just like returning just about any random number that was in a given memory location or register on the computer. This is not cool.

So as you see this program is very simple. I'm not trying to teach, so I'm not going to get into extreme detail on every little thing. This concludes my first post of code. 

Oh and I'm liking the syntax highlighter I added  :-P

Hello world!

Well well. Today I have created the mysterious blog website. It is unique to me as I’m no normal and usual blogger. This word has been unfamiliar to me until recent years. Now I hear it all the time and read about it.

This is the start of it and I shall continue to post many many things when I have the time. Hopefully I grow an audience at some point. I know people will get ccurious <- Not a typo, so shhh. ‘Smiley Face’.

There is no smiley faces in the text editor…Maybe I shall change that.