hey guys, just wondering how I can change this code to C++... It's an old code my brother actually made!

#include <stdio.h>

const int ROW = 4;
const int COL = 13;

int main(void) {
   int i, c, max = COL;
   
   for(i = 0;i<ROW;i++) {
      for(c=0;c<COL;c++) {
         if(c == i+1 || c == 6 || c == max-2)
            putchar(' ');
         else   
            putchar('*');
      }
      --max;
      putchar('\n');
   }
   return 0;
}

Make an attempt before asking for help, or ask questions about the language details, so you can get a better understanding.

Make an attempt before asking for help, or ask questions about the language details, so you can get a better understanding.

Ok well, I'm learning C++, and as far as I can tell, this piece of code is quite similar to C++. He said it is C but I don't know the major differences apart from headers and inputs and outputs should be replaced with cout << and cin >>..

C++ is an extension to C. In addition to iostreams (and fstreams and stringstreams), it provides per-type operator-overloading and inheritable class objects. C code -is- valid C++, and an example as simple as yours doesn't need to be "converted". However, if you're learning C++, might I suggest that you start by understanding what your brother's program does (and how, and why), and then create a class that you can instantiate with any values for ROW and COL. I'll give you a start:

class Funny
{
protected:
    int width;
    int height;

public:
    Funny();  // default constructor
    Funny(int w, int h);  // constructor that takes a specified size
    void Draw();
};

Funny::Funny():
    width(13), height(4)
{}

Funny::Funny(int w, int h):
    width(w), height(h)
{}

void Funny::Draw()
{
    // use cout to layout spaces and stars in whatever way you like,
    // according to width and height
}

>>I don't know the major differences apart from headers and inputs and outputs should be replaced with cout << and cin >>

Well. That's actually all you need to change to turn the code into C++ code.

#include <iostream>  //this replaces stdio.h

const int ROW = 4;
const int COL = 13;

int main(void) {
   int max = COL;
   
   for(int i = 0;i<ROW;i++) { //in C++, it is preferred to declare loop variables in the for-statement.
      for(int c=0;c<COL;c++) {
         if(c == i+1 || c == 6 || c == max-2)
            std::cout << ' ';  //output is done through std::cout.
         else   
            std::cout << '*';
      }
      --max;
      std::cout << std::endl;  //std::endl is used for new-line (instead of '\n').
   }
   return 0;
}

This is very informative! Thank you, is it possible to use only cout << and << endl; without the std::, this is how I have read anyway?... I am playing around with this code actually to try and learn how to manipulate it further and trying to make the 4 triangles use 10 stars and changing the pattern... my code looks OK, but then I am not sure how to progress! This is only for fun so would be helpful any hints you have, I'm 14 so learning slowly!

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main(int argc, char *argv[]) 
{
   const int row = 10;
   const int col = 25;
   
   int max = col;
   
   for( int i = 0; i < row; i++ ) 
   {
      for(int c = 0; c < col; c++) 
      {
         if(c == i+1 || c == 6 || c == max - 2)
            cout << " ";
         else   
            cout << "*";
      }
      max--;
      cout << endl;
   }
	getch();
	return 0;
}

I trying to make it look like this instead...

*    **** ****    *
**   ***   ***   **
***  **     **  ***
**** *       * ****
//up to 10 *

I dont think I can! jajaja

First, yes, your inclusion of the line using namespace std; at the top of your program means you don't have to specify std:: before cin/cout/cerr/endl throughout your code (but posting goddess Narue will yell at you for being so all-inclusive ;-P).

Then I think you CAN make a bunch of triangles! For each line of output and character position within that line, think about where you are in relation to each triangle (hint: there are four squares, each divided in half along a diagonal, and each character position is above/below/on that diagonal), and therefore which character to draw at that position.

Start with just the first triangle, maybe with a vertical line to the right, so you know that you're writing out the correct number of empty spaces. Then add code to do the next triangle, and so on. Let us know how it goes!

First, yes, your inclusion of the line using namespace std; at the top of your program means you don't have to specify std:: before cin/cout/cerr/endl throughout your code (but posting goddess Narue will yell at you for being so all-inclusive ;-P).

Then I think you CAN make a bunch of triangles! For each line of output and character position within that line, think about where you are in relation to each triangle (hint: there are four squares, each divided in half along a diagonal, and each character position is above/below/on that diagonal), and therefore which character to draw at that position.

Start with just the first triangle, maybe with a vertical line to the right, so you know that you're writing out the correct number of empty spaces. Then add code to do the next triangle, and so on. Let us know how it goes!

Jaja well I went a little crazy and I think I got it working! but looks a little messy to me!

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main(int argc, char *argv[]) 
{
   const int ROW = 10;
   const int COL = 54;
 
   int first = 1;
   int second = 15;
   int third = 25;
   int fourth = 29;
   int fifth = 39;
   int sixth = 53;
   int seventh = 54;
   int row;
   int col;
   
   for( row = 0; row < ROW; row++ ) 
   {  
      for( col = 0; col < COL; col++ ) 
      { 
         if( col < first ) 
         {
            cout << "*";
         }
            else if( col >= first && col < second ) 
            {   
               cout << " ";
            }
               else if( col >= second && col < third ) 
               {
                  cout << "*";           
               }
                  else if( col >= third && col < fourth ) 
                  {
                     cout << " ";
                  }
                     else if( col >= fourth && col < fifth ) 
                     {
                        cout << "*";
                     }
                        else if( col >= fifth && col < sixth ) 
                        {
                           cout << " ";
                        }
                           else
                           cout << "*";
      }
      
      cout << endl;
      
      ++first;    
      --third;
      ++fourth;
      --sixth;
   }
   getch();
   return 0;
}
//------------------------------------------------------------------------------

If you want to avoid a lot of repetition and "magic numbers" in your code, you might want to think about computing the 'first', 'second', .. variables in terms of ROW and COL. You also might want to think about not duplicating code that prints the same pattern (like your example pattern has two repeated patterns (lower triangle, upper triangle, lower triangle, upper triangle). A hint is to use the integer modulus operator % and the integer division /.

Hello,
Main difference between c and c++ is that c++ is OOP , but c is not.
You can figure the code using object. It can be wright without object also.
Choice is yours.

commented: C++ is not OOP!! and bump -1 -3

If you want to avoid a lot of repetition and "magic numbers" in your code, you might want to think about computing the 'first', 'second', .. variables in terms of ROW and COL. You also might want to think about not duplicating code that prints the same pattern (like your example pattern has two repeated patterns (lower triangle, upper triangle, lower triangle, upper triangle). A hint is to use the integer modulus operator % and the integer division /.

how would I do the modulus part? I did your first suggestion thankyou

>>how would I do the modulus part?

Well, lets say you want to draw triangles which are 5 characters wide (4 characters of the triangle and one empty column). But if the COL number might be larger than 5, and thus, several triangles could fit along the width. Then if you want to know how many triangles will fit, you can use the integer division, which discards the remainder of the division:

int numberOfTriangles = COL / widthOfOneTriangle; //if COL == 22 and widthOfOneTriangle == 5, you will get numberOfTriangles == 4  (i.e. only 4 triangles fit entirely in 22 characters).

If you want to iterate through the column and know at which position you are in the current triangle you are in, then you can use the modulus (which gives you the remainder of the division only), as so:

for(int i = 0; i < COL; ++i) {
    int positionInTriangle = i % widthOfOneTriangle;  //if i == 18 and widthOfOneTriangle == 5, you will get positionInTriangle == 3  (i.e. the greatest multiple of 5 that fits in 18 is 15, and 18 - 15 = 3).
    // now use positionInTriangle to determine whether to print ' ' or '*'.
  };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.