CSCI 515 Introduction to C Programming Spring 2000



CSCI 515 C/C++ Programming Fall 2002

Dr. Creider

Lab 3 Assignment

Character input, number conversion and arrays

Write a program that reads a series of numbers (positive or negative, integer or float) from a text file (lab3.dat) and converts the characters to the correct numeric value. In this lab you will use a single dimension array in which to store the characters you get from the input buffer. The lab will also use a method of parameter passing to the function called “pass by reference”. This lab will determine if the characters in the input buffer are integer or floating point and signed or unsigned. The program will convert the characters to a double type numeric value.

In main, read a string of characters, one string at a time until the file is empty. Each line of the file will contain one number in the form of a character string. The program will not check for input errors so be sure that you do not type any errors in the file. Store the string in a single dimension array of type char. When the file is empty terminate the loop, and pause the program using the getch() function. Print a message before you pause the program such as “press any key to terminate the program”.

Write a function to convert the characters (in the array) to a numeric value and store the result in a variable of type double. While converting from characters to a number, you should check for a minus sign. There cannot be a blank between a minus sign and a digit. There can be a decimal point after a minus sign. A number ends with a blank, a non-digit character, or the end of the buffer (array). For this program, all numbers will end with the “end of line” character which should be converted to the NULL character as the result of reading the string into the array. Pass to the function the array with the string and use “pass by reference” for all other arguments. The following arguments will be assigned a value in the function: number converted from character string, type of number (1 for integer, 2 for floating point), number of digits before the decimal point, number of digits after the decimal point.

Each time you read a string from the disk file print the string to the screen before you call the function to convert the string to a numeric value. Output should be displayed in the following form. You must be able to display at least a total of 16 digits plus a minus sign if applicable. The number may have no digits before the decimal point, no digits after the decimal point or digits before and after the decimal point.

For example if the file contain the following strings, you would output the following:

input was: 6789

6789 is an integer number, it contains 4 digits

input was: -5436789

-5436789 is an integer number, it contains 7 digits

input was 89563.25

89563.25 is a floating point number, it contains 7 total digits, 5 digits before decimal point, 2 digits after decimal point

input was .4444433333

.4444433333 is a floating point number, it contains 10 total digits, 0 digits before decimal point, 10 digits after decimal point

input was: .000040376

.000040376 is a floating point number, it contains 9 total digits, 0 digits before decimal point, 9 digits after decimal point

input was: 854623585.

85461423585. is a floating point number, it contains 11 total digits, 11 digits before decimal point, 0 digits after decimal point

Name the file you create LAB3. Assignment due September 10, 2002. Hand in a hard copy of the source code, and a floppy on which is saved the source (cpp file) and object code (exe file).

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download