Help With C++ (arrays)

I need to create a program that will diplay a students schedule once you input the ID number for that student.
There will be 5 arrays in this program
(array 1) Enter 5 Students Names
(array 2) Enter 5 ID#s
(array 3) Enter 3 subjects for each
Enter subject 1 for each
Enter subject 2 for each
Enter subject 3 for each
(array 4) Enter a room for each student & each subject
(array 5) Enter a class time for each subject

So pretty much when all array are filled you are asked to enter a student ID# and it will display a schedule for that student & students name

This is what i have so far but im stumpped

//School Schedule

#include<iostream>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

int main()
{
char students[30];
int ID[5];
int time[5];
char subject[5];
int x=1;

while (x<=5)
{
cout<<"Enter Student Names ";
cin>>students[x];

x=x+1;
}
x=1;

while (x<=5)
{
cout<<"Enter Student ID's ";
cin>>ID[x];

x=x+1;
}
x=1;

while (x<=5)
{
cout<<"Enter class times ";
cin>>time[x];

x=x+1;
}
x=1;

while (x<=5)
{
cout<<"Enter classes ";
cin>>subject[x];

x=x+1;
}
x=1;


while (x<=5)
{
cout<<students[x];
cout<<"\n";
cout<<ID[x];
cout<<"\n ";
cout<<time[x];
cout<<"\n ";
cout<<subject[x];
cout<<"\n ";

x=x+1;
}
cout<<"End Of Program ";
_getch();


return 0 ;
}

Do you mean to store schedules for more than one student? Because:
1
2
3
4
char students[30];  // One student's name with 30 letters
int ID[5];          // 5 IDs
int time[5];        // 5 times
char subject[5];    // One subject name with 5 letters 

You confuse array of characters with array of "student's schedules"

1
2
3
4
5
6
7
int x=1; // Why 1 not 0? Arrays start at index 0!
while (x<=5)
{
    cout<<"Enter Student Names ";
    cin>>students[x];   // Fill x'th letter of student's name with what user types
    x=x+1;
}

You should really use array of struct objects: http://www.cplusplus.com/doc/tutorial/structures/
Last edited on
honestly to tell you the truth thats how the instructions were written if you can help me in any way please do. I believe I had a bad teacher for c++ and I want to switch colleges so I can recieve better training on this. in the end I need to use arrays and as long as my final ouput is the schedule of a student matching the id number of that student with their name is all I need I will read the page where.the link sends me as well
thank you
Use of arrays in such assignment is by all means ok, but you need to pay attention to what you put into this arrays, what kind of similar objects are groupped together. My solution would be to create a structure that holds information about one particular student, and then in the main program I would put a group of such objects in an array, but to do this you really need to understand what I linked in previous post.

Also, before you make any hasty decision about your education, follow this easy tutorial on http://www.learncpp.com/ - it is designed for people with 0 programming experience, but after lesson on structs, you'd be able to solve your problem in 5 minutes.
Last edited on
So i looked into a little of the structure thing due to the fact i have to go to work but i think i get it.So for instance with my program i would use it in the sense

struct student
{
char name[30]
char subjects[3][30]
int ID[5]
int room[5]
};

int main()
{
using namespace std;

student one =
{
"John Doe"
"Gym","Science","Music"
12345
321
};






But now even if i get the structures i dont know how i would use arrays as well sorry if i am being a pain but i really want to learn this


closed account (D80DSL3A)
That's a good start. If you can use std::string (this is c++) instead of char arrays (c) that would be cool.
I'll assume you can't though. I'll also assume you have been working with char arrays a while so you know how to use functions like strcpy (copy strings) and strcmp (compare alphabetically), etc.
Some corrections on the structure definition, a few missing ;'s.
1
2
3
4
5
6
7
struct student
{
char name[30];
char subjects[3][30];
int ID[5];// why 5 numbers?
int room[5];// ditto
};

To make life easy, I recommend you add an INIT function to the structure.
The student struct definition would then be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct student
{
char name[30];
char subjects[3][30];
int ID;
int room;
void INIT(char Name[], char Subject1[], char Subject2[], char Subject3[], int Ident, int Room )
{
    strcpy(Name, name);// verify this call!!
    strcpy(Subject1, subjects[0]);// I don't use these functions myself anymore.
    // and for subjects[1], [2]
    ID = Ident;
    room = Room;
}
};

Then in main, you create and initialize an array of students like so:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    // declare some students
    student pupil[5];

    // INIT them    
    pupil[0].INIT("John Doe","Gym","Science","Music", 12345, 321 );
    pupil[1].INIT(...);
    // and for pupil[2],[3],[4]

    return 0;
}


EDIT: I have treated ID and room as single integer numbers, not as arrays. If these really do need to be arrays then make the couple of changes to the last 2 args for INIT and add code to copy each element in INIT.
Also, the code was written on the fly so it probably contains a few errors. My intent is to outline a method.
Last edited on
thank you fun2code i appreciate your help with my program. I do need you to try and elaborate on this a bit more for me actually. I do need arrays for this program but honestly i just need it to run. If i need to i will lose the arrays but i would like to keep them.
closed account (D80DSL3A)
Do you mean the arrays int ID[5]; int room[5]; ?
What are they for? In the example case you gave only one number is given for each.
ID[0] = 12345;
room[0] = 321;
What are you planning to do with ID[1] through ID[4] ?
room[1] through room[4] ?
Are you wishing to store one digit per element, or something?

I do need you to try and elaborate on this a bit more for me actually.

I'm just trying to help with ideas. There's enough info above to get the student data entered. The search by student ID number (which of the 5?) can be done after the student data is all initialized, which I showed exactly how to do (one way). Try to get this part working yourself. Research the strcpy function if you need to so you can complete the INIT function.
So i was watchin a few videos and reading some things as well and i believe i have found a way to get this done but again i am stuck please help anyone if you can.Where i am lost is this will print the student names out but i also need it to take the rest of the variables(ID,Subject,Room,Time) and print the schedule per Student ID
here is the code i have so far

#include<iostream>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>


using namespace std;
int populate(char array1[], int number);
void spit(char array1[], int number);
int main()
{
double drew[5]={};
populate(drew, 5);
spit (drew, 5);
system("pause");
return 0;
}


int populate(char array1[], int number){
char input;
int i;
for(i=0;i<number;i++){
cout << "Enter Student Names " << (i+1) << endl;
cin >> input;
array1[i]=input;
}
return i;
}
void spit(char array1[], int number){
for (int i =0;i<number;i++){
cout << "The name of student " << (i+1) << " is " << array1[i] << endl;
}
}
Last edited on
Topic archived. No new replies allowed.