By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,952 Members | 1,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

Should i be following the C++ Standard?

AmberJain
Expert 100+
P: 884
Hello,

I am presently referring to C++ primer (by Lippman, Lajoie, Moo) for C++ programming.

Now for a simple addition program, book "C++ primer" documents following progam (which is compiled by MinGW compiler of bloodshed Dev C++ IDE:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std ::  cout << "Enter two numbers : " << std :: endl;
  6.  
  7. int v1, v2;
  8.  
  9.  
  10. std :: cin >> v1 >> v2 ;
  11. std :: cout << "The sum of " << v1 << " and " << v2 << " is " << v1+v2 << std :: endl;
  12.  
  13. return(0);
  14.  
  15. }
  16.  
But in my college my lecturers refer using Borland Turbo C++ 3.0 compiler/IDE. The above code doesnot compiles in Turbo C++. Instead turbo C++ (and my teacher too) wants me to write something like:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. int main()
  4. {
  5. cout << "Enter two numbers : ";
  6.  
  7. int v1, v2;
  8.  
  9. cin >> v1 >> v2 ;
  10. cout << "The sum of " << v1 << " and " << v2 << " is " << v1+v2;
  11.  
  12. return(0);
  13.  
  14. }
  15.  
Now I am in dilemma. What should I follow- The standard (as in C++ primer) or the one required by Turbo C++ (and my lecturer too)?

Thanks.......
AmbrNewlearner
Nov 7 '08 #1
Share this Question
Share on Google+
12 Replies


boxfish
Expert 100+
P: 469
I like the fact that the second style does not have std:: everywhere. You can omit std:: if you write the line
using namespace std;
after all the includes of header files.
Nov 7 '08 #2

100+
P: 127
I like the fact that the second style does not have std:: everywhere. You can omit std:: if you write the line
using namespace std;
after all the includes of header files.
"using namespace std;" -- is the standard and suggested way of doing I reckon and have heard.
Nov 7 '08 #3

Expert 100+
P: 671
There shouldn’t be a question of which is correct. What C++ Primer shows you is the correct code. Obviously, you want to use the correct code.

The real question is, are you allowed to? Clearly, your lecturer has not realized the changes that have occurred in over a decade worth. Turbo C++ was released in 1991, and C++ has changed. Talk with him about the issue. It’s up to your lecturer what he allows you.
Nov 7 '08 #4

RedSon
Expert 5K+
P: 5,000
I think you should be more explicit in your coding until you are comfortable with C++ and using namespaces.

That means that you would specify std::

If you want to be lazy (and some would say sloppy) than you can leave it out. Once you get into serious projects you can be lazy since you will have a better understanding of scope and what namespaces and libraries you are using.
Nov 7 '08 #5

Banfa
Expert Mod 5K+
P: 8,916
Borland Turbo C++ 3.0 is a very old compiler and I think produced before C++ was standardised in 1998.

You have a bit of a dilemma, you could try to get your collage to update to a compiler that isn't around 15 years, old GCC for instance, but it is possible you wont be able to do this.

If you can't get the collage to update to a newer compiler then you are going to have to write code that is compatible with Borland Turbo C++ 3.0, unfortunately this means non-standard compliant code.

If you end up doing this then remain aware that that is what you are doing and for the sake of learning the standard repeat all your assignments (or at least the small ones) on your more standard compliant compiler at home (note even Dev-C++ is getting a bit long in the tooth now).
Nov 7 '08 #6

AmberJain
Expert 100+
P: 884
Hello,

There shouldn’t be a question of which is correct. What C++ Primer shows you is the correct code. Obviously, you want to use the correct code.

The real question is, are you allowed to? Clearly, your lecturer has not realized the changes that have occurred in over a decade worth. Turbo C++ was released in 1991, and C++ has changed. Talk with him about the issue. It’s up to your lecturer what he allows you.
You got the nerve of my problem. Personally, I want to use what "C++ primer" suggests but I fear that it might be my teacher who will not allow me to use what C++ primer suggests.

If you remember then I had posted a similar thread in the past when I was learning C programming. And you advised me in the same manner you are doing it now. I managed to deal with C at that time (learning what standard suggested) but now as C++ is a vast language, I simply cannot learn what C++ primer suggest and write in exam (if I ever want to get marks in my college exam).

I know that there is no way I can convince my teacher. If I try to tell him then he will think that I am trying to be oversmart (you know what I mean).

What do you now suggest?

Thanks for your advice.......
AmbrNewlearner
Nov 7 '08 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Your Lippman example is correct.

Your teacher is still in the last century. iostream.h is pre-ANSI C++ and went obsolete when ANSI C++ came into existence in Sep 1998.

As a general rule, if you have a C++ textbook published in 1998 or earlier, get rid of it.

As far as Lippman, goes std::cout is preferred. You could also:

Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2.  
  3. cout << etc...
  4.  
This is not quite as good since the cout is not precisely identified. You could get a compile error:
Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2.  
  3. void MyFunction()
  4. {
  5.     int cout;
  6.  
  7.     cout << cout;
  8. }
  9.  
Here the local cout is used and it is an int. Following Lippman's example:
Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2.  
  3. void MyFunction()
  4. {
  5.     int cout;
  6.  
  7.     std::cout << cout;
  8. }
  9.  
This will compile and run perfectly. I am not suggesting you code like this but the std::cout is safer than using std::cout.

The least favored thing is using namespace std; since this is the least specific way to tell the compiler where cout is declared.
Nov 7 '08 #8

AmberJain
Expert 100+
P: 884
Borland Turbo C++ 3.0 is a very old compiler and I think produced before C++ was standardised in 1998.
It's bad that many students around here in my country use this compiler as lecturers simply compell studens to use them. I fell really sad sometimes about tis attitude:(

You have a bit of a dilemma, you could try to get your collage to update to a compiler that isn't around 15 years, old GCC for instance, but it is possible you wont be able to do this.
As I posted before (and as you foresee), It is impossible to make my college (and teachers) agree to use newer compilers.

If you can't get the collage to update to a newer compiler then you are going to have to write code that is compatible with Borland Turbo C++ 3.0, unfortunately this means non-standard compliant code.
If I follow this path, it means that I am going to stuck to non standard code for a long time. I tried this approach when I was learning C but then I got too much used to non standard code. I had to literally stop using C for 2 months and then reread it (the standard one) so that I get what I am expected to know by real programmers.

If you end up doing this then remain aware that that is what you are doing and for the sake of learning the standard repeat all your assignments (or at least the small ones) on your more standard compliant compiler at home
Well, if I get no other solution, then I will have to stick to Turbo C++ compliant code :( and then relearn standard code at a later time.

(note even Dev-C++ is getting a bit long in the tooth now).
Dont worry.....I am using Dev C++ only because my linux crashed yesterday :(. I am trying to make my linux up and running soon. I use dev c++ only ocassionally when my linux crashes and I have to use windows. I would probably be back to gcc on linux tomorrow.

Thanks for your advice....
AmbrNewlearner
Nov 7 '08 #9

AmberJain
Expert 100+
P: 884
Your Lippman example is correct.

Your teacher is still in the last century. iostream.h is pre-ANSI C++ and went obsolete when ANSI C++ came into existence in Sep 1998.
.................................................. ...................................run perfectly. I am not suggesting you code like this but the std::cout is safer than using std::cout.

The least favored thing is using namespace std; since this is the least specific way to tell the compiler where cout is declared.
Thanks for your advice.........
AmbrNewlearner
Nov 7 '08 #10

RedSon
Expert 5K+
P: 5,000
It will benefit you more if you code to the standard. If your professor requires you to code to an obsolete compiler then you should remind him that what you are doing is standards compliant and works with freely available and up-to-date compilers such as GCC.

It is very dangerous to learn bad habits now in your formative years since they will be tough to unlearn later.

When you submit your projects, submit them in binary form compiled with gcc. You can provide the source and the executable and show that it works.
Nov 7 '08 #11

AmberJain
Expert 100+
P: 884
It will benefit you more if you code to the standard. If your professor requires you to code to an obsolete compiler then you should remind him that what you are doing is standards compliant and works with freely available and up-to-date compilers such as GCC.

It is very dangerous to learn bad habits now in your formative years since they will be tough to unlearn later.

When you submit your projects, submit them in binary form compiled with gcc. You can provide the source and the executable and show that it works.
Ok....I will try to see if this helps......
BTW, thanks for your advice everyone.

AmbrNewlearner
Nov 8 '08 #12

AmberJain
Expert 100+
P: 884
Hello,
@Banfa
Update: My semester results are out and I have passed in all subjects:) (including C++). I followed the approach of reading the minimum amount of C++ required for me to pass in my exams. Now that I had passed my exams, I am going to re-learn the C++ as specified by standard. I am starting with C++ primer.


Thanks for your valuable advice....
AmbrNewlearner
Jan 13 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.