CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2013
    Posts
    9

    Question Pyramid of stars with spaces

    Hello, i'm totally beginer, can somebody help me solve this (for cycle)?

    Name:  31470_4423156869762_496554738_n.jpg
Views: 884
Size:  12.5 KB

    Thanks in advance.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Pyramid of stars with spaces

    Yes, but we're not a do your homework service, or a substitute for your classes or books. What have you got and where are you stuck?

  3. #3
    Join Date
    Mar 2013
    Posts
    9

    Re: Pyramid of stars with spaces

    Yes it is homework, but i really don't know how to solve it. I spend with it about hour.:/

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: Pyramid of stars with spaces

    So, what exactly is the assignment?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Pyramid of stars with spaces

    Quote Originally Posted by confused93 View Post
    Yes it is homework, but i really don't know how to solve it. I spend with it about hour.:/
    Then spend a few more hours, or days or weeks. Start at the beginning of the book and read and reread till it makes sense. You need some understanding of the basics before this forum can help you.

    You'll need a for loop and the cout statement.

  6. #6
    Join Date
    Mar 2013
    Posts
    9

    Re: Pyramid of stars with spaces

    I need program which draw this nice pyramid with for cycle, N=number of lines...
    i need only content of for cycle

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Pyramid of stars with spaces

    I spend with it about hour.:/
    So how far have you got? Post the code you've got so far (even if it doesn't compile or work) and we'll give you some pointers towards your next steps. As it's homework we can't write the code for you as that would be considered cheating. If you are having trouble learning the basics from your tutor you might want to look at these on-line tutorials for c++.

    http://www.cplusplus.com/doc/tutorial/
    http://www.learncpp.com/

    Have you used the cout statement in previous programs you're written and got to work properly? Have you used a for loop before?
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Mar 2013
    Posts
    9

    Re: Pyramid of stars with spaces

    #include <iostream>
    #include "funkce.h"
    using std::endl;
    using std::cout;

    void pyramida(int N, char symbol){

    for(int i=0; i<N; i++){
    cout << symbol << string(i, " ") << endl;
    }

    }
    i need to find right logical condition into for

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Pyramid of stars with spaces

    When you post code, please could you use [code] tags. I should have mentioned this in post #7. Click Go Advanced, select code then click '#'. A couple of pointers.

    Code:
    cout << symbol << string(i, " ") << endl;
    With string() you are trying to create a string of i spaces. However, this will give a compiler error as the space needs to be in single quotes for a character rather than double quotes for a string. Also cout prints from left to right. So you are first printing the symbol and then printing a number of spaces and then a new line!!
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Pyramid of stars with spaces

    Have you actually tried to compile and run this progam? You need to have a main() function that calls your pyramida() function. As you are using the string class, you will also need to have #include <string> as well togther with using std::string; Try compiling and running this program. Look at the output and then see if you can figure out what else needs to go in the for loop to produce the triangle.

    Happy coding
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Mar 2013
    Posts
    9

    Re: Pyramid of stars with spaces

    Yes, i have function.cpp, function.h and main.cpp but all what i need is logical condition into for cycle

    Code:
    void pyramida(int N, char symbol){
    		
    		cout << symbol << endl;
    	    for(int i=0; i<N; i++){
    		cout << string(i, ' ') << endl;
    }

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Pyramid of stars with spaces

    Try this for a starter hint. See what it displays and then see if you can add the code to complete it.

    Code:
    void pyramida(int N, char symbol)
    {
        for (int i = 0; i < N; i++){
    	cout << string(i, ' ');
    	cout << symbol << endl;
         }
    }
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Mar 2013
    Posts
    9

    Re: Pyramid of stars with spaces

    I solved it. Thank you.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured