Error Vis

Hey Greeting everyone

I am getting an unusual error in my following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>
using namespace std;
class caste{
char name[100];
char achieve[500];
int rank;
public:
void getinfo(){
cout<<"Enter Name :";
gets(name);

}
void geta()
{cout<<"Enter Achive :";
gets(achieve);}
void getr(){
	cout<<"Enter rank :";
	cin>>rank;}

};

int main()
{caste khan[10];
for(int i=0;i<5;++i)
{cout<<"Enter info about Khan no. "<<i<<" now : ";
khan[i].getinfo();
khan[i].geta();
khan[i].getr();
}
return 0;
}


the problem is that after 1st member(khan) information is entered thn afterards it starts skipping the "name" part of every remaining member.
It just asks for achievement and rank.
any help ? btw i use visual c++ 2010
Line 21 reads the next integer and leaves everything after the int string untouched. That means that if you enter a number and press enter it only reads the number but the new line character is still there. gets reads until it finds a new line character so when you call gets the next time it will find the new line character that was left over right away making the string empty. One way to solve this is to place something like std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after cin>>rank;. What this ignore call is doing is that it discards everything until it finds the new line character. The new line character is discarded too so this will place you on a new fresh line.
thanx a lot!!! :) really thanx learnt new thing thanks :D
Topic archived. No new replies allowed.