Baking Your First Cake

So I finally tested my knowledge up to now with my very own, simplest of simple programs. I wanted to test to make sure I understood the C++ methods of initializing variables, performing simple math, and displaying answers. Therefore, I figured a simple, non-interative equation would be the way to do it. To begin, the program has all variables stated within the program so the only way to get a different answer is to recompiling after changing the variables. The initial program looked like this.

1 // Simple program to calculate tension
2
3 #include
4 #include
5 #include
6 using namespace std;
7
8 int m1(1),m2(1);
9 double g(9.81), T;
10
11 int main()
12 {
13 numerator = 2*m1*m2;
14 denominator = m1+m2;
15 T = (numerator/denominator)*g;
16
17 cout<<T<<endl;
18
19 return 0;
20 }

So again my first problem arises from the Standard Library. I’m not even really sure where I thought I saw the header file “ifstream”, but it’s not a real thing as told to me like this:

tylerdurden@codebase:~/Documents/Cprogs/source$ g++ tension.cpp
tension.cpp:3:19: fatal error: ifstream: No such file or directory
#include
^
compilation terminated.

Just to be careful I only commented the appropriate line out instead of deleting it completely. Ya know, just to be safe for now. Compile attempt #2. I am confronted with my own absentmindedessness.

tylerdurden@codebase:~/Documents/Cprogs/source$ g++ tension.cpp
tension.cpp: In function ‘int main()’:
tension.cpp:13:2: error: ‘numerator’ was not declared in this scope
numerator = 2*m1*m2;

tension.cpp:14:2: error: ‘denominator’ was not declared in this scope
denomenator = m1+m2;

Oh yea, that’s right! NOTHING goes on the left side of the equals sign that hasn’t been at least declared previously, if not also initiated. I decided to create these “shortcuts” after I had typed all of the declarations I thought I was going to use. Easy fix. As all documentation I have read has told me, I will mention here: Initiation is not completely necessary the way declaration is necessary, but be careful with which variables you leave uninitiated. In the first draft of my simple program the variables m1 and m2 have been initiated because they need values in order for the equation to actually compute. Up to this point it seems like we’re doing alright. So far the the source is looking like:

1 // Simple program to calculate tension
2
3 //#include
4 #include
5 #include
6 using namespace std;
7
8 int m1(42),m2(1);
9 double g(9.81), T, numerator, denominator;
10
11 int main()
12 {
13 numerator = 2*m1*m2;
14 denominator = m1+m2;
15 T = (numerator/denominator)*g;
16
17 cout<<T<<endl;
18
19 return 0;
20 }

And is returning

tylerdurden@codebase:~/Documents/Cprogs/source$ ./a.out
19.1637

It would seem I’m doing fairly well with the basics. So what do I do now? Start messing with the source again, of course. At this stage of the game if I have code that is successfully compiling, then I have new things to be learning. By the time I come back around for another post, I hope to have variables that I can manipulate without re-compiling the source at the very least. Not sure what other kind of tweaks I want to apply just yet. We shall see when I get it posted here.

Or if there are suggestions as to what other kind of “beginner friendly” tweaks and actions I play with, leave them in the comments. Any resources I should check for C++, git, bash, make, or any of the other languages I am or will be working with regularly on this project? Let me know! Any other places I could improve? I love constructive criticism!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s