Thread: Advice

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    43

    Advice

    Code:
    #include <iostream.h>			
    int main()				
    {
    cout<<"enter the year 20";
    int year;
    cin>> year;
    int z;
    z=3;
    
    int  age;
    cout<<"enter your age ";
    cin>> age;
    
    int sum;
    sum = year + age - z;
    
    cin.get();
    cout<<"your age is "<<sum<<" in 20"<<year<<endl;
    
    cin.get();
    return 0;
    }
    /*********
    *prouced  *
    *by           *
    *Charlie    *
    *Helyes     *
    *********/


    That is the most complex program i have wrote. I learnt html,
    and c++ was my next step, i started learning on thursday and that is the stage i have got to. Do you have any usefull tips for people just starting To learn c++?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes...too much in fact...go read some tutorials and books, but first, we like to point out that you aren't up to the standards:
    Code:
    #include <iostream>
    using namespace std;			
    int main()				
    {
    cout<<"enter the year 20";
    int year;
    cin>> year;
    int z;
    z=3;
    
    int  age;
    cout<<"enter your age ";
    cin>> age;
    
    int sum;
    sum = year + age - z;
    
    cin.get();
    cout<<"your age is "<<sum<<" in 20"<<year<<endl;
    
    cin.get();
    return 0;
    }
    Of course that will only work if your compiler is standards compliant. Good luck!
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    It seems as though you're learning from a slightly out of date source, as you're using the older and non-standard <iostream.h> header rather than <iostream>. If possible, get hold of something more up to date that discusses standard C++.

    Pet thing of mine is that right from day one people should be encouraged to use meaningful names for variables and functions, to make code as self-documenting as possible, negating the need for comments much of the time.

    You'll need to invest in 2 or 3 books at least along the way, probably more, but one good beginners book should get you started.

    Try to get to grips with what is and what isn't standard C++ so that you appreciate what is portable and what isn't. You need to use non-portable stuff in almost any significant program, but wherever possible your starting point ought to be to use portable code whenever you can.

    Learn to use the documentation that comes with your particular compiler. The C++ standard leaves many things implementation-defined and your compiler will fill in the details.

    Learn how to use the C++ standard library as soon as possible, such as vectors, the string class, algorithms etc. At times you may choose not to use them, but you can do so based on knowledge rather than ignorance.

    HTH

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    thanx for the advice on using <iostream> not <iostream.h>
    i agree on thats the best way of learning because i made up that program on figuring out your age and im comfortable with variables.
    what does the using namespace std; do?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    C++ allows you to split off parts of your code into namespaces, which contain functions, variables, and the like. "std" is one such set of functions that come in useful on a day to day basis.

    "using namespace X" simply tells the compiler to bring namespace X's function and variables in reach of your program. Without it, you would have to prefix every variable and function you want to use in that namespace with it's name and a double colon. Eg. "std::vector"

    Namespaces come in useful in large projects as you can group a collection of classes which details with a certain part of the project into a single namespace, so it does not pollute the global environment. For example, if you were doing a routing application, which accepted data from a GPS module, you may want all the comms classes to do with the gps in there own namespace, so that they would only be visible to the parts of the program that actually needed them.

    If you have used C before, then a namespace can be compared to using the "static" keyword on a global variable or function in that language, but to a much more powerful degree.
    Last edited by _Elixia_; 11-08-2003 at 05:56 PM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    One thing that I like to do is to keep the integers together (for local variables) under
    Code:
    int main ()
    {
    int year;
    int age;
    etc
    But that I suppose is entirely up to you when you are designing code. You will have to remember local and global varables.

    And another thing that I sometimes do - which for small programs it is ok - maybe on a larger scale it is unwise - but that is another discussion. What I like to do is whenever I don't need to create an integer then I won't.

    I noticed you place int z; then z=3

    If I was writing the same thing I would take this sum = year + age - z;

    and make it sum = year + age - 3;

    Again all up to designer and program size. Totally up to you - My way is probably the worst way - but until I am told otherwise I will use the same methods..

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As to "using namespace std;", it's a good thing to do as a beginner, but it's really not a very good idea.

    Really, the main reason people used to do this was to maintain backwards compatibility with code that predated namespaces. "using namespace std;" basically undoes the effects of putting everything in a namespace to begin with.

    The rationale for this was that you don't want name collisions. Example: there is a C++ standard class called vector, it is something like an array that you can resize, etc. But mathematics libraries also want to use classes called vector. And you can see that a lot of common things, like sort(), for example, might be chosen by more than one person.

    On my own compiler, in addition to the standard libraries, I use about 6 or 7 different libraries from other people. Namespaces help for 2 reasons -- they prevent naming conflicts, and they tell ME, when I reread the code, which libraries I was using.

    That said, I do NOT like "using namespace xxx;" at all. I prefer to simply do a little more typing -- cout becomes std::cout, endl is std::endl, etc.

    So, if I saw this line:

    boost::shared_ptr<std::string> myStringPtr;

    it is IMMEDIATELY clear to me that I am using a string class (from the standard C++ library) and a shared pointer class from the boost library.

    I really like namespaces because of this -- it makes the code more readable, because I can come back to something I haven't looked at in a long time, and I can understand what I did better because I can see what libraries I was using. E.g. a mtl:: (matrix template library) would be a dead giveaway I was doing some kind of matrix or tensor mathematics.

    So if I was writing that code, I would do:

    Code:
    #include <iostream>
    			
    int main()				
    {
         std::cout<<"enter the year 20";
         int year;
         std::cin>> year;
         int z;
         z=3;
    
         int  age;
         std::cout<<"enter your age ";
         std::cin>> age;
    
         int sum;
         sum = year + age - z;
    
         std::cin.get();
         std::cout<<"your age is "<<sum<<" in 20"<<year<<std::endl;
    
         std::cin.get();
         return 0;
    }
    It's a little more typing, but I think it makes it much more clear where all the functions you are calling live.
    Last edited by Cat; 11-08-2003 at 11:46 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    you problably have the tutorials off here outdated....... i know from experience.

    for books, try 'SAMS teach yourself C++ in 21 Days' by Jesse Liberty. About 70 Aussie $$, but very good for self learning. Very up to date.

    the web is something like www.samspublishing.com
    Ph33r the sphericalCUBE

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Cat
    Code:
    #include <iostream>
    			
    int main()				
    {
         std::cout<<"enter the year 20";
         int year;
         std::cin>> year;
         int z;
         z=3;
    
         int  age;
         std::cout<<"enter your age ";
         std::cin>> age;
    
         int sum;
         sum = year + age - z;
    
         std::cin.get();
         std::cout<<"your age is "<<sum<<" in 20"<<year<<std::endl;
    
         std::cin.get();
         return 0;
    }
    It's a little more typing, but I think it makes it much more clear where all the functions you are calling live.
    eh, I don't like that... it looks to messy... I like doing it this way:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    			
    int main()				
    {
         cout<<"enter the year 20";
         int year;
         cin>> year;
         int z;
         z=3;
    
         int  age;
         cout<<"enter your age ";
         cin>> age;
    
         int sum;
         sum = year + age - z;
    
         cin.get();
         cout<<"your age is "<<sum<<" in 20"<<year<<endl;
    
         cin.get();
         return 0;
    }
    IMO, it's more readable that way... actually, If I was writing the code, I would do it differently, but i'm not going to post my version because this post is long enough already...
    Last edited by major_small; 11-10-2003 at 10:31 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    32
    HTML is so easy.
    VB is easy.
    Java - not easy because too many features.
    C - not easy and boring.
    C++ is the toughest of all for me.

    I started to learn C++ on my own a few years ago and until now I am still reading my Using C++ published by Que, C++: Software Engineering Approach, C++ Programming Language by Bjaerne Stroustrup, C++ How to Program , and C How to Program by Deitel & Deitel.

    I am still struggling with control structures and pointers due to lack of practices. Once in a while, I will try some codes from the books. I seldom read my books. There is no progress because I hardly try the codes given in the books.

    Then, I heard that I need to learn Data Structures in order to be good in C++. So I bought a book on Data Structures Using C++. Then, I discovered that I need to know basic stuff like array and pointers in order to master data structures and algorithm. Understanding pointers is rocket science to me.

    Linked list, stacks, queues, searching and sorting give me headache.

    The programs created from the examples given in the books are as boring as DOS. So, I decided to learn Visual C++ and MFC in order to create Windows application.

    C++ is tough. I don't think I can master it without going to college.

    I can't afford to go to college. So if I have any questions, I think I will come to this forum and ask the experts in this great forum.

    Last edited by javacvb; 11-10-2003 at 11:15 AM.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    where do u live?? college is free in england

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    i repeat code untill i know it this last week i have been studying it all day actually typing the code urself and changing stuff to see how it works is the best way to learn

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    32
    Singapore.

    Where do you live?
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  14. #14
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    england colledge is free here

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    32
    Originally posted by makveli
    england colledge is free here
    So, you decided to be in IT line.

    Good luck to you with your programming.

    If there is any language that I wanted to master it has to be C++ the most powerful language in the world.

    I haven't master C++ yet and they already come out with C#.

    I haven't finished my introduction to Visual C++ book and they have replaced Visual C++ with .Net. So, I decided to buy Visual Studio.Net Enterprise Edition to replace my Visual Studio 6 Enterprise Edition. I also bought another book by Deitel & Deitel - Visual C++.Net - A Managed Code Approach for Experienced Programmers. I bought it last month and until now haven't touched the book.

    If you are in your teen, I advise you not to waste time on playing games like what I did in the past. Use your time to master C++ and one day you will be able to create games and sell it to poor people like me.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. Resume advice
    By Zughiaq in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-15-2005, 02:16 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  5. Partitioning advice?
    By mart_man00 in forum Tech Board
    Replies: 6
    Last Post: 01-10-2003, 10:53 PM