I need some help with this program. When I'm trying to print the data to the screen, something weird happens.
also does anybody has any suggestions how to print just one class(in printOne function)?
THANK YOU
Code:
#include <iostream>
#include "myHeader.h"
#include <stdlib.h>
#include <conio.h>
using namespace std;
//Function prototypes
void newData();
void printData();
void printOne();
void printAll();
void quit();
//global variables
bool showMenu = true;
const int NUMCLASSES = 10;
int NUMCLASSES2;
int iIndex;
int cData[NUMCLASSES];
int main()
{
int function;
//shows menu when showMenu is true.
while(showMenu == true)
{
system("cls");
cout << "==========================================" << endl;
cout << " Class Planner " << endl; //Prints data on screen
cout << "==========================================" << endl;
cout << " What do you want to do " << endl;
cout << "1. Enter new data" << endl;
cout << "2. Print data to screen" << endl;
cout << "3. Exit" << endl;
cout << "==========================================" << endl;
cin>>function;
switch(function) //using switch to enter a function
{
case 1:
{
newData();
break;
}
case 2:
{
printData();
break;
}
case 3:
{
quit();
break;
}
default:
{
break;
}
}
}
return 0;
}
//quit function
void quit()
{
char cQuit;
system("cls");
cout<<"Would you like to quit? (Y/N)\n";
cin>>cQuit;
if(cQuit == 'N' || cQuit == 'n')
{
system("cls");
}
else if(cQuit == 'Y' || cQuit == 'y')
{
system("cls");
showMenu = false;
cout<<"Please use this program again!"<<endl;
}
else
{
system("cls");
quit();
}
}
//asks the user to print new data
void newData()
{
system("cls");
cout<<"How many classes do you have? "; cin>> NUMCLASSES2;
cInfo cData[NUMCLASSES];
//int iIndex;
for(iIndex = 0; iIndex < NUMCLASSES2; iIndex++)
{
cout<<endl;
cin.ignore();
cout << "Class name: ";
cin.getline(cData[iIndex].className, NAMELENGTH);
cout << "Class number: "; cin >> cData[iIndex].classNumber;
cin.ignore();
cout << "Class meets on ('MWF' if it meet on Monday, Wednesday, and Friday): ";
cin.getline(cData[iIndex].classMeets, NAMELENGTH);
cout << "Start time: "; cin >> cData[iIndex].classStart;
cout << "End time: "; cin >> cData[iIndex].classEnd;
cin.ignore();
cout << "Teacher name: ";
cin.getline(cData[iIndex].teacherName, (NAMELENGTH));
cout << "Number of students: "; cin >> cData[iIndex].numberStudents;
cout << endl;
}
for(iIndex = 0; iIndex < NUMCLASSES2; iIndex++)
{
cout << endl;
cout << "Class Name: " << cData[iIndex].className << endl;
cout << "Class Number: " << cData[iIndex].classNumber << endl;
cout << "Class meets on: " << cData[iIndex].classMeets << endl;
cout << "Start Time: " << cData[iIndex].classStart << endl;
cout << "End Time: " << cData[iIndex].classEnd << endl;
cout << "Teacher: " << cData[iIndex].teacherName << endl;
cout << "Number of students: " << cData[iIndex].numberStudents << endl << endl;
}
system("PAUSE");
}
void printData()
{
cInfo cData[NUMCLASSES];
int dataOption;
system("cls");
cout<<"Print Data to screen"<<endl;
cout<<"1. Print All"<<endl;
cout<<"2. Print a class"<<endl;
cin>>dataOption;
if(dataOption == 1)
{
system("cls");
for(iIndex=0; iIndex < NUMCLASSES2; iIndex++)
{
cout << endl;
cout << "Class Name: " << cData[iIndex].className << endl;
cout << "Class Number: " << cData[iIndex].classNumber << endl;
cout << "Class meets on: " << cData[iIndex].classMeets << endl;
cout << "Start Time: " << cData[iIndex].classStart << endl;
cout << "End Time: " << cData[iIndex].classEnd << endl;
cout << "Teacher: " << cData[iIndex].teacherName << endl;
cout << "Number of students: " << cData[iIndex].numberStudents << endl << endl;
}
system("PAUSE");
}
else if(dataOption == 2)
{
system("cls");
}
else
{
printData();
}
}
void printOne()
{
system("cls");
}
//(contents of the file myHeader.h)
#ifndef _MYHEADER_H
#define _MYHEADER_H
const int NAMELENGTH = 35;
struct cInfo
{
char className[NAMELENGTH];
int classNumber;
char classMeets[NAMELENGTH];
int classStart;
int classEnd;
char teacherName[NAMELENGTH];
int numberStudents;
};
#endif