How can I write a for loop that will help me run this twice so that I won't have to re-type code for other data type Student variables?

Student S1, S2;


for (int x = 1; x < 3; x++)
    {

    cout << "Enter First Name: " << endl;
    getline(cin,Firstname);
    S1.setFname(Firstname); // I was thinking something like S[x] ????

    cout << "Enter Last Name: " << endl;
    getline(cin,Lastname);
        S1.setLname(Lastname);   // I was thinking something like S[x] ????

    }

Recommended Answers

All 2 Replies

Based on your own idea to use the []-operator (index-operator):

Student students[2];

for(int i = 0; i < 2; i++)
{
cout << "Enter First Name: " << endl;
getline(cin,Firstname);
students.setFname(Firstname);

cout << "Enter Last Name: " << endl;
getline(cin,Lastname);
students.setLname(Lastname);
}

Thanks! I appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.