Home‎ > ‎Assignments‎ > ‎Hour of Code‎ > ‎Solutions‎ > ‎

Loss Gain | Solution


// Your Loss, Our Gain

#include <iostream>

#include <string>

using namespace std;

 

void main(){

int before, after, loss, maxloss, minloss;

string name, winner, worst;

winner="none";

worst="none";

cout << "At the 'New client?' prompt, enter a client's name. \n"

               << "After the last client, enter 'end'.\n" << endl ;

cout << "New client?  ";                                  //First client only

cin >> name;                                                                   

if (name != "end"){

               cout << "Starting weight? ";

               cin >> before;

               cout << "Ending weight? ";

               cin >> after;

               loss = before - after;

               cout << name << " lost " << loss << "lbs.\n" << endl;

               maxloss = loss;

               minloss = loss;

               winner = name;

               worst = name;

               cout << "\nNew client?  ";                              //Second client only

               cin >> name;

               while (name != "end") {

                              cout << "Starting weight? ";

                              cin >> before;

                              cout << "Ending weight? ";

                              cin >> after;

                              loss = before - after;

                              if (loss > maxloss){

                                             maxloss = loss;

                              winner = name;}

                              else

                                             if (loss < minloss) {

                                             minloss = loss;

                                             worst = name;}

                              cout << name << " lost " << loss << " lbs.\n" << endl;

                              cout << "\nNew client?  ";

                              cin >> name;

                              } //end while

               cout << "\n\n";

               cout << "Congratulations, " << winner

<< " you have won the top prize!\n"

                              << "You lost the most weight:  " << maxloss << " lbs.\n";

               cout << "Sorry, " << worst << " you lost the least weight:  "

                              << minloss << " lbs. Try harder." << endl;

}

else

cout << "There was no data." << endl;

 

cout <<"\n\n\nType any key and press Enter to close this window.";

cin >> name;

return;

}




Comments