szianci
April 28th, 2007, 12:04 PM
Hello there. Most of you don't know who I am, but just to introduce my self:
I 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 <iostream.h>
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 <iostream.h>
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:\t\t" << (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 <iostream>
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 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 <iostream.h>
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 <iostream.h>
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:\t\t" << (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 <iostream>
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