Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the program so that it allows the user to enter either a five-character Z

ID: 3818317 • Letter: M

Question

Modify the program so that it allows the user to enter either a five-character ZIP code or a nine-character ZIP code. Pass the number of characters in the ZIP code to the verifyNumeric function. Save and then run the program.

//Modified10.cpp

#include <iostream>
#include <string>
using namespace std;

char verifyNumeric(string zip);

int main()
{
   string zipCode = "";
   char isAllNumbers = ' ';

   cout << "Five-character ZIP code (-1 to end): ";
   cin >> zipCode;

   while (zipCode != "-1")
   {
       if (zipCode.length() == 5)
       {
           cout << "-->Correct number of characters";
           isAllNumbers = verifyNumeric(zipCode);
           if (isAllNumbers == 'Y')
               cout << endl << "-->All numbers";
           else
               cout << endl << "-->Not all numbers";
       }
       else
           cout << "-->Incorrect number of characters";
       cout << endl << endl;

       cout << "Five character ZIP code (-1 to end): ";
       cin >> zipCode;
   }
   return 0;
}

char verifyNumeric(string zip)
{
   string currentChar = "";
   int sub = 0;
   char isAnumber = 'Y';
  
   while (sub < 5 && isAnumber == 'Y')
   {
       currentChar = zip.substr(sub, 1);
       if (currentChar >= "0" && currentChar <= "9")
       sub += 1;
   else
       isAnumber = 'N';
   }
   return isAnumber;
}

Explanation / Answer

The program below is the modified program:

#include <iostream>
#include <string>
using namespace std;
char verifyNumeric(string zip);
int main()
{
string zipCode = "";
char isAllNumbers = ' ';
cin >> zipCode;
if (zipCode != "-1")
{
if (zipCode.length() == 5 || zipCode.length() == 9)
{
cout << zipCode.length()<<"-character ZIP code (-1 to end): ";
cout << "-->Correct number of characters";
isAllNumbers = verifyNumeric(zipCode);
if (isAllNumbers == 'Y')
cout << endl << "-->All numbers";
else
cout << endl << "-->Not all numbers";
}
else
cout << "-->Incorrect number of characters";
  
}
return 0;
}
char verifyNumeric(string zip)
{
string currentChar = "";
int sub = 0;
char isAnumber = 'Y';
  
while (sub < zip.length() && isAnumber == 'Y')
{
currentChar = zip.substr(sub, 1);
if (currentChar >= "0" && currentChar <= "9")
sub += 1;
else
isAnumber = 'N';
}
return isAnumber;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote