0

what is wrong with this...............????????????

hmmm its not properly functioning.................??????????????? #include<iostream> using namespace std; struct students{ char firstName[40]; char lastName[40]; char course[30]; char rollNo[10]; char grade[1]; int age; float gpa; }; int main() { students data[10]; for(int i=0;i<10;i++) { cout<<"Enter the first name:"; cin.getline(data[i].firstName,40); cout<<"Enter the last name:"; cin.getline(data[i].lastName,40); cout<<"Enter the Course:"; cin.getline(data[i].course,30); cout<<"Enter the Roll Number:"; cin.getline(data[i].rollNo,10); cout<<"Enter the grade:"; cin.getline(data[i].grade,1); cout<<"Enter the age:"; cin>>data[i].age; cout<<"Enter your GPA:"; cin>>data[i].gpa; } }

12th Jan 2019, 2:46 PM
Aamir Mehmood
3 Respostas
+ 4
char grade[1]; should be changed into one of the following: 1) char grade; // Not an array, but just a single char 2) char grade[2]; // If it is a char array you need space to accommodate for the null character. Additionally if you call getline after a regular cin you need to place cin.ignore() between the 2, which is what happens at the end of the for loop. This is because cin leaves a newline in the input stream which getline picks up on and skips asking for input. On another note, I recommend that, instead of using char arrays, you use std::string which are alot more dynamic. This is C++ after all, not C.
12th Jan 2019, 3:15 PM
Dennis
Dennis - avatar
+ 2
Aamir Mehmood In the last line inside the for loop. for(...) { ... cout << "Enter your GPA:"; cin >> data[i].gpa; cin.ignore(); } http://www.cplusplus.com/reference/istream/istream/ignore/
14th Jan 2019, 8:48 AM
Dennis
Dennis - avatar
0
Dennis sir where and how to use cin.ignore()
14th Jan 2019, 1:21 AM
Aamir Mehmood