Blog: Keogh Code - By my calculations...
Here's the good news: over the weekend, I managed to create my first little calculator. Granted, it was quite primitive indeed, but it's still a working app.
Siobhan Keogh | Tuesday, April 24 2012 | 3 CommentsHere's the good news: over the weekend, I managed to create my first little calculator. Granted, it was quite primitive indeed, but it's still a working app.
The bad news? Well, I've discovered that the way I learn best is completely at odds with the way the book I'm using teaches (that's Sams Teach Yourself C++ in One Hour a Day, for those of you who didn't read my first blog).
The book likes to dedicate long parts of the chapter to explaining how the code works and what it does before you actually sit down to write any code. Don't get me wrong - it makes sense to me to understand what something does before you actually do it. The problem is that I learn by doing, so when I come across chunks of text about code I haven't learned about yet - this week it was using functions other than main() - before I actually write any code, I panic. From there, I do one of two things: A) Fail to understand, get frustrated and give up, or B) Write the code first, then go through the code line by line working out what each piece of code does.
Obviously, option B is much more helpful to me. When I go back and read the explanations after I actually write the code, everything falls into place and makes a whole lot more sense. I realise the reverse may be true for some people.
And my understanding has actually been pretty good, so far. Not only have I been writing the code in the book and getting everything to compile and work with relative ease, but I've been altering the programs the book has given me to actually improve on them. For example, the calculator in the book was very basic and only designed to add two numbers together, in the command prompt. I managed to alter this app in two ways: by making the program's instructions clearer for the user to understand, and by adding a 'subtract' function.
Unfortunately, I haven't yet gotten to the part in my book where you start making rules like "if x is true, run this part of code; if y is true, run this other part". At the moment, code instead runs in the order it's written in. Because the code for addition comes first, when you run the program you have to unnecessarily add something before you can subtract anything. It's a pain in the butt, but I was still pretty happy with myself for managing to create a calculator with two functions.
For coders who have a little bit of understanding of C++ and are interested in how I altered the code and what it did, here's what I was supposed to write, and the output from it.
Code:
1. #include <iostream>
2. int Add (int first, int second)
3. {
4. std::cout << "Add()received" << first << " and " << second << "\n";
5. return (first + second);
6. }
7.
8. int main()
9. {
10. using std::cout;
11. using std::cin;
12.
13.
14. cout << "I'm in main()!\n";
15. int a, b, c;
16. cout << "Enter two numbers: ";
17. cin >> a;
18. cin >> b;
19. cout << "\nCalling Add()\n";
20. c=Add(a,b);
21. cout << "\nBack in main().\n";
22. cout << "c was set to " << c;
23. cout << "\nExiting...\n\n";
24. return 0;
25. }
What was printed on the screen:
(You'll see I had to enter two numbers, separated by a space, then press enter. For this example, I'm using 4 and 9, which I physically had to type into the program. - Ed)
I thought this code was pretty messy, mostly in its wording. It never actually tells you that your two numbers have to be separated by a space. That's deliberate, of course - it's just trying to teach you to understand that the main() function can point to other functions, in this example Add() and then return to the main() function.
So I made things a little harder for myself and made it into what I thought was a more user-friendly calculator app. I removed all the printed text that explained which function was being used at which point, and coded in that subtract functionality I was talking about before:
Code:
1.#include <iostream>
2. int Add(int first, int second)
3. {
4. return (first + second);
5. }
6.
7. int Subtract(int one, int two)
8. {
9. return (one - two);
10. }
11.
12. int main()
13. {
14. using std::cout;
15. using std::cin;
16.
17. int a, b, c;
18. cout << "Add: Type two numbers separated by a space, then press Enter: ";
19. cin >> a;
20. cin >> b;
21. c=Add(a,b);
22. cout << "\n" << a << " + " << b << " = " << c << "\n";
23. cout << "\nSubtract: Type two numbers separated by a space, then press Enter: ";
24. cin >> a;
25. cin >> b;
26. c=Subtract(a,b);
27. cout << "\n" << a << " - " << b << " = " << c << "\n";
28. cout << "\nPress any letter and then Enter to exit...\n\n";
29.
30. char response;
31. cin >> response;
32.
33. return 0;
34.
35. }
What was printed on the screen:
Eventually, I want to return to this program and alter it so that it includes multiplication and division. That part I could code right now and I've been too lazy to do it. But I also want to come back to it when I know how to establish those 'if' rules I was talking about before. At that point, when you open the program I want it to print something like...
To add, press A, then Enter
To subtract, press S, then Enter
To multiply, press M, then Enter
To divide, press D, then Enter
My understanding is that it's still a pretty simple thing to code; I'm just not quite there yet. But I'm looking forward to it! Maybe once I get this little app working the way I want it to, I'll upload it for all to try out and see what you can do by dedicating only a couple of hours a week to learning. I'm only in week two, but for now I feel like I'm improving fast.
Windows vs. iOS vs. Android:How to choose the best tablet for you
101 great websites:
You haven't heard of yet
DIY desktops:
We ask the pros for building tips
Hot Products || PC World editors iPhone 4S launch pics and unboxing
The iPhone 4S launched at midnight through both Vodafone and Telecom. ... READ MORE
Tux Love || Geoff Palmer Google : Starting to be evil?
Google recently deleted AdBlock Plus from its Android Play Store. This is ... READ MORE
Tech Guy || Juha Saarinen Small balls of solder
The idea that desktops might change forever is enough to send geeks into a ... READ MORE
In a Nutshell || Zara Baxter Logging, not login
At an event in Singapore yesterday, Seamus Byrne, the editor of CNet ... READ MORE
Harley O'Gyver || Harley Ogier Pay for internet by-device? Not on my watch.
So as those of you who follow my twitterstream will know, I'm currently in ... READ MORE
The Arcade || PC World editors New Year, new games
You'er going to laugh. Or at the very least, you're going to scoff and ... READ MORE
Dumb Terminal Live! || PC World editors New Zealand memes: We think we're real funny
We New Zealanders love the internet, and we have a pretty good sense of ... READ MORE





The syntax is a lot clearer to understand
Posted by Anonymous at 19:10:31 on May 7, 2012
Flag abuse
Posted by Harley Ogier, PCW at 13:08:38 on April 25, 2012
Flag abuse
Posted by Thi at 12:56:04 on April 24, 2012
Flag abuse