okay i'm working on a very complicated problem (for me anyway) and i know i'm missing something somewhere. i need to be able to store data into an array but i don't want it looping right off. this is the structure for the data i need and my function headers. i need to read the struct into the addCourse functions but the only way i know to make an array work is to loop for all the data at once. however, thats not what i want to do. i want to be able to add one course then go back to the menu and add another if i chose then output the schedule.

struct courseInfo
{
	string courseName;
	string crn;
	int startTime;
	int endTime;
	string days;
};

//function prototypes
void displayMenu (int choice);
void addCourse (courseInfo addList[]);
void displaySchedule (courseInfo addList[]);

//declare constant
const int LIMIT = 20;

here's the addCourse function. i warn you its messy.

void addCourse (courseInfo addList[])
{
	
	int i=0;

	
		cout << "Enter the course title:" << endl;
		cin >> addList[i].courseName;
		cout << "Enter the course CRN:" << endl;
		cin >> addList[i].crn;
		cout << "Enter the start time for this course:" << endl;
		cin >> addList[i].startTime;
		cout << "Enter the end time for this course:" << endl;
		cin >> addList[i].endTime;
		cout << "Enter the days of the week the course occurs on:" << endl;
		cin >> addList[i].days;
	
	

	return;

}

i've tried a couple variations on loops and such but i just can't seem to make it work right. what i need to be able to do is the menu will show and the user choses the add course option, i need to store that info for recall later by other functions. how do i do that without looping the function itself? i want the option of the menu in between each entry without losing the data. thanks in advance! i hope i wasn't too confusing. if so just ask and i can try to clarify.

Recommended Answers

All 3 Replies

>>void addCourse (courseInfo addList[])
It would be better if all you did was pass only one array element and pass it by refernece

void addCourse (courseInfo& addItem)
{
	
    cout << "Enter the course title:" << endl;
    cin >> addItem.courseName;
    cout << "Enter the course CRN:" << endl;
    cin >> addItem.crn;
    cout << "Enter the start time for this course:" << endl;
    cin >> addItem.startTime;
    cout << "Enter the end time for this course:" << endl;
    cin >> addItem.endTime;
    cout << "Enter the days of the week the course occurs on:" << endl;
    cin >> addItem.days;
}

Then you call the above like this: I'm only showning a loop here for simplicity, so make it however complicated you need it.

int main()
{
    int i;
    for(i = 0; i < NumElements; i++)
    {
         addCourse (addList[i]);
    }
}

okay i tried something similar to that and i'm still not getting the result i wanted. maybe i didn't put something in the right place. here is some more of my code so maybe it will make a little more sense

int main()
{
	//delcare variable
	int choice = 1;
	courseInfo courseList[LIMIT];
	
	//explain program purpose to user
	cout << "This program will help you create a class schedule." << endl << endl;

	//show menu 
	displayMenu(choice);
	
	return 0;
}


void displayMenu (int choice)
{
	

	//loop menu till user decides to exit
	do 
	{
		cout << "Please chose an option from the following menu:" << endl
		<< "1. Add Course" << endl
		<< "2. Delete Course" << endl
		<< "3. Design Schedule" << endl
		<< "4. Save Data to a file" << endl
		<< "5. Load Data from a file" << endl
		<< "6. Exit" << endl << endl;
		
	cin >> choice;

	//process user choice
	if (choice == 1)
		for (int i=0; i < LIMIT; i++)
			addCourse(addList[i]);
	else if (choice == 3)
	{
		cout << endl << "Your schedule includes the following information:" << endl << endl;
		displaySchedule(addList);
	}
	else if (choice == 6)
		break;

	}while (choice < 7 && choice != 0);

	return;
	
} 

void addCourse (courseInfo& addInfo)
{
	

		cout << "Enter the course title:" << endl;
		cin >> addInfo.courseName;
		
		cout << "Enter the course CRN:" << endl;
		cin >> addInfo.crn;
	
		cout << "Enter the start time for this course:" << endl;
		cin >> addInfo.startTime;
		
		cout << "Enter the end time for this course:" << endl;
		cin >> addInfo.endTime;
		
	                cout << "Enter the days of the week the course occurs on:" << endl;
		cin >> addInfo.days;
	
		
	return;

}

whats happening now is what i was talking about earlier where it is looping the addCourse function instead of giving me the menu back. i need to store the data but be able to access the menu in between. am i making sense? because i have myself so confused.

delete the loop on line 36 and only leave line 37. Then declare variable i at the top of the function and initialize it to 0. Increment the value of i after line 37.

void displayMenu (int choice)
{
    int i = 0;
    do
    {
             // display menu here, not shown

             if( choice == 1)
             {
                 addCourse(addList[i]);
                 ++i;
            }
  // other code here

Looking at the rest of the menu items I suspect it will be a little easier if you used a linked list instead of an array of courses. To delete a course you would just have to delete the node in the linked list. In an array you will have to move all array elements up one to overwrite the course you want to delete. It can be done, but its just a tad bit more difficult.

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.