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

Modify the c++ program so that it also allows the user to enter a 10-character Z

ID: 3818360 • Letter: M

Question

Modify the c++ program so that it also allows the user to enter a 10-character ZIP code, as long as the sixth character is a hyphen. Remove the hyphen before sending the ZIP code to the verifyNumeric function. Save and then run the program. Test the program appropriately.

#include <iostream>

#include <string>
using namespace std;
char verifyNumeric(string zip);
int main()
{
   string zipCode = "";
   char isAllNumbers = ' ';
   cout << "Please enter zip code: ";
   cin >> zipCode;
   if (zipCode != "-1")
   {
       if (zipCode.length() == 5 || zipCode.length() == 9)
       {
           cout << zipCode.length() << "-character ZIP code (-1 to end): " << endl;
           cout << "-->Correct number of characters " << endl;
           isAllNumbers = verifyNumeric(zipCode);
           if (isAllNumbers == 'Y')
               cout << endl << "-->All numbers " << endl;
           else
               cout << endl << "-->Not all numbers " << endl;
       }
       else
           cout << "-->Incorrect number of characters " << endld;

   }
   return 0;
}
char verifyNumeric(string zipCode)
{
   string currentChar = "";
   int sub = 0;
   char isAnumber = 'Y';

   while (sub < zipCode.length() && isAnumber == 'Y')
   {
       currentChar = zipCode.substr(sub, 1);
       if (currentChar >= "0" && currentChar <= "9")
           sub += 1;
       else
           isAnumber = 'N';
   }
   return isAnumber;
}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
char verifyNumeric(string zip);
int main()
{
string zipCode = "";
char isAllNumbers = ' ';
cout << "Please enter zip code: ";
cin >> zipCode;
if (zipCode != "-1")
{
if (zipCode.length() == 10 )
{
if (zipCode[5] != '-')
{
cout << "Format of zip code is 11111-1111. That is 5 numbers a hyphen and then 4 numbers" << endl;
}
else
{
zipCode = zipCode.substr(0, 5) + zipCode.substr(6);
  
cout << "-->Correct number of characters " << endl;
isAllNumbers = verifyNumeric(zipCode);
if (isAllNumbers == 'Y')
cout << endl << "-->All numbers " << endl;
else
cout << endl << "-->Not all numbers " << endl;
}
}
else
cout << "-->Incorrect number of characters " << endl;
}
return 0;
}
char verifyNumeric(string zipCode)
{
string currentChar = "";
int sub = 0;
char isAnumber = 'Y';
while (sub < zipCode.length() && isAnumber == 'Y')
{
currentChar = zipCode.substr(sub, 1);
if (currentChar >= "0" && currentChar <= "9")
sub += 1;
else
isAnumber = 'N';
}
return isAnumber;
}

/*

Sample output

Please enter zip code: 12345-9878
-->Correct number of characters

-->All numbers
sh-4.2$ main
Please enter zip code: 12345-789k
-->Correct number of characters

-->Not all numbers
sh-4.2$ main
Please enter zip code: 25
-->Incorrect number of characters
sh-4.2$ main
Please enter zip code: 1234567890
Format of zip code is 11111-1111. That is 5 numbers a hypher and then 4 numbers

*/

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