This is a program that is supposed to use structs to be able to enter a courses data and output it again. I can not get the data to correctly input and then output. I am not sure what is wrong with it, after I input something, the output is a bunch of brackets and vertical lines followed by the first thing I inputed. I have tried many different combinations to try and fix it but to no avail. It seemed to reduce the amout of brackets when I changed the maxlength but not really. Any help would be greatly appreciated. Here is my program:
const int MAXLENGTH = 5; //Set variables to a max length of 5 characters
const int MAXCLASSES = 5;
struct Classes //Set up a struct that contains variables
{
char classnumber[MAXLENGTH];
char classname[MAXLENGTH];
char meetday[10];
char teachername[MAXLENGTH];
float starttime;
float endtime;
int studentnumber;
};
int main()
{
int NUMCLASSES;
int userChoice;
displayMenu: //Create a place holder for the menu
cout <<"What would you like to do?"<<endl; //Ask user what they would like to do
cout <<"1. Add new data"<<endl;
cout <<"2. Display all classes"<<endl;
cout <<"3. Display one class" <<endl;
cout <<"4. Quit"<<endl;
cin>> userChoice; //figures out what the user wants to do
if (userChoice == 1) //if the user wants to enter new data
{
int iIndexA;
cout << "How many classes would you like to enter?" ; cin >> NUMCLASSES ; //Creates how many entries the user will input
Classes classes[MAXCLASSES];
for(iIndexA = 0; iIndexA < NUMCLASSES; iIndexA++)
{
cout << "Class #: " ;
cin >> classes[iIndexA].classnumber ;
cout << "Class name: ";
cin >> classes[iIndexA].classname ;
cout << "Teacher's name: ";
cin >> classes[iIndexA].teachername ;
cout << "Meet day: ";
cin >> classes[iIndexA].meetday ;
cout << "Start time: "; cin >> classes[iIndexA].starttime;
cout << "End time: "; cin >> classes[iIndexA].endtime;
cout << "Number of students: "; cin >> classes[iIndexA].studentnumber;
}
system("PAUSE");
goto displayMenu; //go back to the menu
}
1.) use [code][/code] tags around your code
2.) don't use goto's...they make it hard to follow your code
3.) don't use system()...there is topic up on the top of the beginners forum (do what Duaos says)
Problem I saw: If someone inputs say 10 classes, then you will be writing outside of the bounds of your array, you will need to check that value.
Anyway, you are having a problem with scoping...(I think, since gotos might screw it up). When you leave the if (userChoice == 1) section, all of the variables inside of it are deleted...meaning that NUMCLASSES/classes do not exist anymore. Next, when you go into the second choice, you recreate the classes variable, which will contain garbage data since you have not initialized anything inside of it.
Also, you can do for loops like this:
for(int i = 0; i < somevalue; ++i) to make it shorter and easier to use (you can reuse i after the for loop and not have problems)