xavier666 56 Junior Poster

You might wanna mark this thread as solved...

xavier666 56 Junior Poster

Note your code generates a memory leak on line 43

BTW, is leaking memory like you are doing also a requirement?

Since it was a small program, i thought it wouldn't hurt to have a small leak. Don't worry, I always free memory in big programs and hence forth, I'll also do even in small programs.

xavier666 56 Junior Poster

I was expecting someone to explain him the problems about his code. Ah well ...

Hi harshchandra,
Thank you for presenting your code snippet but your code has some major faults, hence lost it's appeal. Your program displays the bad practices of C. They are as follows

  • conio.h - It is not a standard C header file and is only used in old MS-DOS compilers. You can see about it here
  • void main() - Let's just say 'void main'ers are doomed' (-Salem)
  • goto - There are a lot of alternatives to goto . It is against structured programming
  • getch() & clrscr() come under conio.h, so no need to repeat

This is an excellent link about the bad practices of C (explaining WHY they are bad) and what to do to avoid them.

anonymous alias commented: pay attention, dude. this crap is over 5 years old. it doenst need obvious commentary. +0
xavier666 56 Junior Poster

All this looks very exciting but you have got to understand that some of us don't understand Bulgarian (Had to Google it and I'm still not sure). Please convert the program outputs & comments in English.
You should have read the forum rules which clearly states to write "in full-sentence English".

jephthah commented: if you can't understand C, then you need to quit "helping" -1
xavier666 56 Junior Poster

Points to be noted, raghuhr84

  • Give the complete program, that is, give #include<stdio.h> . Becomes easier for us to check.
  • Use proper indentation
  • Use code tags around your program (See the forum rules)

The problem is that you have not flushed stdin. Put fflush(stdin); before the scanf . That should do it.
Here is the complete program :-

#include <stdio.h>

int main()
{
	char str1[100];
	char str2[100];

	printf("Enter 1st string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str1);

	printf("Enter 2nd string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str2);

	printf("1st string is %s\n", str1);
	printf("2nd string is %s\n", str2);

	return 0;
}
kvprajapati commented: Well said. +6
Aia commented: fflush(stdin) invokes undefined behaviour -1
jephthah commented: flushing an input buffer is meaningless, and adatapost should know better. -1
ssharish2005 commented: Read the manual and be clear beforr you post. fflush is not meant to flush stdin it for stdout. +0
iamthwee commented: Well said +11
xavier666 56 Junior Poster

That post has become to big ;)
I wish you could have give me a more precise link
Ah well ...

xavier666 56 Junior Poster

here's a bug: #include <conio.h> your program is worthless with that in it. why do you people insist on clinging onto that Turbo C crap?

I'm actually new. Can you please explain the problems faced with conio.h I've been getting insults just for using this.
PS - Why did they (don't know who) put conio.h in C in the first place if it was such a problem?

why do you people insist on clinging onto that Turbo C crap?

What should i be clinging to?

xavier666 56 Junior Poster

The program generates a calendar for one year. The program only asks for the day on which 1st January falls from user.
Notes :

  • Compiler - Turbo C++ 4.5
  • Leap year has been considered
  • The only reason for including conio.h is because of clrscr() . If somebody can give me a one-line alternative, I would be happy to modify the code
  • The user can either view a particular month or the whole year
  • Error handling has not been implemented (feeling lazy :yawn:)

Please don't flame me for using global variables. I don't know about the problems of global variables so enlighten me :P
PM me if you find any bugs or if you think it can be improved

jephthah commented: c code snippets are supposed to be useful. anything compiled in TurboC is useless for most of western civilization. if you wont stop using it, then at least stop subjecting others to it. -1
xavier666 56 Junior Poster

There you go

#include <iostream.h>
#include <conio.h>

int i;
int a[10];

int main()
{
	cout<<"\n\n\tEnter The 10 Numbers : \n";
	for(i=0;i<10;i++)
	{
		cout<<"\t";
		cin>>a[i];
	}

	cout<<"\n\n\tThe 10 Numbers In Reverse : \n\t";
	for(i=9;i>=0;i--)
	{
		cout<<a[i]<<"\t";
	}
    return 0;
}
mrnutty commented: Don't give out the answer just yet. +0
xavier666 56 Junior Poster
#include <stdio.h>

int i;	int j;	int row;	int column;
int sum[10][10];
int sub[10][10];

void dimension(void)
{
	printf("\n\n\tEnter The Dimensions Of The Matrix : ");
	printf("\n\n\t\tROW    :  ");
	scanf("%d", &row);
	printf("\n\n\t\tCOLUMN :  ");
	scanf("%d", &column);
}

void display(int *m[][10],int r,int c)
{
	for(i = 0;i < r;i++)
	{
		printf("\n\n\t");
		for(j = 0;j < c;j++)
		{
			printf("%d\t",(*(*(m + i) + j)));
		}
	}
}

void input(int *m[][10],int r,int c)
{
	printf("\n\n\tEnter The Elements : \n\n");
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			printf("\t");
			scanf("%d",&(*(*(m + i) + j)));
		}
	}
}

void addition(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sum[i][j] = m1[i][j] + m2[i][j];
			printf("[%d][%d]\n",m1[i][j],m2[i][j]);
		}
	}
}

void subtraction(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sub[i][j] = m1[i][j] - m2[i][j];
		}
	}
}

int mat1[10][10];
int mat2[10][10];

int main()
{
	dimension();
	input(mat1,row,column);
	input(mat2,row,column);

	printf("\n\n\tMATRIX 1 : ");
	display(mat1,row,column);
	printf("\n\n\tMATRIX 2 : ");
	display(mat2,row,column);


	addition(mat1,mat2,row,column);
	subtraction(mat1,mat2,row,column);

	printf("\n\n\tRESULT OF ADDITION    : ");
	display(sum,row,column);
	printf("\n\n\tRESULT OF SUBTRACTION : ");
	display(sub,row,column);

	return 0;
}

Tell me wats wrong in my code. Urgent!!