dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: My first program 😀

  1. #1
    Join Date
    Jun 2018
    Posts
    2

    Question My first program 😀

    Hey, I'm a beginner in coding and this is my first C++ program, so I just wanted know if all was okay

    Code:
    #include <iostream>
    using namespace std;
    #include <chrono>
    #include <thread>
    
    void my_sleep(int ms)
    {
            std::this_thread::sleep_for(std::chrono::milliseconds(ms));
    }
    
    void display(const std::string &text)
    {
            for (int i = 0; i < text.size(); ++i)
            {
                    std::cout << text[i] << std::flush;
                    my_sleep(52);
            }
    }
    
    int main()
    {
    
    
    
    
            char nom[50];
     
    
             
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
    
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
            cout << "" << endl;
    
            display("ναℓℓ∂αяιση'ѕ α∂νєηтυяє\n");
            cout << "" << endl;
            display("A terrible misfortune has just occurred in the peaceful kingdom of Valldarion; the evil Dragon of the Night has just awakened from a hundred-year-old sleep. You are one of the greatest knights in the kingdom, so it's up to you to defeat the monster.\n");
            cout << "" << endl;
            display("But ... what is your name?\n");
            cout << "" << endl;
            cin >> nom;
            cout << "" << endl;
            cout << "Good, " << nom << "." << endl;
            display("So you wake up one morning in the cozy room of an inn in the town of Balmora. You take your faithful Vaillante sword then your horse, ready for the long quest that awaits you.\n");
            cout << "" << endl;
            display("You ride on horseback, crossing the city at full gallop. At the end of Balmora, you decide to go through the evil forest of Durmur, which is right in front of the mountain of the Dragon of the Night, to avoid making a detour.\n");
            cout << "" << endl;
    
            display("You arrive at the edge of the evil forest at dusk. Your horse seems nervous and refuses to enter the forest after dark.\n");
            cout << "" << endl;
            display("What are you going to do ?\n");
            cout << "" << endl;
            cout << "1. Enter anyway." << endl;
            cout << "2. Redirect to another path." << endl;
            int tchoice = 0;
            cout << "" << endl;
            cin >> tchoice;
            if (tchoice == 1)
    
            {
                    cout << "" << endl;
                    cout << "You enter the evil forest in the middle of the night. Unfortunately for you, the Spirit of the Evil Forest stalks you and ends up finding you. He devours you even before you have unsheathed your sword. The adventure ends here, " << nom << "." << endl;
                    cout << "" << endl;
            }
    
            else
    
            {
                    cout << "" << endl;
                    display("You arrive at the ruins cursed in the middle of the night. There does not seem to be anyone, so you decide to set up a camp for the night and let your horse rest. You watch until you sink into a deep sleep ...\n");
                    cout << "" << endl;
                    display("You wake up with a start and draw your sword after hearing the characteristic cry of the Night Dragon.\n");
                    cout << "" << endl;
                    display("What are you going to do ?\n");
                    cout << "" << endl;
                    cout << "1. To run away." << endl;
                    cout << "2. Take your sword and head to the source of the scream." << endl;
                    int thchoice = 0;
                    cout << "" << endl;
                    cin >> thchoice;
                    if (thchoice == 1)
    
                    {
                            cout << "" << endl;
                            cout << "You flee but unfortunately fall on a band of goblins, who jump on you and put you in piece. The adventure ends here, " << nom << "." << endl;
                            cout << "" << endl;
                    }
    
                    else
    
                    {
                            cout << "" << endl;
                            display("You run to the source of the cry. In front of you is the terrible and gigantic Dragon of the Night, which rushes towards you like an evil shadow. You draw your sword and jump into battle.\n");
                            cout << "" << endl;
                            cout << "After a fierce fight, you finally triumph." << endl;
                            cout << "" << endl;
                            display("What are you gonna do ?\n");
                            cout << "" << endl;
                            cout << "1. Slice the Dragon's Head of the Night." << endl;
                            cout << "2. Return to town and celebrate your victory." << endl;
                            int fthchoice = 0;
                            cout << "" << endl;
                            cin >> fthchoice;
                            if (fthchoice == 1)
    
                            {
                                    cout << "" << endl;
                                    display("You did well. The Night Dragon was not dead yet and was about to jump on you one last time. After completing it, you return to the city and celebrate your victory in the best hostel in Balmora. щεℓℓ ℓąγεđ !\n");
                                    cout << "" << endl;
                            }
    
                            else
    
                            {
    
                                    cout << "" << endl;
                                    cout << "You should not have. The Night Dragon was not dead yet and jumps on you before burning you with its fiery fire. The adventure ends here, " << nom << "." << endl;
                                    cout << "" << endl;
                            }
                    }
            }
    }
    Here's the executable http://bit.ly/ValldarionAdventureCPlusPlus

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,411

    Re: My first program 😀

    Welcome!

    You don't need the "" like this

    Code:
    cout << "" << endl;
    You can just do
    Code:
    cout << endl;
    also you can more than one on a line like this

    Code:
    cout << endl << endl << endl;
    You're using string, but haven't included the header
    Code:
    #include <string>
    using namespace std (if used) would usually come after the last #include

    Why not make nom a string rather than an array of char?

    Code:
    void display(const std::string &text)
    {
            for (int i = 0; i < text.size(); ++i)
            {
                    std::cout << text[i] << std::flush;
                    my_sleep(52);
            }
    }
    would more usually be coded as
    Code:
    void display(const std::string &text)
    {
            for (const auto& t : text) {
                    std::cout << t << std::flush;
                    my_sleep(52);
            }
    }
    using a range-based for loop.

    Where you have a choice of 1 or 2, you are checking for 1 as part of the if, but assuming if not 1 then 2 must have been entered so you do the else part. What if say 3 is entered? For input you could have a loop displaying the text, obtaining input, checking its validity and only exiting the loop if the input is valid. Note that if you have more than 2 choices then you could use the switch statement.

    You're ending up with deeply nested if-statements which can be hard to track and debug. You might want to re-think this logic so that when the advebnture ends, the program ends so the else part of the if isn't needed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  3. #3
    Join Date
    Jun 2018
    Posts
    2

    Re: My first program 😀

    Thanks !

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: My first program 😀

    In modern C++, that is C++17, I would use std::string_view in your display function. So:
    Code:
    void display(std::string_view text)
    {
            for (const auto& t : text) {
                    std::cout << t << std::flush;
                    my_sleep(52);
            }
    }
    Note: it's perfectly fine to pass an std::string_view by value. It's basically a read-only view of a string. It doesn't contain the string itself, it points to a string somewhere else. Since it doesn't contain the string itself, passing by value is very cheap.

    The C++ Standard Library also provides chrono literals. These allow you to simply write something as follows:
    Code:
    std::this_thread::sleep_for(50ms);
    And since you already have a using namespace std:
    Code:
    this_thread::sleep_for(50ms);
    So this maybe reduces the need to write your my_sleep() function.

    Next to what 2kaud said, personally I find your main() function to be a bit too long. You should keep 2kaud's advice in mind, and possibly split the code up in more separate, well-defined helper functions.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)