C++/Game Tutorial 20: printf, C-Strings, and Documentation!



On this episode, along with studying extra about strings, you’ll learn to analysis features utilizing documentation with the intention to be taught issues by yourself! I additionally offer you a mini problem to do, however don’t fret if you cannot determine it out, this one is basically powerful. Attempt utilizing solely 2 or much less for loops in case you can!

Examine right here for printf documentation

Trace 1: have a look at * and .*
Trace 2. Precision works on strings! Attempt it out!

My resolution to printf Problem with rationalization. : ( Remember to strive by yourself first! )

Twitter:
Seed Of Andromeda Channel:
Seed Of Andromeda Web site:

Key: c recreation tutorial

Tags: C++,Tutorial,Seed,Of,Andromeda,Minecraft,OpenGL,SDL,Voxel,Engine,make,video,video games

See also  Making a Doom-style 3D engine in C
26 thoughts on “C++/Sport Tutorial 20: printf, C-Strings, and Documentation!”
  1. My solution:

    int rows;
    char character;

    cin >> rows;
    cin >> character;

    for(int i=0; i<rows; i++){

    printf("%*c", rows-i, character);

    for(int j=0; j<i; j++){

    printf("%c%c", character, character);

    }

    printf("n");

    }

  2. I am pretty sure there is a mistake here. When you use %d for int, it works fine because int is accepted as double (since it takes less bytes), but the actual usage should be %whatevertypeyouhavebeenusing. So that would make it %i for int, %d for double, %f for float, %char for char etc. That's what we learned in C programming, anyway.

  3. here is an easier solution to multiply the string for every iteration to output the desired number of characters. This is the loop for the top half:

    cout << "Enter Height of your Pyramid: ";
    int height;
    cin >> height;

    for (int i = 0; i < height; i++) {
    std::string s((2*i+1), '.');
    printf("%*sn", height + i, s.c_str());
    }

  4. haha no need for to0 much code just gonna leave this here :

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;

    int main()
    {
    string dot = ".";
    int couter = 30;
    for (int i = 18; i > 0;i–)
    {
    printf("%*sn",couter,dot.c_str());
    couter += 1;
    dot += " .";
    }
    system("PAUSE");
    return 0;

  5. Here's the code to do the pyramid using printf() formatting codes and ONE line of code!!! 😀

    for (int i = 1, j = 5; i <= 9; i += 2, j++) printf("%*.*sn", j, i, "………");

  6. A hundred stack tall one!
                                                                                                        *
                                                                                                       *
                                                                                                      ***
                                                                                                     *****
                                                                                                    *******
                                                                                                   *********
                                                                                                  ***********
                                                                                                 *************
                                                                                                ***************
                                                                                               *****************
                                                                                              *******************
                                                                                             *********************
                                                                                            ***********************
                                                                                           *************************
                                                                                          ***************************
                                                                                         *****************************
                                                                                        *******************************
                                                                                       *********************************
                                                                                      ***********************************
                                                                                     *************************************
                                                                                    ***************************************
                                                                                   *****************************************
                                                                                  *******************************************
                                                                                 *********************************************
                                                                                ***********************************************
                                                                               *************************************************
                                                                              ***************************************************
                                                                             *****************************************************
                                                                            *******************************************************
                                                                           *********************************************************
                                                                          ***********************************************************
                                                                         *************************************************************
                                                                        ***************************************************************
                                                                       *****************************************************************
                                                                      *******************************************************************
                                                                     *********************************************************************
                                                                    ***********************************************************************
                                                                   *************************************************************************
                                                                  ***************************************************************************
                                                                 *****************************************************************************
                                                                *******************************************************************************
                                                               *********************************************************************************
                                                              ***********************************************************************************
                                                             *************************************************************************************
                                                            ***************************************************************************************
                                                           *****************************************************************************************
                                                          *******************************************************************************************
                                                         *********************************************************************************************
                                                        ***********************************************************************************************
                                                       *************************************************************************************************
                                                      ***************************************************************************************************
                                                     *****************************************************************************************************
                                                    *******************************************************************************************************
                                                   *********************************************************************************************************
                                                  ***********************************************************************************************************
                                                 *************************************************************************************************************
                                                ***************************************************************************************************************
                                               *****************************************************************************************************************
                                              *******************************************************************************************************************
                                             *********************************************************************************************************************
                                            ***********************************************************************************************************************
                                           *************************************************************************************************************************
                                          ***************************************************************************************************************************
                                         *****************************************************************************************************************************
                                        *******************************************************************************************************************************
                                       *********************************************************************************************************************************
                                      ***********************************************************************************************************************************
                                     *************************************************************************************************************************************
                                    ***************************************************************************************************************************************
                                   *****************************************************************************************************************************************
                                  *******************************************************************************************************************************************
                                 *********************************************************************************************************************************************
                                ***********************************************************************************************************************************************
                               *************************************************************************************************************************************************
                              ***************************************************************************************************************************************************
                             *****************************************************************************************************************************************************
                            *******************************************************************************************************************************************************
                           *********************************************************************************************************************************************************
                          ***********************************************************************************************************************************************************
                         *************************************************************************************************************************************************************
                        ***************************************************************************************************************************************************************
                       *****************************************************************************************************************************************************************
                      *******************************************************************************************************************************************************************
                     *********************************************************************************************************************************************************************
                    ***********************************************************************************************************************************************************************
                   *************************************************************************************************************************************************************************
                  ***************************************************************************************************************************************************************************
                 *****************************************************************************************************************************************************************************
                *******************************************************************************************************************************************************************************
               *********************************************************************************************************************************************************************************
              ***********************************************************************************************************************************************************************************
             *************************************************************************************************************************************************************************************
            ***************************************************************************************************************************************************************************************
           *****************************************************************************************************************************************************************************************
          *******************************************************************************************************************************************************************************************
         *********************************************************************************************************************************************************************************************
        ***********************************************************************************************************************************************************************************************
       *************************************************************************************************************************************************************************************************
      ***************************************************************************************************************************************************************************************************
     *****************************************************************************************************************************************************************************************************

  7. I SERIOUSLY hope that I can get to a point where I don't have to read documentation anymore, or if I do it's a rare thing…I absolutely DESPISE reading documentation. X(

  8. Thank you for these tutorials. It's good review and I've learned a few things that I didn't know previously. Also, in your Pastebin solution, I think you spelled diamond incorrectly.

  9. Hey Ben, thanks for this tutorial. 🙂 The previous c++ guides that I followed always failed to tell me about c-strings and how things all fits together. Resulting in a lot of frustration and ultimately quiting c++.

  10. SPOILER My response to the challenge! Do not read until you have done it ;P

    for (size_t i = 0; i < 10; ++i)
    {
        printf("%.*s n", (10 + i), (1 + (2 * i)), "******************");
    }

    Huzzah!

    Where the bold start is an * but Google uses it to identify bold type.

    For Diamond shape:

    for (signed int i = -9; i < 10; ++i)
    {
        printf("%.*s n", (10 + (9 – std::abs(i))), (1 + (2 * (9 – std::abs(i)))), "******************");
    }

Comments are closed.