DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: New to C++

  1. #1
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9

    New to C++

    Hi,

    I'm not sure if this is the correct section to post this, i'm assuming it is ok as it is for C++. I was wondering if I have coded this correctly. I have only been learning for a few days. I have originally been learning AutoIt but wanted to move onto something more of a programming language.

    Anyway, here is my code. I won't be able to reply until I get home from school.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void products();
    void about();
    void where_to();
    void restart();
    
    int main()
    {
    	cout << "########  ########   ######\n";
        cout << "##     ## ##     ## ##    ##\n";
        cout << "##     ## ##     ## ##\n";
        cout << "##     ## ########   ######\n";
        cout << "##     ## ##              ##\n";
        cout << "##     ## ##        ##    ##\n";
        cout << "########  ##         ######\n";
        cout << "   Dead Pirates Society\n\n";
        cout << "Welcome to the DPS database!\n";
        cout << "Please enter a number from below for more information:\n";
        cout << "1. Products\n";
        cout << "2. Who are we?\n";
        cout << "3. Where?\n";
        string command;
        cin >> command;
    
        if (command == "1")
            products();
    	else if (command == "2")
            about();
    	else if (command == "3")
            where_to();
    }
    void products()
    {
        cout << "**********   *************\n";
        cout << "  Drinks       Chocolates\n";
        cout << "**********   *************\n";
        cout << "Coke		Mars\n";
        cout << "Solo		Bounty\n";
        cout << "Sunkist         Snickers\n";
        cout << "Lemonade	Cherry Ripe\n";
        cout << "Pepsi		Twirl\n";
        cout << "Fanta		Fling\n";
        cout << "$0.80		$1.20\n\n";
        restart();
    }
    void about()
    {
        cout << "We are a group that are raising money for\n";
        cout << "the year 12 common room. We aim to improve\n";
        cout << "the current state it is in to provide a better\n";
        cout << "environment for students to relax and study in!\n\n";
        restart();
    }
    void where_to()
    {
        cout << "We are located in the German Room (Room 21)\n";
        cout << "at lunch time. We can do deliverys and\n";
        cout << "special orders if notified a day before!\n\n";
        restart();
    }
    void restart()
    {
        cout << "1. Products\n";
        cout << "2. Who are we?\n";
        cout << "3. Where?\n";
        string command;
        cin >> command;
    
        if (command == "1")
            products();
    	else if (command == "2")
            about();
    	else if (command == "3")
            where_to();
    }
    Thanks,
    Nathan

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    If you just want to run it once, there is not puch of a problem with the code. But you might be interested in putting it in a loop until an option to exit (say, 4. exit) is given.

    You have made a good choice in deciding to learn C++. If you learn it well, you could learn almost any other language like a breeze, also you would get to know some of the internal working of the computer, its memory structure, and so on.

  3. #3
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9

    Question

    Ok, Thanks.
    What loop would I use and how would I go about it? Also, I have added the exit function, but I'm not sure if it is correct?
    Sorry for the questions.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void products();
    void about();
    void where_to();
    void restart();
    
    int main()
    {
    	cout << "########  ########   ######\n";
        cout << "##     ## ##     ## ##    ##\n";
        cout << "##     ## ##     ## ##\n";
        cout << "##     ## ########   ######\n";
        cout << "##     ## ##              ##\n";
        cout << "##     ## ##        ##    ##\n";
        cout << "########  ##         ######\n";
        cout << "   Dead Pirates Society\n\n";
        cout << "Welcome to the DPS database!\n";
        cout << "Please enter a number from below for more information:\n";
        cout << "1. Products\n";
        cout << "2. Who are we?\n";
        cout << "3. Where?\n";
        cout << "4. Exit?\n";
        string command;
        cin >> command;
    
        if (command == "1")
            products();
    	else if (command == "2")
            about();
    	else if (command == "3")
            where_to();
        else if (command == "4")
            exit(0);
    }
    void products()
    {
        cout << "**********   *************\n";
        cout << "  Drinks       Chocolates\n";
        cout << "**********   *************\n";
        cout << "Coke		Mars\n";
        cout << "Solo		Bounty\n";
        cout << "Sunkist         Snickers\n";
        cout << "Lemonade	Cherry Ripe\n";
        cout << "Pepsi		Twirl\n";
        cout << "Fanta		Fling\n";
        cout << "$0.80		$1.20\n\n";
        restart();
    }
    void about()
    {
        cout << "We are a group that are raising money for\n";
        cout << "the year 12 common room. We aim to improve\n";
        cout << "the current state it is in to provide a better\n";
        cout << "environment for students to relax and study in!\n\n";
        restart();
    }
    void where_to()
    {
        cout << "We are located in the German Room (Room 21)\n";
        cout << "at lunch time. We can do deliverys and\n";
        cout << "special orders if notified a day before!\n\n";
        restart();
    }
    void restart()
    {
        cout << "1. Products\n";
        cout << "2. Who are we?\n";
        cout << "3. Where?\n";
        cout << "4. Exit?\n";
        string command;
        cin >> command;
    
        if (command == "1")
            products();
    	else if (command == "2")
            about();
    	else if (command == "3")
            where_to();
        else if (command == "4")
            exit(0);
    }
    Thanks for your help,
    Nathan

  4. #4
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    alien13, you could use a while loop or do...while loop. Assuming that you are not too good with loops, I will give you the while loop example.

    It is always a good practice to simplify your functions, and for that, I am making a new function called printmenu(). And I am just putting it in the simplest way, in an infinite loop. The code follows:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void products();
    void about();
    void where_to();
    void restart();
    
    int main()
    {
        while(1)
        {
    
            printmenu();
            string command;
            cin >> command;
    
            if (command == "1")
                products();
            else if (command == "2")
                about();
            else if (command == "3")
                where_to();
            else if (command == "4")
                exit(0);
        }
    }
    
    void printmenu()
    {
        cout << "########  ########   ######\n";
        cout << "##     ## ##     ## ##    ##\n";
        cout << "##     ## ##     ## ##\n";
        cout << "##     ## ########   ######\n";
        cout << "##     ## ##              ##\n";
        cout << "##     ## ##        ##    ##\n";
        cout << "########  ##         ######\n";
        cout << "   Dead Pirates Society\n\n";
        cout << "Welcome to the DPS database!\n";
        cout << "Please enter a number from below for more information:\n";
        cout << "1. Products\n";
        cout << "2. Who are we?\n";
        cout << "3. Where?\n";
        cout << "4. Exit?\n";
    }
    
    void products()
    {
        cout << "**********   *************\n";
        cout << "  Drinks       Chocolates\n";
        cout << "**********   *************\n";
        cout << "Coke		Mars\n";
        cout << "Solo		Bounty\n";
        cout << "Sunkist         Snickers\n";
        cout << "Lemonade	Cherry Ripe\n";
        cout << "Pepsi		Twirl\n";
        cout << "Fanta		Fling\n";
        cout << "$0.80		$1.20\n\n";
    }
    void about()
    {
        cout << "We are a group that are raising money for\n";
        cout << "the year 12 common room. We aim to improve\n";
        cout << "the current state it is in to provide a better\n";
        cout << "environment for students to relax and study in!\n\n";
    }
    void where_to()
    {
        cout << "We are located in the German Room (Room 21)\n";
        cout << "at lunch time. We can do deliverys and\n";
        cout << "special orders if notified a day before!\n\n";
    }
    You could also replace the exit(0) with a break command. That way, the program will have only one exit point. It is not required that you look into that now, but in big programs, it is sometimes easy to understand when many exit points are avoided. (Don't worry about it now.)

    **The problem with your approach**
    In your approach, the function stack will grow larger and larger. When functions are called, information relating to where the control has to return after a function completes, where to put the return value, and the arguments to the function are put in the function call stack. At first, when you call main(), your function stack will be like so:

    Code:
    |        |
    | main |
    --------
    Suppose you select products() next. Then, the call stack will become:

    Code:
    |             |
    | products |
    |    main    |
    ------------

    Then, after printing, restart() is called. Now, the call stack becomes:

    Code:
    |             |
    |  restart  |
    | products |
    |    main    |
    ------------
    Supoose you call about() next. Then the call to about again comes on top of the stack. Then, the restart() function is called. This keeps growing on and on. While your application is a simple one, and a hundred or even a couple thousand function calls couldn't fill up your RAM considerable, this can be avoided. The example I have given will not have that problem, as function call s return before the next one is called.

    I hope I am not being too complicated. If you feel so, you do not have to understand it right now, as you are a beginner. Learn more of the constructs of C++, and you can gradually learn this later on.

    Cheers.

  5. #5
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    Thanks for that, you explained it all really good, but the only thing i'm not 100% sure on and that is where you have:
    Code:
    While(1)
    What does the (1) actually do?

    Also, do you have any recommendations for books or video tutorials?
    I am currently watching the 3dbuzz(?) C++ videos, they are good, but a little old.

    Thanks for all your help!
    Nathan

  6. #6
    Join Date
    Nov 2007
    Posts
    1
    (1)here just means "true", you can rewrite it as
    while(true)
    this will turned out to be the same

    i recommended that you read <<c++ primer>>
    this is a great book for beginners

  7. #7
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    Ok, thanks. Seems simple enough!

    Thanks for the recommendation, I'll have a look into getting the book!
    Nathan

  8. #8
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    In C++, any (integer) value other than 0 is true. So you could also give

    while (2) or while(100) or while (-1). In C++, there is also a boolean type, so you could use while(true) or while(false). These loop structures also accept pointers, again, if the pointer points to null, it is false, else true.

    As for books, C++ Primer is good. If you want something for reference, try Microsoft's MSDN collection (it is not the best thing, as there might be many extensions which are Microsoft specific), and try the Linux Man pages (search for "linux man"), and search for your function / command.

  9. #9
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    Ah, ok thanks!

    Yeah I have used MSDN a few times. Thanks for the other reference!~

    Nathan

  10. #10
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    while(true) >> needs one bit for comparison 0 or 1 [finally 8 bit to complete a word but the memory will work only with the last bit]
    while(1) >> 1 is considered as integer, it will occupie 32 bit for comparison only ... I don't know if the code optimizer in C++ compiler will store the 1 as a bit containig "1" or no ; I think no ; so it's better to use the reserved true and false isn't it ?
    Last edited by Amahdy; 11-19-2007 at 05:41 PM.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  11. #11
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    32 bit / 256MB of memory is too small to consider. Besides, for optimization, or compulsarily on some architectures, you always have to use word alignment. So, even a boolean type may consume 4 bytes of memory, of which the 3 bytes will not be considered. And more often than not, comparison will take the same time whether it is 32 bits or 8 bits.

    And comparison does not take with only one bit. Take the following example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      bool val = 2;
      if(val)
        cout<<"true"<<endl;
      else
        cout<<"not true"<<endl;
      return 0;
    }
    If only the last bit was considered, it should have printed "not true", but this example will print "true". The code might not make sense, but is perfectly valid in C++, and shows to prove that though bool is a separate type, it is in most cases treated exactly like a char, or unsigned char rather.

  12. #12
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    But for matters of readability, it IS better to use true. I forgot to add this in my previous post.

  13. #13
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    Ok. Is this code fine?
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void about();
    
    int main()
    {
         cout << "       .__  .__              ____________\n";
         cout << "_____  |  | |__| ____   ____/_   \\_____  \\\n";
         cout << "\\__  \\ |  | |  |/ __ \\ /    \\|   | _(__  <\n";
         cout << " / __ \\|  |_|  \\  ___/|   |  \\   |/       \\\n";
         cout << "(____  /____/__|\\___  >___|  /___/______  /\n";
         cout << "     \\/             \\/     \\/           \\/\n";
         cout << "                 -- alien13 --\n\n";
         cout << "Enter a command from below and hit enter\n\n";
         cout << "1. About\n";
         cout << "2. Exit\n";
        while(true)
        {
                   string command;
                   cin >> command;
        
                   if (command == "1")
                   about();
                   else if (command == "2")
                   exit(0);
        }
    }
    
    void about()
    {
        cout << "Test... TesT....TEST...tESt..\n";
    }
    I wanted it so it still excepted the input but didn't add the header all the time.
    *I can't wait till I can start working with GUIs*

    Thanks,
    Nathan

  14. #14
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Yes, that's just fine.

  15. #15
    Join Date
    Nov 2007
    Location
    Australia
    Posts
    9
    Ok, Cool.

    Thanks for all your help. Everyone!

    Nathan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center