简体   繁体   中英

cin.getline() not working as desired

Iam tring to make a simple program that stores cricketers information onto a file and fetches it back but i have problems with cin.getline.When i use cin.getline for my code the name variable is not being read.It just skips the name variable and moves onto the team variable.Every thing functions well when i use cin.

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
using namespace std ;
static int count ;
char format[6][12]={"Test","ODI","T20I","First class","List A","T20"} ;


class profile
{
private:
//General Info
char name[30] ;//Name of player
char team[20] ;//Team the player represents
char role[30] ;//Role of the player in the team
char batstyle[25] ;//Batting Style
char bowlstyle[25] ;//Bowling Style
int matches[6] ;//Number of matches played by the player
int dobd,dobm,doby ;//Date of birth of the player in dd/mm/yyyy format

//Batting stats
int batinns[6] ;//Number of innings played
int notouts[6] ;//Number of not outs
int batruns[6] ;//Number of runs scored
int highsc[6] ;//Highest score
int cen[6] ;//Number of centuries scored
int fif[6] ;//Number of fifties scored
int four[6] ;//Number of fours hit
int six[6] ;//Number of sixes hit
float batave[6] ;//Batting average
float batstr[6] ;//Batting strike rate

//Bowling stats
int bbiruns[6],bbiwkts[6] ;//Best bowling figures in an innings
int bbmruns[6],bbmwkts[6] ;//Best bowling figures in a match
int bowlinns[6] ;//Number of innings played
int bowlruns[6] ;//Number of runs conceeded
int wkts[6] ;//Number of wickets taken
int fivewkts[6] ;//Number of five wicket haults
int tenwkts[6] ;//Number of ten wicket haults
float bowlave[6] ;//Bowling average
float bowlstr[6] ;//Bowling strike rate
float eco[6] ;//Economy of the bowler

//Fielding stats
int catches[6] ;//Number of catches taken
int stumping[6] ;//Number of stumpings done

public: 
//Write functions:
//Function to write the general information 
void setgendata()
{
    cout<<"Enter the full name of the player: "<<endl ;
    cin.getline(name,30) ;
    cout<<"Enter the team represented by the player:"<<endl ;
    cin.getline(team,20) ;
    cout<<"Enter the date of birth in (dd mm yyyy) format:"<<endl ;
    cin>>dobd>>dobm>>doby ;
    cout<<"Enter the role of the player:"<<endl ;
    cin>>role ;
    cout<<"Enter the batting style of the player:"<<endl ;
    cin>>batstyle ;
    cout<<"Enter the bowling style of the player:"<<endl ;
    cin>>bowlstyle ;
}

void setbatdata()
{
    //Function to write batting stats
    for(int k=0;k<6;k++)
    {
    cout<<"Enter the batting details of player in "<<format[k]<<" format"<<endl ;
    cout<<"Enter the number of matches played: "<<endl ;
    cin>>matches[k] ;
    cout<<"Innings played:"<<endl ;
    cin>>batinns[k] ;
    cout<<"Not outs:"<<endl ;
    cin>>notouts[k] ;
    cout<<"Runs :"<<endl ;
    cin>>batruns[k] ;
    cout<<"High score:"<<endl ;
    cin>>highsc[k] ;
    cout<<"Centuries :"<<endl ;
    cin>>cen[k] ;
    cout<<"Fifties :"<<endl ;
    cin>>fif[k] ;
    cout<<"Fours :"<<endl ;
    cin>>four[k] ;
    cout<<"Sixes :"<<endl ;
    cin>>six[k] ;
    cout<<"Average :"<<endl ;
    cin>>batave[k] ;
    cout<<"Strike rate:"<<endl ;
    cin>>batstr[k] ;
    }
}
void setbowldata()
{
    //Function to write bowling stats
    for(int k=0;k<6;k++)
    {
    cout<<"Enter the bowling details of player in "<<format[k]<<" format"<<endl ;
    cout<<"Innings played:"<<endl ;
    cin>>bowlinns[k] ;
    cout<<"Runs:"<<endl ;
    cin>>bowlruns[k] ;
    cout<<"Wickets:"<<endl ;
    cin>>wkts[k] ;
    cout<<"Best bowling figures in an inning <runs wkts>:"<<endl ;
    cin>>bbiruns[k] ;
    cin>>bbiwkts[k] ;
    cout<<"Best bowling figures in an match <runs wkts>:"<<endl ;
    cin>>bbmruns[k] ;
    cin>>bbmwkts[k] ;
    cout<<"Five wicket haults:"<<endl ;
    cin>>fivewkts[k] ;
    cout<<"Ten wicket haults:"<<endl ;
    cin>>tenwkts[k] ;
    cout<<"Average:"<<endl ;
    cin>>bowlave[k] ;
    cout<<"Strike rate:"<<endl ;
    cin>>bowlstr[k] ;
    cout<<"Economy:"<<endl ;
    cin>>eco[k] ;
    }
}
void setfielddata()
{
    //Function to set fielding stats
    for(int k=0;k<6;k++)
    {
    cout<<endl<<"Enter the fielding stats of the player in "<<format[k]<<" format" ;
    cout<<"Catches:"<<endl ;
    cin>>catches[k] ;
    cout<<"Stumpings:"<<endl ;
    cin>>stumping[k] ;
    }
}   
    //Function to print general information
void getgendata()
{
    cout<<endl<<"GENERAL INFORMATION:"<<endl ;
    cout<<"Full Name: "<<name<<endl ;
    cout<<"Team: "<<team<<endl ;
    cout<<"Date of birth (dd/mm/yyyy): "<<dobd<<"/"<<dobm<<"/"<<doby<<endl ;
    cout<<"Role of the player: "<<role<<endl ;
    cout<<"Batting style: "<<batstyle<<endl ;
    cout<<"Bowling style: "<<bowlstyle<<endl ;
}
void getbatdata(int i,int j)
{
    //Function to print batting information
    cout<<endl<<"BATTING INFORMATION:"<<endl ;
    for(int k=i;k<j;k++)
    {
    cout<<endl<<"Batting details of player in "<<format[k]<<" format"<<endl ;
    cout<<"Matches: "<<matches[k]<<endl ;
    cout<<"Innings played: "<<batinns[k]<<endl ;
    cout<<"Not outs: "<<notouts[k]<<endl ;
    cout<<"Runs: "<<batruns[k]<<endl ;
    cout<<"High score: "<<highsc[k]<<endl ;
    cout<<"Centuries: "<<cen[k]<<endl ;
    cout<<"Fifties: "<<fif[k]<<endl ;
    cout<<"Fours: "<<four[k]<<endl ;
    cout<<"Sixes: "<<six[k]<<endl ;
    cout<<"Average: "<<batave[k]<<endl ;
    cout<<"Strike rate: "<<batstr[k]<<endl ;
    }
}
void getbowldata(int i,int j)
{
    //Function to write bowling stats
    cout<<endl<<"BOWLING INFORMATION:"<<endl ;
    for(int k=i;k<j;k++)
    {
    cout<<endl<<"Bowling details of player in "<<format[k]<<" format"<<endl ;
    cout<<"Matches: "<<matches[k]<<endl ;
    cout<<"Innings played: "<<bowlinns[k]<<endl ;
    cout<<"Runs: "<<bowlruns[k]<<endl ;
    cout<<"Wickets: "<<wkts[k]<<endl ;
    cout<<"Best bowling figures in an innings <runs/wkts>: "<<bbiruns[k]<<"/"<<bbiwkts[k]<<endl ;
    cout<<"Best bowling figures in an match <runs/wkts>: "<<bbmruns[k]<<"/"<<bbmwkts[k]<<endl ;
    cout<<"Five wicket haults: "<<fivewkts[k]<<endl ;
    cout<<"Ten wicket haults: "<<tenwkts[k]<<endl ;
    cout<<"Average: "<<bowlave[k]<<endl ;
    cout<<"Strike rate: "<<bowlstr[k]<<endl ;
    cout<<"Economy: "<<eco[k]<<endl ;
    }
}
void getfielddata(int i,int j)
{
//Function to write fielding stats
    cout<<endl<<"FIELDING STATS:"<<endl ;
    for(int k=i;k<j;k++)
    {
    cout<<endl<<"Fielding stats of the player in "<<format[k]<<" format"<<endl ;
    cout<<"Catches: "<<catches[k]<<endl ;
    cout<<"Stumpings: "<<stumping[k]<<endl ;
    }
//Saving data
}
void storedata()
{
    ofstream outputFile ;
    outputFile.open("Data.dat",ios::app) ;
    //Saving general info
    outputFile<<name<<endl ;
    outputFile<<team<<endl ;
    outputFile<<dobd<<endl ;
    outputFile<<dobm<<endl ;
    outputFile<<doby<<endl ;
    outputFile<<role<<endl ;
    outputFile<<batstyle<<endl ;
    outputFile<<bowlstyle<<endl ;
    //Saving batting info all formats
    for(int k=0;k<6;k++)
    {
        outputFile<<matches[k]<<endl ;
        outputFile<<batinns[k]<<endl ;
        outputFile<<notouts[k]<<endl ;
        outputFile<<batruns[k]<<endl ;
        outputFile<<highsc[k]<<endl ;
        outputFile<<cen[k]<<endl ;
        outputFile<<fif[k]<<endl ;
        outputFile<<four[k]<<endl ;
        outputFile<<six[k]<<endl ;
        outputFile<<batave[k]<<endl ;
        outputFile<<batstr[k]<<endl ;
    }
    for(int k=0;k<6;k++)
    {
        outputFile<<bowlinns[k]<<endl ;
        outputFile<<bowlruns[k]<<endl ;
        outputFile<<wkts[k]<<endl ;
        outputFile<<bbiruns[k]<<endl ;
        outputFile<<bbiwkts[k]<<endl ;
        outputFile<<bbmruns[k]<<endl ;
        outputFile<<bbmwkts[k]<<endl ;
        outputFile<<fivewkts[k]<<endl ;
        outputFile<<tenwkts[k]<<endl ;
        outputFile<<bowlave[k]<<endl ;
        outputFile<<bowlstr[k]<<endl ;
        outputFile<<eco[k]<<endl ;
    }

    for(int k=0;k<6;k++)
    {
        outputFile<<catches[k]<<endl ;
        outputFile<<stumping[k]<<endl ;
    }
    outputFile.close() ;
}
//Reading data
void fetchdata(char temp[])
{
    ifstream inFile ;
    inFile.open("Data.dat") ;
    //Fetching general info
    inFile>>name ;
    while(strcmp(temp,name)!=0)
    {
        inFile>>name ;
    }
    inFile>>team ;
    inFile>>dobd ;
    inFile>>dobm ;
    inFile>>doby ;
    inFile>>role ;
    inFile>>batstyle ;
    inFile>>bowlstyle ;

    //Fetching batting info-all formats
    for(int k=0;k<6;k++)
    {
        inFile>>matches[k] ;
        inFile>>batinns[k] ;
        inFile>>notouts[k] ;
        inFile>>batruns[k] ;
        inFile>>highsc[k] ;
        inFile>>cen[k] ;
        inFile>>fif[k] ;
        inFile>>four[k] ;
        inFile>>six[k] ;
        inFile>>batave[k] ;
        inFile>>batstr[k] ;
    }
    //Fetching bowling info-all formats
    for(int k=0;k<6;k++)
    {
        inFile>>bowlinns[k] ;
        inFile>>bowlruns[k] ;
        inFile>>wkts[k] ;
        inFile>>bbiruns[k] ;
        inFile>>bbiwkts[k] ;
        inFile>>bbmruns[k] ;
        inFile>>bbmwkts[k] ;
        inFile>>fivewkts[k] ;
        inFile>>tenwkts[k] ;
        inFile>>bowlave[k] ;
        inFile>>bowlstr[k] ;
        inFile>>eco[k] ;
    }
    //Fetching fielding info-all formats
    for(int k=0;k<6;k++)
    {
        inFile>>catches[k] ;
        inFile>>stumping[k] ;
    }
    inFile.close() ;
}
} ;

int main()
    {
int search ;
char temp[20] ;
profile s;
int choice ;
char res ;
cout<<"Select an option:"<<endl ;
cout<<"1: Create new profile"<<endl<<"2: Display existing profile"<<endl ;
cout<<"Enter your choice: " ;
cin>>choice ;
switch(choice)
{
case 1:
    s.setgendata() ;
    s.setbatdata() ;
    s.setbowldata() ;
    s.setfielddata() ;
    s.storedata() ;
    break ;
case 2:
    cout<<"Enter name of the player: " ;
    cin>>temp ;
    s.fetchdata(temp) ;
    cout<<"Select an option:\n1:Display full profile\n2:Display profile for an aspect\n3:Display profile in a format\n" ;
    int ch ;
    cout<<"Enter your choice: " ;
    cin>>ch ;
    switch(ch)
    {
    case 1:
        s.getgendata() ;
        s.getbatdata(0,6) ;
        s.getbowldata(0,6) ;
        s.getfielddata(0,6) ;
        break ;
    case 2:
        cout<<"Select an aspect:\n1:General\n2:Batting\n3:Bowling\n4:Fielding\n" ;
        int wish ;
        cin>>wish ;
        switch(wish)
        {
            case 1:
                s.getgendata() ;
                break ;
            case 2:
                s.getbatdata(0,6) ;
                break ;
            case 3:
                s.getbowldata(0,6) ;
                break ;
            case 4:
                s.getfielddata(0,6) ;
                break ;
            default:
                cout<<"Invalid choice"<<endl ;
                main() ;    
                break ;
        }
        break ;
    case 3:
        cout<<"Select a format\n1:Test\n2:ODI\n3:T20I\n4:Fist Class\n5:List A\n6:T20\n" ;
        int wish2 ;
        cin>>wish2 ;
        if(wish2==1||wish2==2||wish2==3||wish2==4||wish2==5||wish2==6 )
        {
            s.getgendata() ;
            s.getbatdata(wish2-1,wish2) ;
            s.getbowldata(wish2-1,wish2) ;
            s.getfielddata(wish2-1,wish2) ;
        }
        else
        {
            cout<<"Invalid choice"<<endl ;
            main() ;
        }
    }
}
cout<<endl<<endl<<"'Q' to quit or any other key to continue : " ;
cin>>res ;
if(res=='q' || res=='Q')
{
    return 0 ;
}  
else
{
    main() ;
}

}

You really need to understand what you're doing to mix reading lines with reading things other than lines. What's happening is that for bowlstyle , you're entering something following by a line ending. So cin>>bowlstyle; grabs whatever you enetered, leaving an empty line for the next getline to eat. You need to read the line ending after reading in the bowlstyle . A cin.ignore() will do it, but really you should design your input code more sensibly in the first place.

Without looking much into the exact code, I suggest you to use std::string

std::getline(std::cin,input); //input is of type std::string

Make sure to use cin.ignore(std::numeric_limits<streamsize>::max(), '\\n'); before every call to getline after std::cin>>

Ref :- ignore , std::getline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM