Chapter 1 #include “stdafx.h” using namespace System; #include using namespace std; int main( ^args) { cout << "Hello, World!" << endl; Console::ReadKey(); return 0; } Chapter 3 // Chapter3.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int grade1, grade2, grade3, grade4, grade5, sumOfGrades, numOfGrades; double average = 0.0; grade1 = 84; grade2 = 89; grade3 = 77; grade4 = 94; grade5 = 88; numOfGrades = 5; sumOfGrades = grade1 + grade2 + grade3 + grade4 + grade5; average = sumOfGrades / numOfGrades; cout << "The average is: " << average << endl; Console::ReadKey(); return 0; } Chapter 4 // avgGrades.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int grade1, grade2, grade3, grade4, grade5, totalGrades; double average; const int numGrades = 5; grade1 = 78; grade2 = 85; grade3 = 92; grade4 = 99; grade5 = 81; totalGrades = grade1 + grade2 + grade3 + grade4 + grade5; average = totalGrades / numGrades; cout << "The average is: " << average << endl; Console::ReadKey(); return 0; } // countCoins.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { double total; const double penny = .01; const double nickel = .05; const double dime = .10; const double quarter = .25; total = (3 * quarter) + (6 * dime) + (5 * nickel) + (7 * penny); cout << "The total amount of change is: $" << total << endl; Console::ReadKey(); return 0; } // votePercent.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { const int totalVotes = 500000; const double candVotePercent = .60; const double inValidPercent = .15; double validVotes, candVotes; validVotes = totalVotes - (totalVotes * inValidPercent); candVotes = validVotes * candVotePercent; cout << "The candidate received " << candVotes << " votes."; Console::ReadKey(); return 0; } Chapter 6 // GradeRange.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { int numGrade; string letterGrade; cout << "Enter a numeric grade: "; cin >> numGrade; if ((numGrade >= 97) && (numGrade <= 100)) letterGrade = "A+"; else if ((numGrade >= 94) && (numGrade <= 96)) letterGrade = "A"; else if ((numGrade >= 90) && (numGrade <= 93)) letterGrade = "A-"; else if ((numGrade >= 87) && (numGrade <= 89)) letterGrade = "B+"; else if ((numGrade >= 84) && (numGrade <= 86)) letterGrade = "B"; else if ((numGrade >= 80) && (numGrade <= 83)) letterGrade = "B-"; else if ((numGrade >= 77) && (numGrade <= 79)) letterGrade = "C+"; else if ((numGrade >= 74) && (numGrade <= 76)) letterGrade = "C"; else if ((numGrade >= 70) && (numGrade <= 73)) letterGrade = "C-"; else if ((numGrade >= 67) && (numGrade <= 69)) letterGrade = "D+"; else if ((numGrade >= 64) && (numGrade <= 66)) letterGrade = "D"; else if ((numGrade >= 60) && (numGrade <= 63)) letterGrade = "D-"; else letterGrade = "F"; cout << "The letter grade is " << letterGrade << "." << endl; Console::ReadKey(); return 0; } // pigLatin.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { string word, letter; cout << "Enter a word: "; cin >> word; letter = word.substr(0,1); if ((letter == "a") || (letter == "e") || (letter == "i") || (letter == "o") || (letter == "u")) cout << letter << " is a vowel"; else cout << letter << " is a consonant"; Console::ReadKey(); return 0; } Chapter 7 // WhileLoop.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int number, sum; number = 1; sum = 0; while (number <= 10) { sum += number; ++number; } cout << "The sum of 1 – 10 is " << sum << endl; Console::ReadKey(); return 0; } // textFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { ifstream myFile; string line; myFile.open("example.txt"); while (!myFile.eof()) { getline(myFile, line); cout << line << endl; } myFile.close(); Console::ReadKey(); return 0; } // readTemps.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ifstream temps; int temp, totalTemps, numTemps; double average; totalTemps = numTemps = 0; temps.open("c:\\data\\temperatures.txt"); while (!temps.eof()) { temps >> (int)temp; // cast to int totalTemps += temp; ++numTemps; } average = totalTemps / numTemps; cout << "The average temperature is: "<< average << endl; Console::ReadKey(); return 0; } // AverageGrade.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int testScore, total, count; double average; total = 0; count = 0; cout << "Enter a test score (-1 to quit): "; cin >> testScore; while (testScore != -1) { total += testScore; ++count; cout << "Enter a test score (-1 to quit): "; cin >> testScore; } average = total / count; cout << endl << "The average test score is: " << average << endl; Console::ReadKey(); return 0; } // ForLoop.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { for(int i = 1; i <= 10; ++i) { cout << i << " "; } Console::ReadKey(); return 0; } // ForLoop.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { double rate = 1.02; double principal = 10000; for(int i = 1; i <=10; ++i) { principal *= rate; } cout << "After 10 years, $10,000 invested at 2%" << " annually will be worth: $" << principal << endl; Console::ReadKey(); return 0; } // ForLoop.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { string sentence = "Now is the time for all good" sentence += " people to come to the aid of " sentence += " their party."; int vowelCount = 0; string letter = ""; int strLen = sentence.size(); for(int i = 0; i < strLen; ++i) { letter = sentence.substr(i,1); if ((letter.compare("a") == 0) || (letter.compare("e") == 0) || (letter.compare("i") == 0) || (letter.compare("o") == 0) || (letter.compare("u") == 0)) ++vowelCount; } cout << "Number of vowels in the sentence is: " << vowelCount << endl; Console::ReadKey(); return 0; } // ForLoop.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { int product; for(int leftOp = 1; leftOp <= 5; ++leftOp) { for(int rightOp = 1; rightOp <= 5; ++ rightOp) { product = leftOp * rightOp; cout << leftOp << " x " << rightOp << " = " << product << "\t"; } cout << endl; } Console::ReadKey(); return 0; } Chapter 8 // Vector.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { vector numbers; for(int i = 1; i <= 10; ++i) { numbers.push_back(i); } int sum = 0; for(int i = 0; i < numbers.size(); ++i) { sum += numbers[i]; } cout << "The sum of the integers 1 to 10 is: " << sum << endl; Console::ReadKey(); return 0; } // RandomNumber.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { srand(time(NULL)); int numberToGuess = rand() % 10 + 1; int guess; cout << "Enter a guess: "; cin >> guess; if (guess == numberToGuess) cout << "Right!"; else cout << "Wrong. The number was " << numberToGuess; Console::ReadKey(); return 0; } // Vector.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include #include using namespace std; int main(array ^args) { vector numbers; srand(time(NULL)); for(int i = 1; i <= 1000; ++i) { numbers.push_back(rand() % 100 + 1); } // display the numbers in the vector for(int i = 0; i < numbers.size(); ++i) { cout << numbers[i] << " "; } unsigned int sum = 0; for(int i = 0; i < numbers.size(); ++i) { sum += numbers[i]; } cout << endl << endl << "The sum is: " << sum << endl; Console::ReadKey(); return 0; } Chapter 9 // square.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int square(int number) { return number * number; } int main(array ^args) { int num; cout << "Enter a number to square: "; cin >> num; cout << num << " squared is " << square(num) << endl; Console::ReadKey(); return 0; } // Power.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int power(int base, int exp) { int number = 1; for(int i = 1; i <= exp; ++i) { number *= base; } return number; } int main(array ^args) { for(int i = 1; i <= 10; ++i) { cout << i << "\t" << power(2,i) << endl; } Console::ReadKey(); return 0; } // Swap.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; void swap(string word1, string word2) { string temp = word1; word1 = word2; word2 = temp; } int main(array ^args) { string word1, word2; word1 = "banana"; word2 = "apple"; cout << word1 << " " << word2 << endl; swap(word1, word2); cout << word1 << " " << word2 << endl; Console::ReadKey(); return 0; } // Swap.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; void swap(string &word1, string &word2) { string temp = word1; word1 = word2; word2 = temp; } int main(array ^args) { string word1, word2; word1 = "banana"; word2 = "apple"; cout << word1 << " " << word2 << endl; swap(word1, word2); cout << word1 << " " << word2 << endl; Console::ReadKey(); return 0; } // ScopeExample.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int changeNumber(int number) { number *= 3; cout << "The value of number in the changeNumber()" cout << " function is: " << number << endl; return number; } int main() { int number = 3; cout << changeNumber(number); cout << endl << "The value of number in the main()" cout << " function is: " << number << endl; Console::ReadKey(); return 0; } // GlobalScope.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int number = 1; // global variable int changeNumber() { number += 5; return number; } int main(array ^args) { cout << "The value of number is: " << number << endl; number = changeNumber(); cout << "Now the value of number is: " << number << endl; Console::ReadKey(); return 0; } // BlockScope.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { for(int i = 1; i <= 10; ++i) { cout << "The value of i in the loop is: " << i << endl; } Console::ReadKey(); return 0; } // BlockScope.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int number = 1; cout << "The value of number is: " << number << endl; { int number = 100; cout << "The value of number in this block" << " is: " << number << endl; } number += 4; cout << "The value of number is: " << number << endl; Console::ReadKey(); return 0; } #include "stdafx.h" using namespace System; #include #include using namespace std; void DisplayVector(vector n) { for(int i = 0; i < n.size(); ++i) { cout << n[i] << " "; } cout << endl; } int main(array ^args) { vector numbers; for(int i = 1; i <= 10; ++i) { numbers.push_back(i); } DisplayVector(numbers); Console::ReadKey(); return 0; } // VectorFunctions.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include #include using namespace std; void DisplayVector(vector n) { for(int i = 0; i < n.size(); ++i) { cout << n[i] << " "; } cout << endl; } int SumVector(vector n) { int sum = 0; for(int i = 0; i < n.size(); ++i) { sum += n[i]; } return sum; } void CurveGrades(vector &n) { for(int i = 0; i < n.size(); ++i) { n[i] += 5; // add 5 to each vector element } } int main(array ^args) { vector grades; srand(time(NULL)); for(int i = 1; i <= 10; ++i) { grades.push_back(rand() % 100 + 1); } DisplayVector(grades); CurveGrades(grades); DisplayVector(grades); Console::ReadKey(); return 0; } Chapter 10 // Arrays.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int numbers[10]; const int sizeNumbers = 10; for(int i = 0; i < sizeNumbers; ++i) { numbers[i] = i + 1; } cout << "Element" << "\t" << "Value" << endl << endl; for(int i = 0; i < sizeNumbers; ++i) { cout << i << "\t" << numbers[i] << endl; } Console::ReadKey(); return 0; } // avgTemp.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { const int numTemps = 7; int temps[] = {85, 77, 81, 86, 89, 92, 87}; double average = 0.0; int total = 0; for(int i = 0; i < numTemps; ++i) { total += temps[i]; } average = total / numTemps; cout << "The average temperature was: " << average << endl; Console::ReadKey(); return 0; } // minMax.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int min, max; const int size = 100; int numbers[size]; srand(time(NULL)); // initialize the array for(int i = 0; i < size; ++i) { numbers[i] = rand() % 100 + 1; } min = numbers[0]; max = numbers[0]; // search for min and max for(int i = 0; i < (size-1); ++i) { if (numbers[i+1] < min) min = numbers[i+1]; if (numbers[i+1] > max) max = numbers[i+1]; } cout << "The minimum value in the array is " << min << endl << "The maximum value in the array is " << max << endl; // display the values for(int i = 0; i < size; ++i) { cout << numbers[i] << " "; } Console::ReadKey(); return 0; } // Array2D.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { const int rows = 5; const int cols = 5; int total = 0; double average = 0.0; int grades[rows][cols] = {{75, 82, 84, 79, 91}, {85, 81, 94, 96, 89}, {92, 91, 94, 89, 90}, {74, 72, 81, 78, 80}, {84, 82, 82, 83, 81}}; for(int r = 0; r < rows; ++r) { cout << "Student " << r+1 << ": "; for(int c = 0; c < cols; ++c) { cout << grades[r][c] << " "; total += grades[r][c]; } average = total / cols; cout << "Average: " << average << endl; total = 0; average = 0.0; } Console::ReadKey(); return 0; } // Array2D.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { const int rows = 5; const int cols = 5; int total = 0; double average = 0.0; int grades[rows][cols] = {{75, 82, 84, 79, 91}, {85, 81, 94, 96, 89}, {92, 91, 94, 89, 90}, {74, 72, 81, 78, 80}, {84, 82, 82, 83, 81}}; for(int r = 0; r < rows; ++r) { cout << "Student " << r+1 << ": "; for(int c = 0; c < cols; ++c) { cout << grades[r][c] << " "; total += grades[r][c]; } average = total / cols; cout << "Average: " << average << endl; total = 0; average = 0.0; } cout << endl << "Test Averages: " << endl; total = 0; average = 0.0; for(int c = 0; c < cols; ++c) { cout << "Test " << c+1 << ": "; for(int r = 0; r < rows; ++r) { cout << grades[r][c] << " "; total += grades[r][c]; } average = total / rows; cout << "Average: " << average << endl; total = 0; average = 0.0; } Console::ReadKey(); return 0; } Chapter 11 // NameClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; class Name { private: string first, middle, last; public: void setName(string firstName, string middleName, string lastName) { first = firstName; middle = middleName; last = lastName; } string getName() { return first + " " + middle + " " + last; } }; int main(array ^args) { Name aName; aName.setName("John", "Quincy", "Adams"); cout << aName.getName() << endl; Console::ReadKey(); return 0; } // NameClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; class Name { private: string first, middle, last; public: Name() { first = ""; middle = ""; last = ""; } Name(string firstName, string middleName, string lastName) { first = firstName; middle = middleName; last = lastName; } Name(string firstName, string lastName) { first = firstName; middle = ""; last = lastName; } Name(string lastName) { first = ""; middle = ""; last = lastName; } void setFirst(string firstName) { first = firstName; } void setMiddle(string middleName) { middle = middleName; } void setLast(string lastName) { last = lastName; } void setName(string firstName, string middleName, string lastName) { first = firstName; middle = middleName; last = lastName; } string getFirst() { return first; } string getMiddle() { return middle; } string getLast() { return last; } string getName() { return first + " " + middle + " " + last; } string getInitials() { return first.substr(0,1) + middle.substr(0,1) + last.substr(0,1); } }; int main(array ^args) { Name myName("Mary", "Alice", "Smith"); cout << myName.getName() << endl; // Mary gets married to Joe Brown myName.setLast("Brown"); cout << myName.getName() << endl; cout << "The first name in myName is: " << myName.getFirst() << endl; cout << "The middle name in myName is: " << myName.getMiddle() << endl; cout << "The last name in myName is: " << myName.getLast() << endl; cout << "myName's initials are: " << myName.getInitials() << endl; Console::ReadKey(); return 0; } // Name class header file #include using namespace std; class Name { private: string first, last, middle; public: Name(); Name(string, string, string); Name(string, string); Name(string); void setFirst(string); void setMiddle(string); void setLast(string); void setName(string, string, string); string getFirst(); string getMiddle(); string getLast(); string getName(); string getInitials(); }; // Name class source file #include "stdafx.h" using namespace System; #include "Name.h" Name::Name() { first = ""; middle = ""; last = ""; } Name::Name(string firstName, string middleName, string lastName) { first = firstName; middle = middleName; last = lastName; } Name::Name(string firstName, string lastName) { first = firstName; middle = ""; last = lastName; } Name::Name(string lastName) { first = ""; middle = ""; last = lastName; } void Name::setFirst(string firstName) { first = firstName; } void Name::setMiddle(string middleName) { middle = middleName; } void Name::setLast(string lastName) { last = lastName; } void Name::setName(string firstName, string middleName, string lastName) { first = firstName; middle = middleName; last = lastName; } string Name::getFirst() { return first; } string Name::getMiddle() { return middle; } string Name::getLast() { return last; } string Name::getName() { return first + " " + middle + " " + last; } string Name::getInitials() { return first.substr(0,1) + middle.substr(0,1) + last.substr(0,1); } // Test program for Name class // NameClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; #include "Name.h" int main(array ^args) { Name myName("Mary", "Alice", "Smith"); cout << myName.getName() << endl; // Mary gets married to Joe Brown myName.setLast("Brown"); cout << myName.getName() << endl; cout << "The first name in myName is: " << myName.getFirst() << endl; cout << "The middle name in myName is: " << myName.getMiddle() << endl; cout << "The last name in myName is: " << myName.getLast() << endl; cout << "myName's initials are: " << myName.getInitials() << endl; Console::ReadKey(); return 0; } // Header file for List class #include using namespace std; class List { private: vector datastore; int numItems; public: List(); void add(string); void clear(); void display(); int count(); }; // Source file for List class #include "stdafx.h" using namespace System; #include "List.h" List::List() { vector datastore; int numItems = 0; } void List::add(string item) { datastore.push_back(item); } void List::clear() { datastore.clear(); } string List::display() { string items = ""; for(int i = 0; i < datastore.size(); ++i) { items += datastore[i]; items += " "; } return items; } int List::count() { return datastore.size(); } // Test program for List class // ListClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; #include "List.h" int main(array ^args) { List groceries; groceries.add("milk"); groceries.add("bread"); groceries.add("eggs"); cout << "First list: " << endl; cout << groceries.display() << endl; cout << "Number of items in list: " << groceries.count() << endl; groceries.clear(); groceries.add("cereal"); groceries.add("coffee"); groceries.add("apples"); groceries.add("ice cream"); cout << endl << "Second list: " << endl; cout << groceries.display() << endl; cout << "Number of items in list: " << groceries.count() << endl; Console::ReadKey(); return 0; } Chapter 12 // CreateFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ofstream outFile("c:\\data\\grades.txt"); outFile << 85; outFile << endl; outFile << 77; outFile.close(); Console::ReadKey(); return 0; } // CreateFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ofstream outFile("c:\\data\\grades.txt"); int grade; cout << "Enter a grade (-1 to quit): "; cin >> grade; while (grade > -1) { outFile << grade << endl; cout << "Enter a grade (-1 to quit): "; cin >> grade; } outFile.close(); Console::ReadKey(); return 0; } // ReadFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ifstream inFile("c:\\data\\grades.txt"); int grade; inFile >> grade; cout << grade << endl; inFile >> grade; cout << grade << endl; inFile.close(); Console::ReadKey(); return 0; } // ReadFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ifstream inFile("c:\\data\\grades.txt"); int grade; while (!inFile.eof()) { inFile >> grade; cout << grade << endl; } inFile.close(); Console::ReadKey(); return 0; } // ReadFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ifstream inFile("c:\\data\\grades.txt"); int grade, total, numGrades; double average; total = numGrades = 0; while (!inFile.eof()) { inFile >> grade; cout << grade << " "; total += grade; ++numGrades; } inFile.close(); average = total / numGrades; cout << endl << "The average grade is: " << average << endl; Console::ReadKey(); return 0; } // AppendFile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; int main(array ^args) { ofstream outFile("c:\\data\\grades.txt", ios::app); outFile << endl; // next grade on new line int grade = 0; cout << "Enter a grade (-1 to quit): "; cin >> grade; while (grade > -1) { outFile << grade << endl; cout << "Enter a grade (-1 to quit): "; cin >> grade; } outFile.close(); Console::ReadKey(); return 0; } Chapter 13 // Switch.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { char letterGrade; cout << "Enter your letter grade: "; cin >> letterGrade; switch (letterGrade) { case 'A': cout << "A grade is in the range of 90-100." << endl; break; case 'B': cout << "B grade is in the range of 80-89." << endl; break; case 'C': cout << "C grade is in the range of 70-79." << endl; break; case 'D': cout << "D grade is in the range of 60-69." << endl; break; case 'F': cout << "A F grade is in the range of 0-59." << endl; break; default: cout << "The letter grade must be A-D," cout << " or F." << endl; } Console::ReadKey(); return 0; } // Switch.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { char letterGrade; cout << "Enter your letter grade: "; cin >> letterGrade; switch (letterGrade) { case 'A': case 'B': case 'C': case 'D': cout << "You passed the test!" << endl; break; case 'F': cout << "Sorry, you failed the test." << endl; break; default: cout << "Enter a valid letter grade." << endl; } Console::ReadKey(); return 0; } // isVowel.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; bool isVowel(char letter) { switch (letter) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return true; break; default: return false; } } int main(array ^args) { string sentence; char ltr; int numVowels = 0; cout << "Enter a sentence: "; getline(cin, sentence); for(int i = 0; i < sentence.size(); ++i) { ltr = sentence[i]; if (isVowel(ltr)) ++numVowels; } cout << "There are " << numVowels << " vowels in the sentence."; Console::ReadKey(); return 0; } // DoWhile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int op1, op2, result, answer; char quit; srand(time(NULL)); op1 = rand() % 100 + 1; op2 = rand() % 100 + 1; result = op1 + op2; cout << "What is " << op1 << " + " << op2 << "? "; cin >> answer; if (result == answer) cout << endl << "Right!"; else cout << "Sorry. That is wrong."; cout << "Another problem (y or n)?"; cin >> quit; while (quit != 'n') { op1 = rand() % 100 + 1; op2 = rand() % 100 + 1; result = op1 + op2; cout << "What is " << op1 << " + " << op2 << "? "; cin >> answer; if (result == answer) cout << endl << "Right! "; else cout << "Sorry. That is wrong."; cout << " Another problem (y or n)?"; cin >> quit; } cout << endl << "Thanks for practicing your addition." << endl; Console::ReadKey(); return 0; } // DoWhile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int op1, op2, result, answer; char quit; srand(time(NULL)); do { op1 = rand() % 100 + 1; op2 = rand() % 100 + 1; result = op1 + op2; cout << "What is " << op1 << " + " << op2 << "? "; cin >> answer; if (result == answer) cout << endl << "Right! "; else cout << "Sorry. That is wrong."; cout << "Another problem (y or n)?"; cin >> quit; } while (quit != 'n'); cout << endl << "Thanks for practicing your addition." << endl; Console::ReadKey(); return 0; } // DoWhile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { double balance, rate; int years = 0; balance = 5000; // Enter 1.02 for 2% cout << "What is the interest rate? "; cin >> rate; while (balance < 10000) { balance *= rate; ++years; } cout << "It will take " << years << " to reach $10,000.00." << endl; Console::ReadKey(); return 0; } // DoWhile.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { double balance, rate; int years = 0; balance = 5000; // Enter 1.02 for 2% cout << "What is the interest rate? "; cin >> rate; do { balance *= rate; ++years; } while (balance < 10000); cout << "It will take " << years << " to reach $10,000.00." << endl; Console::ReadKey(); return 0; } Chapter 14 // PointerExample1.cpp : main project file. #include "stdafx.h" using namespace System; #include using namespace std; int main(array ^args) { int num = 100; int *ptrNum; ptrNum = # cout << "The memory address of num: " << ptrNum << endl; cout << "The value stored in num: " << *ptrNum << endl; Console::ReadKey(); return 0; } // Swap.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; void swap(string &word1, string &word2) { string temp = word1; word1 = word2; word2 = temp; } int main(array ^args) { string word1, word2; word1 = "banana"; word2 = "apple"; cout << word1 << " " << word2 << endl; swap(word1, word2); cout << word1 << " " << word2 << endl; Console::ReadKey(); return 0; } // Swap.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; void swap(string *word1, string *word2) { string temp; temp = *word1; *word1 = *word2; *word2 = temp; } int main(array ^args) { string word1, word2; word1 = "banana"; word2 = "apple"; cout << word1 << " " << word2 << endl; swap(word1, word2); cout << word1 << " " << word2 << endl; Console::ReadKey(); return 0; } // PointersAndArrays.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int nums[100]; srand(time(NULL)); const int size = 100; for(int i = 0; i < size; ++i) nums[i] = rand() % 100 + 1; cout << nums[0] << endl; int *ptrNums; ptrNums = &nums[0]; cout << *ptrNums; Console::ReadKey(); return 0; } // PointersAndArrays.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int nums[100]; srand(time(NULL)); const int size = 100; for(int i = 0; i < size; ++i) nums[i] = rand() % 100 + 1; cout << nums[0] << endl; cout << *nums << endl; Console::ReadKey(); return 0; } // PointersAndArrays.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int nums[100]; const int size = 100; srand(time(NULL)); for(int i = 0; i < size; ++i) nums[i] = rand() % 100 + 1; cout << "The first element: " << *(nums+0) << endl; cout << "The second element: " << *(nums+1) << endl; cout << "Accessing the second element "; cout << " without parentheses: " << *nums+1 << endl; Console::ReadKey(); return 0; } // PointersAndArrays.cpp : main project file. #include "stdafx.h" using namespace System; #include #include #include using namespace std; int main(array ^args) { int nums[100]; const int size = 100; srand(time(NULL)); for(int i = 0; i < size; ++i) nums[i] = rand() % 100 + 1; for(int i = 0; i < size; ++i) cout << *(nums+i) << " "; Console::ReadKey(); return 0; } Chapter 15 // EmployeeClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; class Employee { private: string empName; double pay; public: Employee(string name, double payRate) { empName = name; pay = payRate; } string getName() { return empName; } double getPay() { return pay; } double grossPay(int hours) { return pay * hours; } }; int main(array ^args) { Employee emp1("Mary Smith", 15.0); cout << "Employee name: " << emp1.getName() << endl; cout << "Pay rate: " << emp1.getPay() << endl; cout << "Gross pay: " << emp1.grossPay(40) << endl; Console::ReadKey(); return 0; } // EmployeeClass.cpp : main project file. #include "stdafx.h" using namespace System; #include #include using namespace std; class Employee { protected: string empName; double pay; public: Employee(string name, double payRate) { empName = name; pay = payRate; } string getName() { return empName; } double getPay() { return pay; } double grossPay(int hours) { return pay * hours; } }; class Manager : public Employee { protected: bool salaried; public: Manager(string name, double payRate, bool isSalaried) : Employee(name, payRate) { salaried = isSalaried; } bool getSalaried() { return salaried; } double grossPay(double hours) { if (salaried) return pay; else return Employee::grossPay(hours); } }; int main(array ^args) { Employee emp1("Mary Smith", 15.0); cout << "Employee name: " << emp1.getName() << endl; cout << "Pay rate: " << emp1.getPay() << endl; cout << "Gross pay: " << emp1.grossPay(40) << endl; Manager emp2("Liz Warren", 1500, true); cout << "Employee name: " << emp2.getName() << endl; cout << "Pay rate: " << emp2.getPay() << endl; cout << "Gross pay: " << emp2.grossPay(40) << endl; Console::ReadKey(); return 0; } // Date class code only class Date { private: int month, day, year; public: Date() { month = 0; day = 0; year = 0; } Date(int m, int d, int y) { month = m; day = d; year = y; } void setDate(int m, int d, int y) { month = m; day = d; year = y; } string getDate() { stringstream dt; dt << month << "/" << day << "/" << year; return dt.str(); } }; // Employee class code only class Employee { private: Name empName; Date hireDate; public: Employee(string first, string middle, string last, int m, int d, int y) { empName.setName(first, middle, last); hireDate.setDate(m, d, y); } string getEmployee() { return empName.getName() + "\n" + hireDate.getDate() + "\n"; } };