Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have written a program c++ However near the end of the program i stumbled upon

ID: 3690931 • Letter: I

Question

I have written a program c++

However near the end of the program i stumbled upon a problem that i dont know how to fix.

In here I have made a VERY simple piece of code which includes the problem (easier to view instead of 10++ pages of code).

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

struct person {
    string name;
    int age;
};


int main(void){

    const int size = 3;
    person p1;

    for (int i = 1; i < size; i++) {
       
        cout << "Input name: ";
        getline(cin, p1.name);
       
        cout << " Input Age: " << endl;
        cin >> p1.age;
    }

}

In here the first time i run p1.xx it runs fine... the loop i made is suppost to run 2 times only (hence the i from 1 to 3)

(in the actual program the loop runs thousands of time)

My problem is that i need a way to get p1 to become p2, p3 and so on... using p[i] naturally wont work, so i have absolutely no idea how to fix this loop issue

Explanation / Answer

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

struct person {
   string name;
   int age;
};


int main(void){

   const int size = 3;
   person p[size];

   for (int i = 1; i < size; i++) {

       cout << "Input name: ";
       getline(cin, p[i].name);

       cout << " Input Age: " << endl;
       cin >> p[i].age;
       cin.ignore();
   }

}