-3

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 :lisa2@hotmail.com

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 :lisa2@hotmail.com

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();
}

PolarToCartesian
  • 744
  • 7
  • 16
  • Please indent your code for better legibility. – 0_0perplexed Oct 29 '20 at 10:46
  • @ThomasSablik no it doesn't match the thing I need – Sheikh Araf Oct 29 '20 at 10:55
  • The solution described in the dupe is: call `std::cin.ignore(std::numeric_limits::max(), '\n')` after all `cin>>...;` – Thomas Sablik Oct 29 '20 at 11:19
  • and phone number wasn't part of the question either. – Sheikh Araf Oct 29 '20 at 11:35
  • _"and phone number wasn't part of the question either"_ Then you shouldn't post it in your [mcve] – Thomas Sablik Oct 29 '20 at 11:43
  • @ThomasSablik I read the link or not how do you know? I read the link twice. I couldn't understand and match the problem that's what I get. and about the phone number I posted because I wasn't able to find where I was doing mistakes I also mentioned that. I think this was not the part where was the mistake I can assume at least. Whatever thanks for your so useful informations and vote. Good Luck! – Sheikh Araf Oct 29 '20 at 12:14
  • The title of the dupe is _"Why does std::getline() skip input after a formatted extraction? "_ and the question is why `std::getline` after `std::cin` doesn't work. In your code all `std::getline` after `std::cin` doesn't work. You had to read only the title and 10 lines of code to see that this is exactly your problem. – Thomas Sablik Oct 29 '20 at 12:19
  • @ThomasSablik my problem wasn't about why ```std::getline``` after ```std::cin``` doesn't work. My problem was getline() wasn't working on some certain situations. if you can see my output.it works after ```cin``` sometimes and sometimes not so how do you want me to explain with the link's explanation where it didn't work anytime when ```cin``` and ```getline``` used? – Sheikh Araf Oct 29 '20 at 12:23
  • That's the problem with this question and the accepted answer. You still don't understand the problem and probably will repeat it. Look again at your output. **All** `std::geline` directly after `std::cin` are skipped. That's because `std::cin` is a formatted read and doesn't remove the newline. `std::getline` is an unformatted read and reads the previous newline after a formatted read. `getline(cin,stu.firstName);` is skipped after `cin>>choice;`, `getline(cin,stu.Section);` is skipped after `cin>>stu.Roll;`, `getline(cin,stu.Department);` is skipped after `cin>>stu.Semester;`, ... – Thomas Sablik Oct 29 '20 at 12:31
  • okay. I didn't understand the full concept from that link now how can I understand and what is the main solution? – Sheikh Araf Oct 29 '20 at 14:20
  • The answer https://stackoverflow.com/a/21567292/4645334 is divided into multiple sections. There is a basic explanation for beginners, an in-depth explanation that you can skip and multiple solutions. You should read it until you understand it. If you don't understand something you should ask. I already gave you a short version in my comment: _"The solution described in the dupe is: call `std::cin.ignore(std::numeric_limits::max(), '\n')` after all `cin>>...;`"_ – Thomas Sablik Oct 30 '20 at 07:28

2 Answers2

0
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.

PolarToCartesian
  • 744
  • 7
  • 16
0

Another possibillity would be to change all getLine() fucntions to cin>>stu... calls.

The error occurs, because the cin method doas not take the \n out of the input buffer. The following getLine takes the remaining \n out of the input Buffer and instantly terminates.

dawnb
  • 86
  • 3