Spring 2025 [Bono]
We will still use the C++ compiler here (so we will use the usual compile command for a single-file C++ program), but because the point of the lab is to work more with C, you will be restricted on C++ specific features you can use. You may use only the following C++ features in your code for this lab:
char str[10]; char *str2;So str, defined above, would be able to hold at most a 9-char string, such as "ideologue", but does not have room for "ideologues" because of the null character. Because those two types (array and pointer) are somewhat interchangeable, you can use either kind as a parameter to C-string functions. Unlike other functions that take an array as a parameter, the C-string functions generally do not need the size passed to them, because they assume there will be the terminating null char in the C string.
Take a look at the starter code in phone.cpp: it reads from standard input
line by line, until it reaches end of file, just echoing out each line
to the screen. You can provide input from the keyboard, or you can
use input redirection from a text file. Compile and run the code,
with input from the keyboard, and again with input from one of the
data files provided.
You're now going to make some changes to phone.cpp so it can parse phone numbers. Assume the input on each line will be a single phone number with the format shown by example below:
213-740-4510For each such line of input, parse the line into it's three constituent parts: area code (the first 3 digit chars), prefix (the second 3 digit chars), and line number (the last 4 digit chars). Each one of these will be a C-string with the names shown in the code below. I.e., the parts will be C strings, not ints. Print out the parts using the following lines of code:
cout << "area code: '" << areaCode << "' " << endl;
cout << "prefix: '" << prefix << "' " << endl;
cout << "line number: '" << lineNumber << "'" << endl;
You may assume that the input will be in the correct format (i.e., you
do not have to do any error-checking.) You may also leave in the
"LINE READ" statement that echoes out the whole line of input. Note: there
are a few useful named constants defined for you already in the code.
Test your code on input from the keyboard, and on the test data file inPhone. Do a run such that you save the output from inPhone into a file called outPhone.
If you are a more experienced C programmer, and want to use a different interface that uses pointer arithmetic, you may do so.
Enhance namePhone.cpp so that it now can read a name and phone number from each line, saving the name as one field, and the three parts of the phone number into the three other fields, like before. The format of a line will be as follows:
George H. W. Bush:713-255-9190
So, to generalize, the name is not a fixed width, can consist of 0 or more words, and will be delimited by the ':' character. The phone number will appear directly after that ':', and will have the same format as described for the last two exercises.
Print out the name using the following line of code
cout << "name read: '" << name << "'" << endl;
So the complete output for this line of input would be:
LINE READ: George H. W. Bush:713-255-9190 name: 'George H. W. Bush' area code: '713' prefix: '255' line number: '9190'
Test your code on input from the keyboard, and on the test data file inNamePhone. Do a run such that you save the output from inNamePhone into a file called outNamePhone.