473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Should i be following the C++ Standard?

AmberJain
884 Expert 512MB
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
12 1968
boxfish
469 Expert 256MB
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
itsraghz
127 100+
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
oler1s
671 Expert 512MB
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
5,000 Expert 4TB
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
9,065 Expert Mod 8TB
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
884 Expert 512MB
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
9,208 Expert Mod 8TB
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
884 Expert 512MB
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
884 Expert 512MB
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
5,000 Expert 4TB
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
884 Expert 512MB
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
884 Expert 512MB
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

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
10
by: da Vinci | last post by:
Hello. First off, I am not sure of the exact jargon used, so I will ask a question regarding it. Then on to my other question. When you use things like cout and cin from the iostream header...
58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
5
by: spibou | last post by:
I believe I have a good working knowledge of C but now I want to reach a point where I understand all the dirty little details. For example I understand what a "natural" macro will expand to but...
61
by: norb1 | last post by:
After tracking down a bug in my Fortran program, I found that it assumed max(NaN,0.) = 0. This makes no sense, as the outcome of the operation is undefined and should be NaN. max(NaN,0.) = NaN...
8
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - When should I use eval? ----------------------------------------------------------------------- The ` eval() `...
37
by: nolo contendere | last post by:
I've recently begun playing with prototype.js as well as scriptaculous, and found both very helpful in developing web pages with lots of pop. Modal boxes, SlideUp/Down, calendars, etc. I've also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.