Computer Science Canada N00B Questions. |
Author: | 0x8000 [ Thu Jun 15, 2006 7:37 pm ] | ||
Post subject: | N00B Questions. | ||
Hey, I just started to program today, I got bored and started reading a dumb book I found in my basement for learning C++. I started experimenting with it and it's not too hard, I download Dev C++ and am using it to compile my programs. So far I've learned only a bit, but I've only been studieing it for about a little over an hour. Anyway I just decided to make my own simple math program, take any two numbers and add, subtract, multiple, or divid them. I have it working but I don't know what the code is to keep the command window open. I'd like to have it close by maybe pressing a hotkey, like Esc or Del... Heres what I got:
So if I can get help answering that question, and if anyone has any pointers whatsoever.. thanks.[/code] |
Author: | Martin [ Thu Jun 15, 2006 8:57 pm ] | ||||||||||||||||||
Post subject: | |||||||||||||||||||
To make it stop, #include<cstdlib> and put the line getch(); at the bottom of your code (on the line above the return 0; ) to make it wait for a keypress.
Can be simplified to:
You should use endl instead of "\n" so that if you compile your code on a system where '\n' isn't the endline character, it will still end the line (endl is set to whatever the endline character is on the current system). Instead of #include<iostream.h>, the standard is to use #include<iostream> This puts cout inside the 'namespace' called std (short for standard). A namespace is a collection of functions. They use them because C++'s library is so big, there are going to be different functions with the same name. So, instead of saying cout, you have to specify which cout you want to use. You do this by saying 'I want to use the standard (std's) cout', as follows:
That said, you can also tell the compiler that, whenever you don't specify which cout you're using, assume that you mean the standard one. To do this, give it the line:
below the include statements. The following two programs are equivalent:
You could also put the entire program in a loop, allowing the user to check multiple things. Play with this code:
In your if statement, you're making too many checks.
Instead of multiple if statements, use an if - else if - else structure.
Finally, for if statements, you can also use a switch/case structure. I'll leave this as an exercise for you to figure out. Good job using enums by the way! |
Author: | 0x8000 [ Thu Jun 15, 2006 9:49 pm ] |
Post subject: | |
Thanks a bunch Martin. Great information that I will implement into my little program. I'm really liking this programming thing, it's not as hard as I thought it would be, but then again I'm not programming anything complicated. Only a simple math program. Anyways thanks again, I'll be sure to hang around the forums, seems like there are lots of intelligent people here. Got work tomorrow, it's almost eleven, so I'll be heading off now. ![]() |
Author: | Martin [ Thu Jun 15, 2006 10:52 pm ] |
Post subject: | |
Anytime! One of the best things that you can do when learning a new programming language (or anything at all really) is to take something simple and polish it to perfection. |
Author: | 0x8000 [ Fri Jun 16, 2006 9:18 pm ] | ||
Post subject: | |||
I fixed it all up, is there anything else I can add/remove or change?
I did notice that if I were to say chose division and then give a number like 4444444444, it would make the program go completely crazy repeating the text of the quesiton over and over forever... |
Author: | wtd [ Fri Jun 16, 2006 9:59 pm ] |
Post subject: | |
"iostream.h"... Check Martin's post again. ![]() |
Author: | wtd [ Fri Jun 16, 2006 10:07 pm ] |
Post subject: | |
Your indenting could also use work. A logic error: Your while loop tests to see if subject != 0. The problem is that subject has not at that point been initialized. At this point it contains some random value. You may either wish to have subject initialized, or use a do while loop to ensure that that the loop runs at least once. |
Author: | Martin [ Fri Jun 16, 2006 11:23 pm ] | ||
Post subject: | |||
A note on indentation: My personal preference, and that of a lot of C++ programmers is to follow the simple style that anything in the braces { } gets indented a number of spaces. For example
#include<iostream.h> versus #include<iostream> : iostream.h is the old version, iostream is the new one. The new one is nicer, you should use that. using namespace std; Your code is syntactically fine, but there is redundency in it. The using namespace std; line is telling C++ that 'If you see a function and you don't know what it is, it's coming from the standard namespace - namespace std' So your code comes up to a lonely cout function, says 'wait a sec, he told me that it'd be in the standard namespace, which is defined by iostream.' It checks there, and you're off. So you're telling it to check std as a default, and then you're specifically checking std when coding. So, in summary - either get rid of the using namespace std; line, or get rid of the std::'s. I'd go with the latter. A thing to note: cin is also part of the standard namespace. If you were to get rid of the using namespace std; line, you'd need to say std::cin instead of just cin. For now though, I just wouldn't worry about it. Keep the using namespace std; line in, forget about the std:: things and carry on. Namespaces are something that you'll come to explore in depth later in learning, and it really won't make a ton of sense until then anyway. So much fuss over a single line of code eh? |