Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,264 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




Nested Loop Problem not printing out exactly right

 
Reply to this topicStart new topic

Nested Loop Problem not printing out exactly right, I need some help to make my nested loop print right

fero4u123
3 Nov, 2006 - 03:30 PM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2006
Posts: 1


My Contributions
CODE
/************************************************************************/
/*                            Header Files                              */
/************************************************************************/
#include <iostream.h>                                                   */
#include <conio.h>                                                      */
#include <stdlib.h>                                                     */
/************************************************************************/
/*                  Structures, Types, Classes, Macros                  */
/************************************************************************/
/************************************************************************/
/*                       Global Declarations                        */
/************************************************************************/
/************************************************************************/
/*                              Prototypes                              */
/************************************************************************/
void triangle (int height);
/************************************************************************/
/*                                Main                                  */
/************************************************************************/
int main()
  {    clrscr();
    int height;
    cout << "What is the height of the triangle??\n";
    cin >> height;

    triangle(height);
    getch();
    return 0;
  }

/************************************************************************/
/*                  Task: Printout the Triangle                       */
/************************************************************************/
void triangle (int height)
{  for(int row =1; row <=height; row++)
      {textcolor(row);
       for(int blanks=0; blanks <= row; blanks++)
      cprintf(" ");
       for(int star=height; star>= row*2-1; star--)
      cprintf("*");
      cout << "\n";
      }
}


//now my problem is when i run this program it is suppose to print out //a triangle with the height of what the user entered and the //characters should be decreasing by 2. So if i ran the program and i //put the number 5 for the height i want it to print out something like //this

//     *********
//      *******
//        *****
//          ***
//            *

//can someone please help me with this ?


im sorry but the triangle at the bottom is suppose to look like a upsidedown pyramid but i cant get it to come out right

This post has been edited by fero4u123: 3 Nov, 2006 - 03:35 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Nested Loop Problem Not Printing Out Exactly Right
3 Nov, 2006 - 10:09 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(fero4u123 @ 3 Nov, 2006 - 04:30 PM) *

//now my problem is when i run this program it is suppose to print out //a triangle with the height of what the user entered and the //characters should be decreasing by 2. So if i ran the program and i //put the number 5 for the height i want it to print out something like //this

// *********
// *******
// *****
// ***
// *

//can someone please help me with this ?

im sorry but the triangle at the bottom is suppose to look like a upsidedown pyramid but i cant get it to come out right


CODE
void triangle (int height)
{  for(int row =1; row <=height; row++)
      {textcolor(row);
       for(int blanks=0; blanks <= row; blanks++)
      cprintf(" ");
       for(int star=height; star>= row*2-1; star--)
      cprintf("*");
      cout << "\n";
      }
}

You code is a bit of a C++ and C blend, and I believe that you need to spend a little more time with pen and paper to map out the algorithm. Some improvement can be made by using all C++ ( that is iostreams).

Have you considered the following from iostream/iomanip:
  • setw
  • setfill
They will definitely make this work much better smile.gif

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Nested Loop Problem Not Printing Out Exactly Right
4 Nov, 2006 - 03:38 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
Some improvement can be made by using all C++ ( that is iostreams).

Or stick to ONLY C.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month