PLEASE NO USE OF JAVA SCRIPT JUST C# First you’ll create a list of zip codes, st
ID: 3884681 • Letter: P
Question
PLEASE NO USE OF JAVA SCRIPT JUST C#
First you’ll create a list of zip codes, stored as values of array elements, as follows:
Ask for the quantity of 5-digit zip codes you’ll need. Allow the user to enter 0 as well. Save to a variable.
i.Use try-catch to make sure that an integer is entered. Loop back and make the user try again until an integer is entered.
Ask for that many 5-digit zip codes (perhaps you’ll use a For loop). Make sure that the user types in 5 digits. Make them try again and again until they give a 5-digit value.
i.Of course, if the user entered 0 in step a, your program won’t ask for any 5-digit zip codes.
Save each zip code as an array element. I would probably make the array’s data type be string (giving you a length property to work with when you’re checking to make sure it’s 5 digits).
Now, as long as your quantity wasn’t 0, you have the array that holds a varying quantity of zip codes of areas to which this company makes deliveries.
If the quantity is not 0, prompt the user to enter a 5-digit zip code. Make them enter 5 digits – and repeat if they don’t get it right. Save the 5-digit zip code to a variable.
If the quantity was not 0, have your code check to see if the zip code entered in at step #3 exists in the array of available zip codes (created in step #1).
If the zip code entered in at step #3 exists in the array, set a boolean variable named zipExists (or something like that) equal to true.
If the zip code entered does not exist in the array, set the boolean variable equal to false.
If the quantity was not 0, and If the boolean variable set in step #4 is true, display a message to the screen indicating that the zip code exists. Make sure that the message includes the zip code that was keyed in at step #4.
If the boolean variable = false, display a message, including the zip code in the message, saying that the zip code doesn’t exist in the list.
If, on the other hand, steps 3-5 never happened because the quantity was 0, write a message stating that there are not zip codes to validate against.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string[] zip_codes = new string[5];
int non_zero_count = 0;
for (int i = 0; i < 5; i++)
{
int valid = 0;
do
{
Console.WriteLine("Enter zip code " + (i+1) + ": ");
zip_codes[i] = Console.ReadLine();
if (zip_codes[i] != "0")
{
if (zip_codes[i].Length == 5)
{
try
{
int zip = Convert.ToInt32(zip_codes[i]);
non_zero_count++;
valid = 1;
}
catch (Exception e)
{
Console.WriteLine("Enter a valid input");
}
}
}
else
{
break;
}
} while (valid == 0);
}
if (non_zero_count > 0)
{
int valid1 = 0;
string zip1="";
do
{
Console.WriteLine("Enter zip code : ");
zip1 = Console.ReadLine();
if (zip1.Length == 5)
{
try
{
int zip = Convert.ToInt32(zip1);
non_zero_count++;
valid1 = 1;
}
catch (Exception e)
{
Console.WriteLine("Enter a valid input");
}
}
} while (valid1 == 0);
Boolean found = false;
for (int j = 0; j < 5; j++)
{
if (zip_codes[j] == zip1)
{
found = true;
break;
}
}
if (found)
{
Console.WriteLine("Zip code exists");
}
else
{
Console.WriteLine("Zip code does not exist");
}
}
else
{
Console.WriteLine("There are not zip codes to validate against");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.