infinite loop after char is entered:
I have the program working but when I check if the input to make sure it is not a char it creates a infinite loop. I used an if statement to check for the issue and solve it but its not working. I even tried throwing an exception but I learned later they are not used for things like this.
Code:
#include <iostream>
#include <limits>
#include "contacts.h"
using namespace std;
int main()
{
addressBook MyaddressBook;
int userInput=0;
nodePtr temp=new node;
string NAME;
MyaddressBook.userPromptStatement();
while(1)
{
cin>>userInput;
if (userInput!=1||userInput!=2||userInput!=3||userInput!=4)
{
cout<<"Only enter a 1,2,3 or 4"<<endl;
cout<<"You have entered an incorrect value."<<endl;
cout<<endl<<endl;
MyaddressBook.userPromptStatement();
}
if (isalpha(userInput)) {
cout<<"ERROR!!!,\n"
<<"Only enter a 1,2,3 or 4"<<endl;
cout<<"\n"<<"\n";
cin.fail();
cin.clear();
MyaddressBook.userPromptStatement();
}
else
switch (userInput) {
case 1:
cout<<"NAME: ";
cin.ignore();
getline(cin,temp->name,'\n');
cout<<"\nRANK: ";
getline(cin,temp->last,'\n');
cout<<"\nAGE: ";
cin>>temp->age;
MyaddressBook.addContact(temp);
MyaddressBook.userPromptStatement();
cout<<"\n"<<"\n";
break;
case 2:
cout<<"Which contact would you like to delete?\n";
cin>>NAME;
MyaddressBook.deleteName(NAME);
MyaddressBook.userPromptStatement();
cout<<"\n"<<"\n";
break;
case 3:
MyaddressBook.editName(NAME);
break;
case 4:
MyaddressBook.printContactList();
MyaddressBook.userPromptStatement();
cout<<"\n"<<"\n";
default:
break;
}
}
return 0;
}