here i am giving some tips for all those who are new to visual c++ 2008 and have decided to ditch turbo c++.if you are finding it difficult to run your old turbo c programs on visual c++ compiler,here are some tips to solve those frustrating compatibility issues.
the following tips originally suggested by bharat m urs. credit goes to him.
- clrscr():
In turbo c,you had to include <conio.h> header.but in visual c++ it wont work.you have to include windows.h header.and system(“cls”); is the replacement code for clrscr()
2.iostream
the right way of declaring iostream.h header is “#include <iostream>” and not #include<iostream.h>.other header files like stdio,conio works alike.
3.cout,cin,endl commands
to use these commands you have to namespace std.
the following source code of nqueen’s problem can get you a better idea.
4.getch()
use _getch() instead of getch()
more tips coming soon…
example program…….
/*nqueens*/
#include<iostream>
#include<conio.h>
#include<windows.h>
int count;
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int abs(int n)
{
if(n<0)
n=-n;
return n;
}
int canplace(int k,int c[])
{
int i;
for(i=0;i<k;i++)
{
if(c[i]==c[k] || abs(c[i]-c[k])==abs(i-k))
return 0;
}
return 1;
}
void display(int n,int c[])
{
int i,j;
count++;
char q[100][100];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
q[i][j]=’*';
for(i=0;i<n;i++)
q[i][c[i]]=’Q';
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<q[i][j]<<” “;
}
cout<<endl;
}
}
void queen(int n)
{
int k,c[100];
c[0]=-1;
k=0;
while(k>=0)
{
c[k]++;
while(c[k]<n && !canplace(k,c))
c[k]++;
if(c[k]<n)
{
if(k==n-1)
{
cout<<”\nSolution Number “<<count+1<<” is\n”;
display(n,c);
cout<<”\n”;
}
else
{
k++;
c[k]=-1;
}
}
else
k–;
}
if(count==0)
{
cout<<”\nNo Solution !!”<<endl;
}
}
void main()
{
int n;
system(“cls”);
cout<<”\nEnter the number of queens : “; cin>>n;
queen(n);
cout<<”\n\nThere are “<<count<<” solutions”;
_getch();
}
2 Comments
hey we can use “getch()” as well…
using getch() gives a warning.so _getch() complies clean!!
ya like bharath said _getch() is not mandatory.