So, I am currently having 2 issues with my code.

Problem 1: Any value entered in for the GPA that isn't a number causes an infinite loop. (example: enter in the letter X) Instead of giving an error message, it just goes into that infinite loop.

bool right = false;
		while(!right)
                        {
				cout << "4) GPA	      : ";
				cin >> mGPA;
				if(mGPA)
                                        {
						if(mGPA < 0 || mGPA > 4) 
                                                   cout << "GPA MUST BE WITHIN RANGE! (0 to 4)" << endl << endl;
						else 
                                                   right = true;
					} 
                                else 
                                        cout << "GPA ENTERED WAS NOT A NUMBER!" << endl << endl;
			}

I'm open to alternatives on that.

Problem 2: If a user is selecting an option from the menu, if it's another number (ex: 6) or a letter (ex: x) it just ends the program rather than saying it's invalid and going back to display the menu again.

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;
        
        
//#############CREATES A NEW STUDENT IN THE ARRAY###################
        if (menuChoice == 1)
           {
		......
	   }
           
           
           
//#############DELETES A STUDENT IN THE ARRAY###################           
        else if (menuChoice == 2)
           {    
		......
           }
           
           
           
           
//#############DISPLAYS ALL STUDENTS IN THE ARRAY###################
           
        else if (menuChoice == 3)
           {
               ...... 
           }
           
           
           

//#############SEARCHES ARRAY FOR ONE STUDENT IN PARTICULAR###################           
        else if (menuChoice == 4)
           {
              ........
           }
           
           
           
           
//#############QUITS THE PROGRAM###################
        else if (menuChoice = 5)
           {                         
		term = 1;
           }
        
        else
            cout << "Please enter in a correct menu choice!";
}while(term == 0);

Again, open to any suggestions to amend these issues.

Thanks again.

Recommended Answers

All 4 Replies

You need to post more code. We need to see the declaration of mGPA. Presumably it's a double, float, long, int, something like that, so when you enter "x", >> can't do anything with it and it has this "x" in the stream that it must get past before it can ever get to any good data you enter later. The "x" is in the way and will never be consumed because a double, float, or int can't consume an "x". You need to clear and flush the input stream when you get bad data so you can try again without having that "x" continually ruin everything. See this thread:

http://www.daniweb.com/forums/thread90228.html

#include <iostream>
using namespace std;

int main ()
{
    double a = -1;

    do
    {
      cout << "Enter a number greater than 1 : ";
      cin >> a;
      if (a <= 1 || cin.fail ())
      {
        cin.clear ();
        cin.ignore ( 80, '\n' );
        cout << "Bad data entered.  Please try again.\n";
      }
    }
    while (a <= 1);

    return 0;
}

As to your second problem, again, we need more code. Presumably term equals 0 when you ENTER the do while loop? Otherwise it will exit.

Ah ha. Thank you for the explanation to my first problem. :) Resolved that one now!

I didn't want to flood this with code, but I suppose I can.

int term = 0;
  int menuChoice = 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;
        
        
        if (menuChoice == 1)
           {
               if (numStudents < 30)
               {
                       while(1)
                       {
							cout << endl << "1) First Name : ";
							cin >> mFName;
							bool bRejected = false;

							for (int nIndex=0; nIndex < mFName.length() && !bRejected; nIndex++)
                            {
								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++)
                            {
								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    : ";
                       cin >> mAddress;
                       getline(cin,nAddress);
                       
                       int right = -1;
                       do
                        {
							cout << "4) GPA	      : ";
							cin >> mGPA;
								if(mGPA < 0 || mGPA > 4 || cin.fail() ) 
								{    
                                     cin.clear ();
                                     cin.ignore (80, '\n');
                                     cout << "INVALID DATA! GPA MUST BE WITHIN RANGE! (0 to 4)" << endl << endl;
                                }       
        
								else 
                                     right = 1;
						} while(right=-1);
                    
                       mAddress = mAddress + nAddress;
                       mID = (rand()%9999)+1; 
                       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 << "Unable to create new student! Please delete a current student to make a new one.";
           }
           
           
       
        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)
                                  {
                                      students[i] = kosong;
                                      found = true;
                                      cout << "Student with ID " << delID << " has been deleted!" << endl << endl;
                                  }
                              }
                       if(!found)
                                 cout << "Student ID not found!" << endl << endl;
                       }
                       else
                           cout << "Student ID not vaild!" << endl << endl;
           numStudents--;
           }
           
           
          
           
        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;   
           }
           
           
                     
        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 << "STUDENT ID NOT FOUND!" << endl << endl;	
						}
                        else 
                             cout << "STUDENT ID IS NOT VALID!" << endl << endl; 
           }
           

        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
            cout << "Please enter in a correct menu choice!";
}while(term == 0);

It doesn't even appear to be getting to that last else statement because nothing is printed. It just ends.

Line 160 - the old = versus == typo. Make sure you don't have it anywhere else.

Wow. I feel like an idiot! Haha! Many thanks for your assistance again!

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.