Thursday, 11 August 2016

Chapter 4 // Exercise 19, 20, 21 - Principles & Practice Using C++

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 4 Exercise // 4.19

Write a program where you first enter a set if name-and-value pairs, such as Joe 17 and Barbara 22. For each pair, add the name to a vector called names and the  number to a vector called scores (in corresponding positions, so that if names[7]=="Joe" then scores[7]=="17"). Terminate input with NoName 0. Check that each name is unique and terminate with an error message if a name is entered twice. Write out all the (name,score) pairs, one per line.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

int main()
{
vector<string> names;
vector<int> scores;

string inputName;
int inputScores;
char loop = 'y';

cout << "Please enter a name followed by the score. To stop input enter 'NoName' in name and 0 in scores.\n";

//get names and scores
while (loop == 'y')
{
cout << "Name: ";
cin >> inputName;
cout << "Score: ";
cin >> inputScores;

for (int i = 0; i < names.size(); ++i)
{
if (names[i] == inputName)
{
cout << "Sorry, that name has already been entered. Please Re-Name it: \n" << endl;
cin >> inputName;
}
}

if (inputName == "NoName" && inputScores == 0)
{
loop = 'n';
}

names.push_back(inputName);
scores.push_back(inputScores);
}

cout << '\n';

//print them out
for (int x = 0; x < names.size()-1; ++x)
{
cout << names[x] << '\t' << scores[x] << '\n';
}

keep_window_open();

return 0;
}


Chapter 4 Exercise // 4.20

Modify the program from exercise 19 so that when you enter a name, the program will output the corresponding score or name not found.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

int main()
{
vector<string> names;
vector<int> scores;

string inputName;
int inputScores;
char loop = 'y';

cout << "Please enter a name followed by the score. To stop input enter 'NoName' in name and 0 in scores.\n";

//get names and scores
while (loop == 'y')
{
cout << "Name: ";
cin >> inputName;
cout << "Score: ";
cin >> inputScores;

for (int i = 0; i < names.size(); ++i)
{
if (names[i] == inputName)
{
cout << "Sorry, that name has already been entered. Please Re-Name it: \n" << endl;
cin >> inputName;
}
}

if (inputName == "NoName" && inputScores == 0)
{
loop = 'n';
}

names.push_back(inputName);
scores.push_back(inputScores);
}

cout << '\n';

string findName;
loop = 'y';
char nameFound = 's';

while (loop == 'y')
{
cout << "Whose score do you want to find?: \n";
cin >> findName;

for (int x = 0; x < names.size() - 1; ++x)
{
if (names[x] == findName)
{
cout << names[x] << '\t' << scores[x] << '\n';
nameFound = 'y';
}
}

loop = 'n';

if (nameFound != 'y')
{
cout << "Sorry, name not found. Do you what to try again? y/n: \n";
cin >> loop;
}

}

keep_window_open();

return 0;
}



Chapter 4 Exercise // 4.21

Modify the program from exercise 19 so that when you enter an integer, the program will output all the names with that score or score not found.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

int main()
{
vector<string> names;
vector<int> scores;

string inputName;
int inputScores;
char loop = 'y';

cout << "Please enter a name followed by the score. To stop input enter 'NoName' in name and 0 in scores.\n";

//get names and scores
while (loop == 'y')
{
cout << "Name: ";
cin >> inputName;
cout << "Score: ";
cin >> inputScores;

for (int i = 0; i < names.size(); ++i)
{
if (names[i] == inputName)
{
cout << "Sorry, that name has already been entered. Please Re-Name it: \n" << endl;
cin >> inputName;
}
}

if (inputName == "NoName" && inputScores == 0)
{
loop = 'n';
}

names.push_back(inputName);
scores.push_back(inputScores);
}

cout << '\n';

int findScore;
loop = 'y';
char scoreFound = 's';

while (loop == 'y')
{
cout << "Please enter a score to find: \n";
cin >> findScore;

for (int x = 0; x < scores.size() - 1; ++x)
{
if (scores[x] == findScore)
{
cout << names[x] << '\t' << scores[x] << '\n';
scoreFound = 'y';
}
}

loop = 'n';

if ( scoreFound != 'y')
{
cout << "Sorry, no scores found. Do you what to try again? y/n: \n";
cin >> loop;
}

}

keep_window_open();

return 0;
}

And so the chapter 4 exercises were rounded out by some nice easy questions. I'll be honest, when I first read them I honestly didn't think I'd be able to solve them. I just thought they were too hard and impossible to do at my level. When I started exercise 1 I still didn't understand vectors all that well or loops. I didn't even know how to print a vector or compare them, now I can do all those things. It's taken me around 2 months to fully work through this chapter but I work full time and some of these exercises would take me around 6 hours to solve.

It feels good though,when you do solve them. And it's amazing to see just how far I've come in 2 months.

No comments:

Post a Comment