Hi all,
I tried one program to understand the concept of "Default Arguments" that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).
Here's my program
#include <iostream.h>
#include <stdio.h>
using namespace std;
void justcall(char *c="Parthiban");
//char a[10]; //Global declaration needs for Turbo C++ Compiler
void main()
{
char *ch;
char *accept();
char flag='y';
cout<<"Do u want to enter ur name:[y/n] ";
cin>>flag;
if(flag=='n')
justcall();
else
justcall(ch=accept());
fflush(stdin);
cin.get();
}
void justcall(char *a)
{
cout<<"Hello "<<a<<"!\n";
fflush(stdin);
cin.get();
}
char* accept()
{
char a[10]; //Local declaration
char *p;
cout<<"Please enter ur name: ";
cin>>a;
p=a;
return p;
}
As i mentioned in the comment this program works only when character array ( a[10]) globally declared( for Turbo Compiler) .If i use locally defined array it displays garbage value .
I think it's scope vanished but how it is possible ?
Please tell me what is going on behind the scenes ?
and also clarify this doubt .
I need to call cin.get() twice ( main function justcall)function to temporary halt the program to view the results. Is it not enough to place only in main function(since anyhow the control will be return back to main function) .
Thanks in advance .