hi i wrote this program... it compiles with out any errors.. but when i try to run it in G++ compiller it gives the error "Segmentation Fault (core dump)"... i did a debug on visual c++
and it breaks at line 32 **which is marked below.

what the program does is, it takes 3 arguments, a number and 2 symbols,,, and draws a diamond...

it worked fine before i put the arguments to int main()..... can enyone point out the error for me please...i just have no friggin idea what went wrong

the codes are as follows......thanks :)...............

#include <iostream>
#include <stdio.h>
#include <ctype.h>
using namespace std;

void diamondrun(int * , char  , char );
void diamonddraw (int  , int , char , char );

// put arguments///////////////////

int main (int argc, char *argv[])
{
int hight;
char insymb, outsymb;

if ((argc > 4)) 
{
cout << '\n';
cout << "starting diamond" << '\n';
cout << "Syntax: diamond size outside inside" << '\n';
cout << "size must be integer > 0" << '\n';
cout << "outside and inside must be single chars" << '\n';
}
else
{
cout << '\n';
cout << "starting diamond" << '\n';
//printDiamond(value, *string1, *string2);

hight = atoi(argv[1]);//////////////////breaks here////////////////
insymb = *argv[2];
outsymb = *argv[3];

         diamondrun(&hight,insymb,outsymb);
		return 0;
}
}

// this function runs the diamonddraw function
void diamondrun(int *hightptr, char insymb, char outsymb)
{
   int c;   // counter

   if (*hightptr % 2 == 1)   // to check odd or even
   {
      // Draw the top half of the diamond, including the middle
      for(c=1; c <= *hightptr; c+=2)
      {
         diamonddraw(*hightptr, c, insymb, outsymb);  
      }

      // Draw the remaing half of the Diamond
      for(c=*hightptr-2; c >= 1; c-=2)
      {
         diamonddraw(*hightptr, c, insymb, outsymb);  
      }

   }
   else        // if the hight is even
   {
      // Draw the top half of the diamond, including the middle
      for(c=1; c <= *hightptr; c+=2)
      {
         diamonddraw(*hightptr, c, insymb, outsymb);  
      }
	  
      // Draw the remaing half of the Diamond      
      for(c=*hightptr-3; c >= 0; c-=2)
      {
         diamonddraw(*hightptr, c, insymb, outsymb);  
      }
	 
   }
}

void diamonddraw(int width, int symbcount, char insymb, char outsymb)
{
   int preSpaces = (width - symbcount) / 2; 
   int c;                 

   // to print the spaces at the begining       
   for (c = 1; c <= (preSpaces); c++)
   {
      printf(" ");

   }
  
   for (c = 0; c <= (symbcount); c++)
   {
      printf("%c", &outsymb);// outline symbol

	  for (c = 2; c <= (symbcount-1); c++)
      {
        printf("%c", &insymb);// body symbol
   
	  }
	  //draw right side of big diamond :P
	    if (c!=2)
	    {
		  printf("%c", &outsymb);//outline symbol
	    }
   }
  
   // next line
   printf("\n");//jumps to next line

}

Recommended Answers

All 4 Replies

Are you in fact passing arguments to the program when you run it? It works ok for me, other than printing characters other than what's specified for the diamond's inner body and edge.

Why all the use of pointers and address operators, when it looks like all function parameters should just be by value?

Are you in fact passing arguments to the program when you run it? It works ok for me, other than printing characters other than what's specified for the diamond's inner body and edge.

Why all the use of pointers and address operators, when it looks like all function parameters should just be by value?

yes.. i have to run it on linux using the g++ compiler. so there the input is like......
/a.out 4 & $. the 4 being the int hight and the required symbols. it compiles in the g++ compiler with no trouble... but when running it ..gives the error "Segmentation Fault core dump"

& is special to the shell, it means run the program in the background (with only one argument).

1. You MUST check argc before blindly assuming that all of argv is valid.
Like for example, checking < 4, and not > 4 like you are doing.

2. If you want to pass & as a parameter, then perhaps
./a.out "&"
or
./a.out \&

Use

if(argc != 4) return;
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.