0

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] ????

    }

Edited by pyTony: fixed formatting

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ableg02
0

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);
}

Edited by mbulow: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.