Question: I noticed while testing my C++ contact manager that when the phone number entered is not an integer the program displays all of the cout

I noticed while testing my C++ contact manager that when the phone number entered is not an integer the program displays all of the cout statements over and over without stopping. I can't figure out what if statement to use to check for an integer. Here is the code i have so far:

#include

#include

#include // std::setprecision

using namespace std;

// Declaring Global Arrays

string names[10];

long long phoneNos[10];

// Function Declarations

void getData();

void sort();

void display();

long long searchPhoneNum();

int main()

{

// Declaring variables

int choice;

/* This while loop continues to execute

* until the user enters a valid menu option

*/

while (true)

{

cout << "********Menu*********" << endl;

cout << "1.Add Contact" << endl;

cout << "2.Display All Conacts" << endl;

cout << "3.Search Contacts" << endl;

cout << "4.Exit" << endl;

cout << "****Enter Choice:****"<< endl;

cin >> choice;

/* Based on the Menu option selected

* the corresponding case will be executed

*/

switch (choice)

{

case 1:

{

// Calling the get data functions to store data

//Need to figure out if I need an IF statement here when phone number is //not an integer

getData();

continue;

}

case 2:

{

// Calling the sort and display functions

sort();

display();

continue;

}

case 3:

{

// Calling the search for phone number functions

long long no = searchPhoneNum();

if (no != -1)

cout << "The Telephone number is :" << no << endl;

else

cout << "~Sorry, no contact found~" << endl;

continue;

}

case 4:

{

cout << "** Good Bye **" << endl;

break;

}

default:

{

cout << "!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;

cout << "!!Invalid Menu Option!!" << endl;

cout << "!!!!!!!!!!!!!!!!!!!!!!!!! << endl;

continue;

}

}

break;

}

return 0;

}

/* Function implementation which will

* get the data from the user and populate

* the data into arrays. Case 1

*when testing this entering alpha characters

*all of the cout statements repeat to no end

*need to add a condition that if the phone number

*entered is not an integer, it loops back to enter phone

*number while keeping the stored # matching the name

*/

void getData()

{

string name;

long long phNo;

for (int i = 0; i < 10; i++)

{

cin.ignore();

cout << "Enter name of Person#" << i + 1 << ":";

std::getline(std::cin, name);

names[i] = name;

cout << "Enter Phone Number:";

cin >> phNo;

phoneNos[i] = phNo;

}

}

/* Function implementation which will

* which will sort the data in the arrays. Case 2 part 1

*/

void sort()

{

// This Logic will Sort the Array of elements in Ascending order

string tempName;

long long tempNum;

for (int i = 0; i < 10; i++)

{

for (int j = i + 1; j < 10; j++)

{

if (phoneNos[i] > phoneNos[j])

{

tempNum = phoneNos[i];

phoneNos[i] = phoneNos[j];

phoneNos[j] = tempNum;

tempName = names[i];

names[i] = names[j];

names[j] = tempName;

}

}

}

}

/* Function implementation which will

* search the phone number in the arrays. Case 3

*/

long long searchPhoneNum()

{

string name;

cin.ignore();

cout << "Enter the name :";

std::getline(std::cin, name);

int i = 0;

while (i < 10)

{

if (name.compare(names[i]) == 0)

{

return phoneNos[i];

}

i++;

}

return -1;

}

/* Function implementation which will

* display the data in the arrays. Case 2 part 2

*/

void display()

{

cout << "^^Here are your Contacts^^ " << endl;

for (int i = 0; i < 10; i++)

{

cout << setw(10) << names[i] << "\t\t" << phoneNos[i] << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!