11.2: Focus on Software Engineering: Combining Data into Structures: 10976

Question

Declare a structure whose tag name is Book and that contains exactly three fields (or members), each of type int. The first field is nCopies, the second field is nPages and the third field is nAuthors.

Solution
struct Book
{
    int nCopies, nPages, nAuthors;
};

11.2: Focus on Software Engineering: Combining Data into Structures: 11005

Question

Given the declaration of a structure whose tag is DATE write the declaration of the following variables enrolled_on, paid_on, and completed_on, each of which is of type DATE.

Solution
DATE enrolled_on, paid_on, completed_on;

CH10: Programming Challenge #18

Question

10.18: Phone Number List
Write a program that has an array of at least 50 string objects that hold people’s names and phone numbers. The program then reads lines of text from a file named phonebook  into the array.

The program should ask the user to enter a name or partial name to search for in the array. All entries in the array that match the string entered should be displayed-- thus there may be more than one entry displayed.

Prompts And Output Labels. The program prints the message "Enter a name or partial name to search for: " and then after the user enters a some input and hits return, the program skips a line, and prints the heading: "Here are the results of the search:", followed by each matched string in the array on a line by itself.

Input Validation. None.

Solution (Raezorane R.)


#include 
#include 
#include 
using namespace std;

int main()
{
const int SIZE = 50;
string phoneDirectory[SIZE];
int size=0;
string name; //name to look for

ifstream inFile;
inFile.open("phonebook");
while (!inFile.fail()) {
getline(inFile,phoneDirectory[size]);
size++;
}
inFile.close();

// Get a name or partial name to search for.
cout << "Enter a name or partial name to search for: ";
getline(cin, name);

cout << "\nHere are the results of the search: " << endl;
int numberEntriesFound = 0;

for (int k = 0; k < size; k++)
{
if (phoneDirectory[k].find(name.data(), 0) < phoneDirectory[k].length())
{
numberEntriesFound ++;
cout << phoneDirectory[k] << endl;
}
}
if (numberEntriesFound == 0)
cout << "\nNo Entries were found for " << name;
return 0;


}

CH10: Programming Challenge #15

Question

10.15: Character Analysis
Write a program that reads the contents of a file named text.txt and determines the following:
The number of uppercase letters in the file
The number of lowercase letters in the file
The number of digits in the file

Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following prompts (respectively): "Uppercase characters: ", "Lowercase characters: ", "Digits: ".

Input Validation. None.

Solution (By Raezorane R.)

// Chapter 10, Programming Challenge 15: Character Analysis
#include 
#include 
using namespace std;

int main()
{
char ch; // To hold a character from the file

// Counters for the number of uppercase characters,
// lowercase characters, and digits found in the file.
int uppercase = 0;
int lowercase = 0;
int digits = 0;

// Open the file.
ifstream inputFile;
inputFile.open("text.txt");

// Read each character in the file
// and analyze it.
while (inputFile >> ch)
{
if (isupper(ch))
uppercase++;

if (islower(ch))
lowercase++;

if (isdigit(ch))
digits++;
}

// Close the file.
inputFile.close();

// Display the results.
cout << "Uppercase characters: " << uppercase << endl;
cout << "Lowercase characters: " << lowercase << endl;
cout << "Digits: " << digits << endl;

return 0;


10.7: More about the C++ string Class: 10864

Question

Assume that  word is a variable of type  string that has been assigned a value. Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip".

Assume that there is another variable declared,  drWord , also of type  string . Write the statements needed so that the 4-character substring word of the value of  word starting with "dr" is assigned to  drWord . So, if the value of  word were "George slew the dragon" your code would assign the value "drag" to  drWord .

Solution
drWord = word.substr(word.find("dr"),4);

10.7: More about the C++ string Class: 10847

Question

Assume that  name is a variable of type  string that has been assigned a value. Write an expression whose value is the first character of the value of  name . So if the value of  name were "Smith" the expression's value would be 'S'.

Solution
name[0]