Solution

/*

Programmer: Jennifer Leopold

Date: September 6, 2016

File: hw3.cpp

Purpose: Simulate a jazz charity marathon where each

musical note played earns points to feed a

certain animal. Output the sound of each note

played throughout the marathon, and the total

number of points earned for each animal.

To compile: fg++ hw3.cpp -o hw3

To execute: ./hw3

*/

#include <iostream>

using namespace std;

// Number of musical notes

const int NUM_NOTES = 7;

// Musical notes (encoded as integers)

const int NOTE_A = 0;

const int NOTE_B_FLAT = 1;

const int NOTE_C_PLUS = 2;

const int NOTE_D_MINUS = 3;

const int NOTE_E_SHARP = 4;

const int NOTE_F_FLAT = 5;

const int NOTE_H = 6;

// Sound associated with each musical note

const string SOUND_A = "wah";

const string SOUND_B_FLAT = "bleep";

const string SOUND_C_PLUS = "do-wah";

const string SOUND_D_MINUS = "ding";

const string SOUND_E_SHARP = "honk";

const string SOUND_F_FLAT = "wapp";

const string SOUND_H = "DOH!";

// Animal that earns points for each musical note

const string ANIMAL_A = "El Chupacabra";

const string ANIMAL_B_FLAT = "The bucktoothed slugs";

const string ANIMAL_C_PLUS = "Freaky fish";

const string ANIMAL_D_MINUS = "3-legged snakes";

const string ANIMAL_E_SHARP = "Trash bears";

const string ANIMAL_F_FLAT = "Hans";

const string ANIMAL_H = "Cletus";

// Valid range for the "inspiration number"

const int MIN_INSPIRATION_NUM = 10;

const int MAX_INSPIRATION_NUM = 100;

// Values for modifying "inspriation number"

const int EVEN_INSPIRATION_MODIFIER = 2;

const int ODD_INSPIRATION_MODIFIER = 3;

// Value of inspiration_num that ends the marathon

const int UNINSPIRED = 1;

int main()

{

int animal_A_pts = 0; // points earned by each animal

int animal_B_Flat_pts = 0;

int animal_C_Plus_pts = 0;

int animal_D_Minus_pts = 0;

int animal_E_Sharp_pts = 0;

int animal_F_Flat_pts = 0;

int animal_H_pts = 0;

bool valid_input = false; // used to validate user input

int inspiration_num; // determines each note played

// and termination of marathon

int note; // musical note to be played

// Greeting

cout << "Welcome to the Snax-A-Thon!\n\n";

// Get input for the "inspiration number" which, in part,

// will determine how long the marathon runs

while (! valid_input)

{

cout << "Enter the inspiration number to get things "

<< "started: ";

cin >> inspiration_num;

valid_input = (inspiration_num >= MIN_INSPIRATION_NUM) &&

(inspiration_num <= MAX_INSPIRATION_NUM);

if (! valid_input)

cout << "Invalid input! Inspiration number must be "

<< MIN_INSPIRATION_NUM << "-"

<< MAX_INSPIRATION_NUM << " (inclusive).\n\n";

}

// Continue playing notes until inspiration_num becomes

// UNINSPIRED

while (inspiration_num != UNINSPIRED)

{

note = inspiration_num % NUM_NOTES;

// Output the sound of the current note and increment points

// for the associated animal

if (note == NOTE_A)

{

cout << SOUND_A;

animal_A_pts++;

}

else if (note == NOTE_B_FLAT)

{

cout << SOUND_B_FLAT;

animal_B_Flat_pts++;

}

else if (note == NOTE_C_PLUS)

{

cout << SOUND_C_PLUS;

animal_C_Plus_pts++;

}

else if (note == NOTE_D_MINUS)

{

cout << SOUND_D_MINUS;

animal_D_Minus_pts++;

}

else if (note == NOTE_E_SHARP)

{

cout << SOUND_E_SHARP;

animal_E_Sharp_pts++;

}

else if (note == NOTE_F_FLAT)

{

cout << SOUND_F_FLAT;

animal_F_Flat_pts++;

}

else

{

cout << SOUND_H;

animal_H_pts++;

}

cout << " ";

// Calculate the next inspiration_num

if (inspiration_num % 2 == 0)

inspiration_num /= EVEN_INSPIRATION_MODIFIER;

else inspiration_num = inspiration_num *

ODD_INSPIRATION_MODIFIER + 1;

}

// Output number of points each animal earned

cout << "\n\nThe results of the Snax-A-Thon are as follows:\n";

cout << ANIMAL_A << " earned "

<< animal_A_pts << " snacks\n";

cout << ANIMAL_B_FLAT << " earned "

<< animal_B_Flat_pts << " snacks\n";

cout << ANIMAL_C_PLUS << " earned "

<< animal_C_Plus_pts << " snacks\n";

cout << ANIMAL_D_MINUS << " earned "

<< animal_D_Minus_pts << " snacks\n";

cout << ANIMAL_E_SHARP << " earned "

<< animal_E_Sharp_pts << " snacks\n";

cout << ANIMAL_F_FLAT << " earned "

<< animal_F_Flat_pts << " snacks\n";

cout << ANIMAL_H << " earned "

<< animal_H_pts << " snacks\n";

// Sign-off

cout << "\nThank you for your support!\n";

return 0;

}