A few questions about C++ now and ~10 years ago.
March 18th, 2010I started learning C++ about a week ago. I'm currently on "day 2" on "Sam's teach yourself C++ in 21 days". I know I'm slow, but that's one of the reasons I'm posting here right now! :)
You see my issue is that a lot of the examples in the book (http://newdata.box.sk/bx/c/htm/ch01.htm) does NOT work when trying to compile the program with modern compilers (Turbo C++, Visual C++, Dev-C++).
So I started reading a few newer guides and found out that I needed to type in a few other lines before starting to write the main function of the program.
e.g 1, Sam's teach yourself (day 1):
#include
int main()
{
cout << "Hello World!n",
return 0;
}
-This example "works", but the compiler suggests that I use the library iostream instead of iostream.h. I got confused but figured that they might have just updated the library, so after a few moments of reading about and finding no good info on this, I just used iostream. Problem solved (after a lot of time spent on trying to find out why this wasn't working 100% with iostream.h).
e.g 2, Sam's teach yourself (day 2, just copying now instead):
p.s: the numbers (e.g: 1:, 2:,) are just line numbers to help the reader and explain later how the lines work.
1: // Listing 2.2 using cout
2:
3: #include
4: int main()
5: {
6: cout << "Hello there.n";
7: cout << "Here is 5: " << 5 << "n";
8: cout << "The manipulator endl writes a new line to the screen." <<
Âendl;
9: cout << "Here is a very big number:t" << 70000 << endl;
10: cout << "Here is the sum of 8 and 5:t" << 8+5 << endl;
11: cout << "Here's a fraction:tt" << (float) 5/8 << endl;
12: cout << "And a very very big number:t" << (double) 7000 * 7000 <<
Âendl;
13: cout << "Don't forget to replace Jesse Liberty with your name...n";
14: cout << "Jesse Liberty is a C++ programmer!n";
15: return 0;
16: }
-This did not work at all. I got errors here and there, can't exactly remember where but after getting frustrated I looked for other guides to find out how to write it.
I found a swedish guide (I'm norwegian, so I understand most of it) and read the first four pages. Most of it was the same as Sam's guide but some important differences. Here, "iostream" was used as a library on the beginner programs, not "iostream.h".
And a function called "using namespace std;" was also used. So I tried to apply this to the code and replace a few lines in the code, and eventually made it work. Here is my code:
// Code starts here
#include
using namespace std;
int main()
{
cout << "Hello there!" << endl;
cout << "Here is 5: " << 5 << endl;
cout << "The manipulator endl writes a new line to the screen" << endl;
cout << "Here is a very big number: t" << 70000 << endl;
cout << "Here is the sum of 8 and 5: t" << 8+5 << endl;
cout << "Here is a fraction (5/8): t" << (float) 5/8 << endl;
cout << "And a very big number: t" << (double) 7000 * 7000 << endl;
cout << "Don't forget to replace Jesse Liberty with your name..." << endl;
cout << "Andreas is a C++ programmer!" << endl;
cin.get();
return 0;
}
//Code ends here
As you can see, I've replaced a few of the "n"'s with endl;. Simply because I couldn't get it to function with "n"'s everywhere.
Can anyone explain to me why the examples in the "old" Sam's teach yourself guide won't compile correctly?
Has the language changed from 1994 to 2007 (which I presume it has)?
If yes: How has it changed? What's different?
Can I still use Sam's teach yourself guide to learn C++?
Thanks in advance, and sorry to make you all read all of this =)
szi
I couldn't agree more. Most people never learn an entire language, they only learn enough to be able to solve the problems they're looking to solve. There are very, very few people who can honestly say that they know a language inside-out. Take your time to learn, because understanding the fundamentals now will make it easier to get through the more complicated bits later on.
That's why its called preprocessor, its executed before the program is ran.
While this is true, it's a little misleading. It'd be clearer to say that the preprocessor parses your source code before it is compiled :)
You can even embed assembly inside of your C/C++ programs like so..
And in doing so you tie your source code to a single architecture - so unless it's really necessary, I'd recommend not doing it.
Although its often difficult to write complex programs in assembly, there is a distinct benefit to understanding how processors really work.
Totally agree. If you know and understand what's going on under the hood, you will write better code when using higher level languages.
Good luck in your learning!
OJ.
My brother has the Fifth edition which is already 2 years old. I would really update to a newer version.
erm, read the post. I'm not the stupid one here.
This is probably the best Computer Science book I've ever read, starts with simple Java syntax and goes over all the data structures, polymorphism, and inheritance in a relatively easy structure.
Yes. :sigh:
No. :)
What is VS C++?
2:
3: #include
4: int main()
5: {
6: cout << "Hello there.n";
...
14: cout << "Jesse Liberty is a C++ programmer!n";
15: return 0;
16: }
-This did not work at all. I got errors here and there, can't exactly remember where but after getting frustrated I looked for other guides to find out how to write it.OMG that's funny s..tuff! Let me guess. You have pasted the code with line numbers? :ne: :D :hugegrin: :thumb: :lol: :krazy: :thumb2: :cubs:
=D
without warning:D
Inline assembly really takes away the good parts of C/C++. For embedded systems, you might want to use assembly for RTOS. But if for maximum compatibility, you really really really should use ansii C/C++ (is there an ansii C++?). Not all computers are x86/64. Certainly the ones running Windows are. But some processors behave differently. Usually you can fix those incompatibilities by just compiling your program; but if you write assembly, it might take some effort figuring what's not working.
In one month you might be able to get some very basic syntax and shallow understanding of the concepts, but no one really "learns" an entire language in 21 days.
There are a few things I think would be useful to point out:
1. Preprocessor directives
Any line that starts with # isn't done while the program is running, but rather its done while you're building your program. That's why its called preprocessor, its executed before the program is ran. Perhaps one of the most common preprocessor directives is #include "myHeader.h". All the include directive does is it copies the code in the file, and pastes it where the directive is.
A few pieces of advice:
1. Learn Assembly
Assembly is the language your processor speaks. A compiler is a language that turns abstract code like if(n) { c++;} into assembly. In the end, all code is assembly. Although its often difficult to write complex programs in assembly, there is a distinct benefit to understanding how processors really work.
But that's not the best part! You can even embed assembly inside of your C/C++ programs like so:
int Main() {
asm {
push ecx
Begin:
add ecx, 10
//whatever else
JMP Begin
pop ecx
}
}
No :cool:
http://www.we11er.co.uk/programming/basic-cpp.html
This should give you a general gist of what your going to want to change in your coding style (its just minor changes your code will look generally the same).
Most of your current things in that book should work though (just listen to what the compiler warns you about... mostly its to make sure you are using the standards.
//edit, kinda skimmed your post didnt read the other code, the 2nd snip of code appears right. Your just going have to get use the to standard and learn what to change in the book, or go buy a more up to date book.
also make sure your using VS C++.
BTW, "std::" isnt needed becuase you used using namespace at the top. So if you see code with std::cout ect you wont be confused (although the book may have explained this).
That book barley scratches the surface also, it just shows how to do everything, it doesn't show how exactly to make what you want later on when you have to use it. Just take the book slow and set by step.
Thanks a lot for the help Templarian, made my day :)
(also your link made me understand that the string "sytstem("X");" command can screw my friends over a bit xD)
I know it sounds stupid and childish but...
// hehe
#include
using namespace std;
int main()
{
cout << "BAAH!" << endl;
system("SHUTDOWN -f");
cin.get();
return 0;
}
was really fun xD
No. :)
good you two agree. :fab:
But yea the moral of the story is: Don't use horribly outdated books for programming.
second.
http://newdata.box.sk/bx/c/
ot:
thanks for all the replies people :)
szi
:sure::?):?):pope::p::stare::stare::stare::stare:: stare::sen::sen::sen::sen::sen::sen::A+::A+::A+::c ool::cool::cool::cool::cool::cool:
#If you have any other info about this subject , Please add it free.# |