Write a program that accepts the name and ID number of 5 students and stores them in an array. After
all the students have been entered, the application should display the title “Student Roster” and then
list the names in the array. 2D Array.

#include <cstdlib>
#include <iostream>
#include <string> 

using namespace std;

int main()
{
    string students[5][3];

    cout << "Enter ID#: ";
    cin >> students[0][0];

    cout << "Enter First name: ";
    cin >> students[0][1];

    cout << "Enter Last name: ";
    cin >> students[0][2];


    cout << "STUENTS ROSTER" << endl;

    cout << students[0][0];

    system("PAUSE");
    return 0;
}

this is what i have so far, i would prefer a loop than to type so much but idk how to do that, can someone please help me?

Recommended Answers

All 13 Replies

Start out by coding a very simple loop

int i;
for(i = 0; i < 5; i++)
{
   // code goes here
}

that goes on line 10, after students declaration. Now move lines 14-18 inside the loop and use the i counter to index into student array, like this:

cin >> students[i][0];

It would be a lot better to use a structure or a class, but you probably have not learned those yet. So arrays like you have them will work.

thank you very much...how do i write the cout statement to print everything?

use a loop very similar to the one I posted above except use cout instead of cin.

keeps crashing :(

post code

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string students[5][3];
    int i;

    for(i = 0; i < 5; i++)
{
    cout << "Enter ID#: ";
    cin >> students[i][0];
    cout << "Enter First name: ";
    cin >> students[i][1];
    cout << "Enter Last name: ";
    cin >> students[i][2];
}
for(i = 0; i < 5; i++)
{
cout << "Students Roster" << endl;
cout << students[i][0];
}    
    system("PAUSE");
    return 0;
}

You need to start a new loop on line 21, you can't reuse the same loop because the value of i counter is now beyond the bounds of the array. So just copy line 12 to between lines 20 and 21.

I dont understand this, making me hate IT now :( loops are the hardest thing.

did it, check

when it runs now it gives the Id number and repeats student roster beside it

Mr. dragon, i think i got it :) it runs fine now i just need to put in some setw n those stuff, thank you for ur help..i appreciate it Big hug! life saver

cout << "Students Roster" << endl;

for(i = 0; i < 5; i++)

{
cout << students[i][0] << students[i][1] << students[i][2] <<endl;
}   
    system("PAUSE");
    return EXIT_SUCCESS;
}

loops are the hardest thing.

Yes they do seem difficult when you are first learning -- But, like most everything else you do, a little practice helps clarify things in your head. Been there, and done that too.

Here's a run of the finished thing...again thanks so much for your help.

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.