Thread: [Newbie Programming] How to increase/increment the size of a string variable..

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up [Newbie Programming] How to increase/increment the size of a string variable..

    So I want to create step/waterfall type effect using string.

    I have managed to draw a single step. However, everytime the program loops I want it to increase the size of the step.

    ie:

    Code:
    *
    **
    ***
    ****
    Current Code
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    
    
    int main()
    {
    
    
        // A prompting message to user
        cout << "How many steps you wish to draw: ";
        int steps;    // Variable that stores the amount of steps    
        cin >> steps;    // stores users given value
    
    
        // The loop runs depending on the user value
        for(int i = 0; i < steps; i++)
        {
            // This is the normal length of a step
            string nstep = "*";
            // Draws the step
            cout << nstep + "*" << endl;    
        }
    
    
        int x;
        cin >> x;
    
    
        return 0;
    }
    Current code produces:

    How many steps you wish to draw?
    5

    and it draws:

    Code:
    *
    *
    *
    *
    *
    Last edited by Swadesh; 08-22-2012 at 11:50 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make a simple flowchart that demonstrates how to accomplish this. It's not harder than that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Swadesh View Post
    However, everytime the program loops I want it to increase the size of the step.
    No, actually you just want to print more stars each time.
    I'm just trying to correct your thinking in such a way that it'll correct your code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If i were you i would think like this.In 1st loop i want one star to be printed.At 2nd loop i want two stars to be printed etc. and based on this i would solve the problem

    Also if i was in your shoes,i would also check if the user typed a non-negative number.By mistake(or not) if a user types -1 for example the your program will print nothing,but ok steps is good to be a non-negative number.In this code there is no problem,but in other code it might be.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Try using two for loops. The second loops length should depend on the which stage of the first loop you are at.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up

    This Is How I managed To Solve.

    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int main()
    {
    
    
        // Showing message to user 
        cout << "Hey, enter the amount of steps you wish to draw: " ;
        int stepsn;  // Variable that will store the user value
        cin >> stepsn;  // stores the value from user
        cout << "" << endl; 
    
    
        // Default step size, is simply empty
        string steps = "";
    
    
        // A loop that runs depending on user input 
        for(int i=0 ; i < stepsn ; i++)
        {
            // Takes the last size of variable steps and stores that info into new variable called stepz
            string stepsz(steps.size(), '*');
    
    
            // Here, it combines last step size, plus an additional * is added and applies to the original steps variable
            steps = stepsz + "*";
    
    
            // Finally drawn on screen everytime loop runs
            cout << steps << endl;
        }
    
    
        // This code is just to stop the program from closing ..
        int x;
        cin >> x;
    
    
        return 0;
    }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It is good practice to write int main(void) instead of int main().

    Also at line 15,i think that you want to do is just switching to new line.This can be done by cout<<endl; I mean the "" was not necessary.

    And last but not least,it feels good solving the problem ,isn't it?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    It is good practice to write int main(void) instead of int main().
    No, it is not. In C++, they are the same, with the former looking uglier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Elysia View Post
    No, it is not. In C++, they are the same, with the former looking uglier.
    Oh yes,i am have the c style in mind always .Thanks for correcting me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String with variable size
    By stdq in forum C Programming
    Replies: 5
    Last Post: 05-15-2011, 05:46 PM
  2. Increase Message Queue Size
    By Yarin in forum Windows Programming
    Replies: 3
    Last Post: 12-15-2008, 10:06 AM
  3. Increase size of global multidimensional array
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 04-22-2006, 09:00 PM
  4. increase array size?
    By rodrigorules in forum C Programming
    Replies: 3
    Last Post: 09-18-2005, 12:15 PM
  5. Increase Font Size Or Bold???
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 08-27-2004, 05:26 PM