Search code examples
c++stringc++11structure

Why some of the string variable not taking inputs from structure to void function?


In the below code some of the string is taking inputs but some are just skipping. I checked everything didn't discover anything. It would be ideal if you let me know where I am fouling up.I need to mention that I have a lot more work to do on this problem that's why I created functions

Expected Output

:::::::::::::::::::::::::: Choose Option From Below ::::::::::::::::::::::::::

1.for add new student

2.for add new teacher

3.for add new notice

1

Enter student's first name : Marry

Enter student's last name :lisa

Enter student's roll no :245

Enter student's section : C

Enter student's year :1

Enter student's semester :2

Enter student's department : IR

Enter student's email :[email protected]

Enter student's phone :15648955

Enter student's address : 2/A XYZ street

Output Now

:::::::::::::::::::::::::: Choose Option From Below ::::::::::::::::::::::::::

1.for add new student

2.for add new teacher

3.for add new notice

1

Enter student's first name :Enter student's last name :lisa

Enter student's roll no :245

Enter student's section :Enter student's year :1

Enter student's semester :2

Enter student's department :Enter student's email :[email protected]

Enter student's phone :15648955

Enter student's address :

Process returned 0 (0x0) execution time : 52.725 s Press any key to continue.

#include <iostream>
#include<string>
#include<iomanip>
using namespace std;

struct student{
    string firstName;
    string lastName;
    int Roll;
    string Section;
    int Year;
    int Semester;
    string Department;
    string Email;
    int Phone;
    string Address;
};

void student_part(void){
    struct student stu;
    cout<<"Enter student's first name :";
    getline(cin,stu.firstName);
    cout<<"Enter student's last name :";
    getline(cin,stu.lastName);
    cout<<"Enter student's roll no :";
    cin>>stu.Roll;
    cout<<"Enter student's section :";
    getline(cin,stu.Section);
    cout<<"Enter student's year :";
    cin>>stu.Year;
    cout<<"Enter student's semester :";
    cin>>stu.Semester;
    cout<<"Enter student's department :";
    getline(cin,stu.Department);
    cout<<"Enter student's email :";
    getline(cin,stu.Email);
    cout<<"Enter student's phone :";
    cin>>stu.Phone;
    cout<<"Enter student's address :";
    getline(cin,stu.Address);
}



void add_info(void){
    int choice;
    cout<<"::::::::::::::::::::::::::"<<endl;
    cout<<" Choose Option From Below"<<endl;
    cout<<"::::::::::::::::::::::::::"<<endl;
    cout<<"\n1.for add new student"<<endl;
    cout<<"2.for add new teacher"<<endl;
    cout<<"3.for add new notice"<<endl;
    cin>>choice;
    if(choice==1){
        student_part();
    }
}


int main()
{
    add_info();
}


Solution

  • cin>>stu.Phone;
    

    Is fetching an integer from the command line so it doesn’t read the \n character at the end which getline reads immediately after.

    Before getline, write fgetc(stdin) to read the newlîne.