I was trying to input strings into a structure I made, but I couldn't succeed. I use Visual C++ 2010 Express Edition.
The code is this:
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
struct schrec
{
char childname[20];
int childage;
};
void main()
{
schrec record[20];
int n;
cout<<"How many students?\n";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter name of student "<<i+1<<endl;
gets(record[i].childname);
cout<<"Enter age of the student\n";
cin>>record[i].childage;
}
cout<<"\nThe record you gave is";
for(int i=0;i<n;i++)
cout<<endl<<record[i].childname<<" ("<<record[i].childage<<")\n";
_getch();
}
The output in Visual C++ is
How many students?
1
Enter name of student 1
Enter age of the student
15
The record you gave is
(15)
But when I compile this in Borland C++ v4.5 (with necessary changes like removing namespace std, system("cls") to clrscr() etc) it works fine.
What can I do to overcome the problem?
Thanks in advance...