In this assignment you will create a simple program that can obtain a numeric input from the console window (stdin), and use it to index and array of characters.

computer science

Description

Task Description:


In this assignment you will create a simple program that can obtain a numeric input from the console window (stdin), and use it to index and array of characters. The program will use the ctype.h library to determine the type of character contained in that array index location and print it to the screen. The program will run in a continuous loop until terminated by the operator by entering q (for quit). The program will also perform error checking on the character input to determine if it is a valid number. If the character entered by the operator isn’t correct, the program will alert the operator and ask that the number be re-entered correctly.


Procedure:


Create a Win32 console application in MSVisual C and call it w0######_A4.

Once the project is created start adding code to perform the following:

Include the <ctype.h> library to you program. This will give you access to all the functions in the ctype.h library.

Variable declarations. Declare a character array and initialize it with the following characters. As you can see the array contain letters (upper and lower case), numbers, print format, punctuation,, and control characters)

char  chartypes[] = {‘A’,’\t’,‘T’,’e’,’s’,’t’,’1’,’!’,’ ’,’\0’};

All other variables defined will depend on your program implementation.


PART 1 – Determine character type


Print a message to the console window (stdout) asking the operator to input a number from 0 to the size of the array, to index the array.

An easy way to determine the size of the array is to use the sizeof() function contained in the <stdlib.h> library. This function will return the array size in bytes.

Perform a google search on c sizeof() to determine its implementation

This will allow your program to work even if you change the array size and contents between runs.

Once the operator enters the number, you will have to obtain that number from the (stdin) buffer and store it appropriately. This can be done using one of the three methods discussed in class.

Once a valid number is obtained, use it to obtain the character in that location in the array (Remember, arrays are zero indexed).

Using the <ctype.h> library of functions, add code to determine the character type contained in that array index specified by the operator. It should be 1 of  5

letter, 

number, 

punctuation, or 

control character. 

print format character, 

This will require the use of a multi-layered conditional statement.

For each condition, print the array character to the screen in its proper format, using a sentence stating the type of character detected. 

Have a default case for instances where the character isn’t recognized.

Letters - If the character is a letter, add code to determine if it is an upper case or lower case letter. Alert the operator as to its case with a printf() message and then convert it to its opposite case and print it to the console (stdout) window.

Note: Spaces and tabs won’t display on the console window. They will appear as blank spaces.

Add a statement to clear the (stdin) buffer once the program has completed

Compile your code to ensure there are no syntax errors. If errors exist, correct them before continuing.

Execute the program to verify it is working as specified.


PART 2 – Program control


Modify the program further to allow the entire program to repeat over and over again until terminated by the operator.

This will require giving the operator a choice as to whether to continue or terminate the program after each run.

If the operator chooses to continue, the console window will clear and the program will repeat.

If the operator chooses to terminate the program. The program will print the message “Program terminated by operator”.

Compile your code to verify that the program continues until terminated by the operator.


PART 3 – Input Error Checking


Input error checking - Add code immediately after the statement that obtains the operator entered number from (stdin). This new code will interrogated the number entered to ensure it is within the range specified. If it isn’t, the operator will be alerted and asked to re-enter the number. 

The operator will only be given three chances, after which, the program will exit with the message “Improper character entered 3 times. Program terminated.”.

This could be accomplished using a for-loop or a do-while with a compound condition.

Once complete, compile your code and test your program to verify that it operates as requested, i.e. enter three numbers outside of the range to verify that the program exits with the error message specified. Enter a number inside the range and verify the proper message is printed to (stdout).

PART 4 – Additional Test


Change the number of characters in the array and re-run your code. Verify the range of numbers the operator is allowed to enter changes and the program still functions correctly.


Instruction Files

Related Questions in computer science category