-2

Code:

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

using namespace std;

struct student
{
    char name[20];
    int id;
    float cgpa;
};

int main()
{   
    student s[100];

    int n,i;
    cout<<"Enter number of students: ";
    cin>>n;
    cout<<"Enter records of "<<n<<" students"<<endl;

    for(i=0; i<n ; i++)
    {   
        cout<<"Enter name: ";           
        gets(s[i].name);
        cout<<"Enter ID: ";
        cin>>s[i].id;
        cout<<"Enter CGPA: ";
        cin>>s[i].cgpa;
        cout<<endl;
    }   

    for(i=0; i<n ; i++)
    {
        cout<<"\nName: "<<s[i].name;
        cout<<"\nID: "<<s[i].id;
        cout<<"\nCGPA: "<<s[i].cgpa<<endl;
    }   
}

output:

Enter number of students: 2
Enter records of 2 students

Enter name: Enter ID: 

using sublime text 3 c++

mch
  • 9,424
  • 2
  • 28
  • 42
  • 4
    There are quite a lot of problems in your code. The biggest is that you don't use C++ functions to read all input into C++ strings, but instead `char` arrays and the [***dangerous***](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) (and no longer standard C or C++) function `gets`. I suggest you get [a few good C++ books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and almost start over from the beginning. – Some programmer dude Oct 25 '18 at 06:31
  • What is the error/problem? – Jaques Oct 25 '18 at 06:31
  • As for your problem: You ended the last input (the number `2`) with the `Enter` key, right? That `Enter` key will be put into the input buffer as a newline `'\n'`. Now think about what happens when you call `gets` and the first character it reads is the newline. – Some programmer dude Oct 25 '18 at 06:35
  • @some programmer dude if i add fflush(stdin) before gets, even then it doesn't work. I'm supposed to use these commands as instructed by our teacher. He's teaching using codeblocks. – Aman Yadav Oct 25 '18 at 06:51
  • 1
    And that is explicitly mentioned in the standards as *undefined behavior*. Passing an input-only stream to `fflush` is not supported in the standards, but some systems implement it as an *extension*. Don't do it though. And really, stop using `char` arrays and most importantly stop using `gets`. – Some programmer dude Oct 25 '18 at 06:54
  • Alright man. Thanks a ton – Aman Yadav Oct 25 '18 at 07:06

2 Answers2

1

Use <string> for name instead of a character array, then use the cin as usual, instead of gets, to read in the string.

awiebe
  • 3,758
  • 4
  • 22
  • 33
0

As pointer out by Some Programmer Dude, the enter you pressed is put into the input buffer as a newline '\n' and then later accepted by get() causing it to skip to the next input.

There are two ways you can approach this problem :

  1. Use cin.ignore() at the start of the loop where you take the input.

  2. The better choice will be to get rid of gets(). It has been deprecated since c++14. Secondly, replace the character array with a string.

So the structure would see a modification string name;

And simply do cin>>s[i].name; in the loop.

Thats it.

Also, its preferable to use cstdio instead of stdio.h.

arjunkhera
  • 929
  • 6
  • 23
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – arjunkhera Oct 25 '18 at 07:26