string first character skips

string skips first character when I cout it..for example if I input string like this

string n;
getline(cin, n);

and then if input this ->>>>> "MOHSIN"
and then I getting output it skips first character

cout<<n;

it show me like this->>>>> OHSIN
it skips 'M'

what is this problem..??
The first character is probably consumed by another piece of code that you have not shown posted here.
then how can i fix it..??
i face problem in this program which given below.....



HEADER

#include <iostream>


class addresstype
{
private:
std::string street, state, city;
int zip_code;
public:
addresstype();
void setd();
void printd() const;
};

class extpersontype
{
private:
std::string firstname;
std::string lastname;
std::string persontype[3];
std::string person_contact;
public:
extpersontype();
void setd();
void printd() const;
};

class dateType
{
private:
int day;
int month;
int year;
public:
dateType();
void setdate();
int getmonth() const;
void printdate() const;
};


class addressBookType : public addresstype, public extpersontype, public dateType
{
public:
void setd();
void printd() const;
};


IMPLEMENTATION

#include <iostream>
#include <string>

#include "Header.h"

using namespace std;

////////// ADDRESSTYPE CLASS \\\\\\\\\\

addresstype::addresstype()
{
street = "0";
state = "";
city = "--";
zip_code = 0;
}

void addresstype::setd()
{
cout<<"Enter Street adress : ";
cin.ignore();
getline(cin,street);

cout<<"Enter State : ";
cin.ignore();
getline(cin,state);

cout<<"Enter City : ";
cin.ignore();
getline(cin,city);

cout<<"Enter Zip Code : ";
cin>> zip_code;
}

void addresstype::printd() const
{
cout<<"Street : "<<street<<endl;
cout<<"State : "<<state<<endl;
cout<<"City : "<<city<<endl;
cout<<"Zip COde : "<<zip_code<<endl;
}


////////// EXTPERSONTYPE CLASS \\\\\\\\\\

extpersontype::extpersontype()
{
firstname = "";
lastname = "";
persontype[0] = "-";
persontype[1] = "-";
persontype[2] = "-";
person_contact = "99999999";
}

void extpersontype::setd()
{
cout<<"Enter First Name : ";
cin.ignore();
getline(cin,firstname);

cout<<"Enter Last Name : ";
cin.ignore();
getline(cin,lastname);

cout<<"Enter Reference Person "<<endl<<endl;

cout<<"Enter Family Member (if Any) otherwise Enter ' - ' ";
cin.ignore();
getline(cin,persontype[0]);

cout<<"Enter Friend (if Any) otherwise Enter ' - ' ";
cin.ignore();
getline(cin,persontype[1]);

cout<<"Enter Business Associate (if Any) otherwise Enter ' - ' ";
cin.ignore();
getline(cin,persontype[2]);

cout<<"Enter Conatact Number : ";
cin.ignore();
getline(cin,person_contact);
}

void extpersontype::printd() const
{
cout<<"Name : "<<firstname<<" "<<lastname<<endl<<endl;
cout<<"Reference Person "
<<endl<<endl
<< "Family Member : "<<persontype[0]<<endl
<< "Friend : "<<persontype[1]<<endl
<< "Business Associate : "<<persontype[2]<<endl<<endl;
cout<<"Contact Number : "<<person_contact<<endl<<endl;
}

////////// DATETYPE CLASS \\\\\\\\\\

dateType::dateType()
{
day = 0;
month = 0;
year = 0;
}

void dateType::setdate()
{
cout<<"Set Date-of-Birth"<<endl<<endl;
int d = 0, m = 0, y = 0;
do
{
cout<<"Enter Day : ";
cin>>day;
if(day >= 1 && day <= 31)
d = 1;
else
cout<<"Enter Day Between 1-30 "<<endl;
}while (d != 1);

do
{
cout<<"Enter Month : ";
cin>>month;
if(month >= 1 && month <= 12)
m = 1;
else
cout<<"Enter Month Between 1-12 "<<endl;
}while (m != 1);

do
{
cout<<"Enter Year : ";
cin>>year;
if(year >= 1960 && year <= 2000)
y = 1;
else
cout<<"Enter Year Between 1960-2000 "<<endl;
}while (y != 1);
}

int dateType::getmonth() const
{
return month;
}

void dateType::printdate() const
{
cout<<"Date-of-Birth : "<<day<<"-"<<month<<"-"<<year<<endl<<endl;
}

////////// ADDRESSBOOKTYPE CLASS \\\\\\\\\\

void addressBookType::setd()
{
addresstype::setd();
extpersontype::setd();
dateType::setdate();
}

void addressBookType::printd() const
{
addresstype::printd();
extpersontype::printd();
dateType::printdate();
}

MAIN

#include <iostream>
#include <string>
#include <windows.h>

#include "Header.h"

using namespace std;

#define SIZE 500

int main()
{
string serchname;
int n;
addressBookType adbook[SIZE];

/////////////////-------------->>>>>>>>> INPUT
cout<<endl<<endl<<" *** Enter Data ***"<<endl<<endl
<<"Enter Number of entries do you want to Enter : ";
cin>>n;

for (int i=0;i<n;i++)
{
cout<<"Enter entry No. "<<i+1<<endl<<endl<<endl;
adbook[i].setd();
cout<<endl<<endl<<endl;
}
system("pasue");
system("CLS");
cout<<endl<<endl<<"***** Data Entry Completed *****"<<endl;
system ("pasue");
system ("CLS");


/////////////----------->>>>>>>>> OUTPUT

cout<<endl<<endl<<" *** Output ***"<<endl<<endl;
for(int i=0;i<n;i++)
{
cout<<endl<<endl<<"Output # "<<i+1<<endl<<endl;
adbook[i].printd();
}

system("pause");
system("CLS");

for(int i=5; i>0;i--)
{
cout<<endl<<endl<<endl<<endl<<" Your Program Will Close in " <<i<<" sec ."<<endl;
Sleep(400);
system("CLS");
cout<<endl<<endl<<endl<<endl<<" Your Program Will Close in " <<i<<" sec .."<<endl;
Sleep(400);
system("CLS");
cout<<endl<<endl<<endl<<endl<<" Your Program Will Close in " <<i<<" sec ...";
Sleep(400);
system("CLS");
}




return 0;
}
The problem is that you call cin.ignore() before each call to getline.

Instead you should call cin.ignore() after each time you have read from cin using the >> operator.

Or instead of using cin.ignore() you can use ws (http://en.cppreference.com/w/cpp/io/manip/ws) before each call to getline.

 
getline(cin >> ws, str);

I can strongly recommend this way because it's very easy to use correctly and it also has the following advantages compared to using cin.ignore():
– If the user press enter before inputting anything it will keep waiting for input just like using the >> operator would.
– There is no problem if the user has inputted additional whitespace characters on the previous input line.
– Any whitespace characters at the beginning of the line are ignored and will not be part of the string.
thanks..now i can get the point. thank you for helping me out.... :-)
Registered users can post here. Sign in or register to post.