0

So, I created a program that allows a user to create student, delete student, display all students, search students, and quit. They are labeled 1 - 5, respectively. However, if a user were to enter 1, create the student, be taken back to the main menu and typed in x. It shoots back into whatever was done last. In this case, it was creating the student. It then asks for the GPA over again and creates a duplicate student in the array.

I'll attach the main.cpp

Attachments
//OO Programming
//Assignment #1

#include <iostream>
#include <string>
#include <cstdlib>
#include "student.h"
using namespace std;

int main(void)
{
  Student students[30];
  Student delStudent;
  int term = 0;
  int searchID = 0;
  int numStudents = 0;
  int menuChoice = 0;
  int delID = 0;
  string mFName = "";
  string mLName = "";
  string mAddress = "";
  string nAddress = "";
  int mID = 0;
  float mGPA = 0.0;
  
  do
  {

        cout << "MENU" << endl;
        cout << "1. Create New Student" << endl;
        cout << "2. Delete Student (by ID Number)" << endl;
        cout << "3. Display All Students" << endl;
        cout << "4. Search for Student (by ID Number)" << endl;
        cout << "5. Quit" << endl;
        cout << "Which would you like to do? ";
        cin >> menuChoice;
        cout << endl;

        
//#############CREATES A NEW STUDENT IN THE ARRAY###################
        if (menuChoice == 1)
           {
               //Checks to make sure that there are not already 30 students in the array
               //If numStudents is less than 30, it allows the user to create the student
               //Otherwise if it's more than 30, it tells them they have to delete a student to continue
               if (numStudents < 30)
               {

                      //Allows input and checks the first name to verify it only contains alpha characters
                       while(1)
                       {
							cout << endl << "1) First Name : ";
							cin >> mFName;
							bool bRejected = false;

							for (int nIndex=0; nIndex < mFName.length() && !bRejected; nIndex++)
                            {
								// Checks character to see if it is in the alphabet
								if (isalpha(mFName[nIndex])) 
                                   continue;
								if (mFName[nIndex]==' ') 
                                   continue;
								bRejected = true;
							}
							if (!bRejected) 
                               break;
							   cout << "NAME CAN ONLY CONTAIN CHARACTERS FROM ALPHABET!" << endl << endl;
						}
						
                       while(1)
                       {
                            cout << "2) Last Name  : ";
							cin >> mLName;
							bool sRejected = false;
							
							for (int nIndex=0; nIndex < mLName.length() && !sRejected; nIndex++)
                            {
								// Checks character to see if it is in the alphabet
								// If it is, it allows it to continue
								if (isalpha(mLName[nIndex])) 
                                   continue;
								if (mLName[nIndex]==' ') 
                                   continue;
								sRejected = true;
							}
							if (!sRejected) break;
							   cout << "NAME CAN ONLY CONTAIN CHARACTERS FROM ALPHABET" << endl << endl;
                        }
                        
                       cout << "3) Address    : ";
                       //Allows input, but ends up taking only the address up to the space
                       cin >> mAddress;
                       //Pulls in the entire line
                       getline(cin,nAddress);
                       
                       int right = 0;
                       while(right == 0)
                        {
							cout << "4) GPA	      : ";
							cin >> mGPA;
								if(mGPA < 0 || mGPA > 4 || cin.fail() ) 
								{    //clears and flushes the input stream because of bad data
                                     cin.clear ();
                                     cin.ignore (80, '\n');
                                     cout << "INVALID DATA! GPA MUST BE WITHIN RANGE! (0 to 4)" << endl << endl;
                                }       
        
								else 
                                     right = 1;
						} 
                     
                       //Merges the two strings from the address inputs to make a COMPLETE address
                       mAddress = mAddress + nAddress;
                       //Randomizes ID Number
                       mID = (rand()%9999)+1; 
                       //Calls functions to set values
                       students[numStudents].setID(mID);
                       cout << "5) Student ID : " << mID << endl << endl;
                       students[numStudents].setFName(mFName);
                       students[numStudents].setLName(mLName);
                       students[numStudents].setAddress(mAddress);
                       students[numStudents].setGPA(mGPA);
                       numStudents++;
               }
               else
                   cout << endl << "Unable to create new student! Please delete a current student to make a new one.";
           }
           
           
           
//#############DELETES A STUDENT IN THE ARRAY###################           
        else if (menuChoice == 2)
           {          
                       cout << "Which student do you want to delete? ";
                       cin >> delID;
                       cout << endl;
                       if(delID)
                       {
                              bool found = false;
                              for (int i=0;i<30;i++)
                              {
                                  if (students[i].getID() == delID)
                                  {
                                      //Deletes the student and sets the value of that part of the array to delStudent
                                      students[i] = delStudent;
                                      found = true;
                                      cout << endl << "Student with ID " << delID << " has been deleted!" << endl << endl;
                                  }
                              }
                       if(!found)
                                 cout << endl << "Student ID not found!" << endl << endl;
                       }
                       else
                       {   //clears and flushes the input stream because of bad data
                           cin.clear ();
                           cin.ignore (80, '\n');
                           cout << endl << "Student ID not vaild!" << endl << endl;
                       }
          //Decreases the number in numStudents to correct number of students in the array
           numStudents--;
           }
           
           
           
           
//#############DISPLAYS ALL STUDENTS IN THE ARRAY###################
           
        else if (menuChoice == 3)
           {
                    cout << endl<<endl<<" Display All Students "<<endl;
					cout <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
					cout << endl<< "FIRST NAME	LAST NAME	ADDRESS		     GPA		ID "<<endl;
					cout <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
                       for (int k=0; k<=numStudents; k++)
                           {
                                students[k].display();
                           }
                    cout << endl;   
           }
           
           
           

//#############SEARCHES ARRAY FOR ONE STUDENT IN PARTICULAR###################           
        else if (menuChoice == 4)
           {
                       cout << endl << "Which student are you looking for? (Please enter their ID Number) ";
                       cin >> searchID;
                       
                       if(searchID){
							bool found = false;
							for(int i=0;i<30;i++){
								if(students[i].getID() == searchID) 
                                {
									cout << endl << endl << "++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
									cout << "STUDENT WITH ID "<< searchID <<" FOUND:"<<endl;	
									students[i].SearchDisplay();
									cout << "++++++++++++++++++++++++++++++++++++++++++++++++++" << endl << endl;
									found = true;
								}
							}
							if(!found) 
                                  cout << endl << "STUDENT ID NOT FOUND!" << endl << endl;	
						}
                        else 
                        {    //clears and flushes the input stream because of bad data
                             cin.clear ();
                             cin.ignore (80, '\n');
                             cout << endl << "STUDENT ID IS NOT VALID!" << endl << endl;
                        } 
           }
           
           
           
           
//#############QUITS THE PROGRAM###################
        else if (menuChoice == 5)
           {   
               // ends the while loops by setting term to 1
                       term = 1;
           }
        
        //Any other input from user responds with this message   
        else
        {   //clears and flushes the input stream because of bad data
            //cin.clear();
            cin.ignore(80, '\n');
            cout << endl << "Please enter in a correct menu choice!" << endl << endl;
        }
}while(term == 0);
  system("pause");
  return 0;
}
1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by wyett
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.