Github Support Community for C++ Developers

A support communnity for C++ developers like me allways looking foreword for some help from other developers.

Ask quuestions on this topic and share it with other developers.

The #software-development category would be the right place if you’d like to discuss programming issues on this forum. I hope that helps!

1 Like

Can Anyone helpp me with this c++ code.

#include <iostream>
#include <string>

using namespace std;

struct test{
    string name;
    int age;

int main(){
    cout << "No of students:\t";
    int entries;
    cin >> entries;
    cout << "Enter the data" << endl;
    test * data = new test[entries];
    cout << "Enter Name: ";
    cout << "Enter Age: ";
    cin >> data[0].age;
    return 0;    

The code implements a structure named test which holds a string and an int.
The main function declares an array of pointer to test named details with new keyword
{test * data = new test[entries];}
I am trying to take inputs but faceing these problems:-
I am unable to enter name which is a string type.
It directly jumps to age input.

cin and getline don’t play nice together. cin leaves a newline in the input buffer, which causes your getline to fail.

You could just use cin instead of getline to populate name or you could call cin.ignore() just before calling getline.

See here.

1 Like

Thank you For your Help