Difference: SolutionCodeName (1 vs. 2)

Revision 22018-04-12 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="Spring2017"

In Class Activity

Classes, Friends, Operators
Line: 21 to 21
  int main() { Name yourName, bachName("Johann", "Sebastian", "Bach");
Changed:
<
<
do {
>
>
  cout << "Enter a name (2 or 3 words)"; yourName.input(cin);

cout << "The names: " << endl << "1. ";

Changed:
<
<
bachName.output(cout);
>
>
cout << bachName;
  cout << endl << "2. "; yourName.output(cout); cout << endl << endl;
Deleted:
<
<
// if (yourName.equalLast(bachName)) { // if (equalLast(yourName,bachName)) {
  if (yourName==bachName) { cout << "Same Last Names!!!" << endl; } else { cout << "Not the same Last Names!!!" << endl; }
Deleted:
<
<
} while (1);
 
Added:
>
>
if (yourName<=bachName) { cout << "Your name before or equal to Bach!!!" << endl; } else { cout << "Bach first!!!" << endl; }
 }
Added:
>
>
 %ENDCODE%

Name.h

Line: 58 to 60
  Name(); Name(string f, string m, string l); void input(istream& s);
Changed:
<
<
bool equalLast(Name n);
>
>
friend bool equalLast(Name n1, Name n2); friend bool operator==(Name n1, Name n2);
  void output(ostream& s);
Added:
>
>
friend ostream& operator<<(ostream& s, Name n);
  string getLast();
Changed:
<
<
friend bool operator==(Name n1, Name n2);
>
>
friend bool operator<=(Name n1, Name n2);
  private: string first;
Line: 69 to 73
  string last; };
Changed:
<
<
#endif /* NAME_H */%ENDCODE%
>
>
#endif /* NAME_H */ %ENDCODE%
 

Name.cpp

Line: 79 to 84
 #include #include #include
Added:
>
>
#include
 #include "Name.h"

using namespace std;

Line: 96 to 102
  s << first + " " + middle + " " + last; }
Added:
>
>
ostream& operator<<(ostream& s, Name n) { s << n.first + " " + n.middle + " " + n.last; return s; }
 void Name::input(istream& s) { first = middle = last = ""; string nameLine;
Added:
>
>
vector names;
  getline(s,nameLine);
Deleted:
<
<
cout << nameLine << endl;
 
Changed:
<
<
size_t pos1 = nameLine.find(' '); if (pos1 == string::npos) { if (nameLine.length()==0) { cout << "Nothing entered!!!!" << endl; } else { last = nameLine; } return;
>
>
istringstream nameStream(nameLine); do { string name; nameStream >> name; names.push_back(name); } while (nameStream);

int nc = names.size() - 1; switch (nc) { case 0: cout << "Blank name!" << endl; break; case 1: first = middle = ""; last = names.at(0); break; case 2: middle = ""; first = names.at(0); last = names.at(1); break; case 3: first = names.at(0); middle = names.at(1); last = names.at(2); break; default: first = names.at(0); last = names.at(nc - 1); for (int i = 1; i < nc - 1; i++) { middle += names.at(i) + " ";

  }
Deleted:
<
<
// more than one name first = nameLine.substr(0,pos1);

size_t pos2 = nameLine.find(' ',pos1+1); if (pos2 == string::npos) { // this is the last name (no middle name) last = nameLine.substr(pos1+1); return;

  }
Deleted:
<
<
// There is a middle and last name middle = nameLine.substr(pos1+1,pos2-pos1-1); last = nameLine.substr(pos2+1);

// ...

 }

string Name::getLast() { return last; }

Changed:
<
<
bool Name::equalLast(Name n) { return (last == n.getLast());
>
>
bool equalLast(Name n1, Name n2) { return (n1.last == n2.last);
 }

bool operator==(Name n1, Name n2) {

Deleted:
<
<
//return ( n1.getLast()==n2.getLast());
  return (n1.last==n2.last); }
Added:
>
>
bool operator<=(Name n1, Name n2) { cout << n1.last << n2.last << endl; if (n1.last < n2.last) { return true; } else if (n1.last == n2.last) { if (n1.first < n2.first) { return true; } else if (n1.middle <= n2.middle) { return true; } } return false; }
 %ENDCODE% \ No newline at end of file

Revision 12017-04-27 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Spring2017"

In Class Activity

Classes, Friends, Operators

We will be writing code to use Friends and Operators

  1. Finish the name input routine
  2. Add a version of equalLast with 2 Name parameters.
  3. Get == to work on last name
  4. get <= to work on whole name (last name first)

main.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include "Name.h"

using namespace std;

int main() {
    Name yourName, bachName("Johann", "Sebastian", "Bach");
    do {
    cout << "Enter a name (2 or 3 words)";
    yourName.input(cin);

    cout << "The names: " << endl << "1. ";
    bachName.output(cout);
    cout << endl << "2. ";
    yourName.output(cout);
    cout << endl << endl;

//    if (yourName.equalLast(bachName)) {
//    if (equalLast(yourName,bachName)) {
    if (yourName==bachName) {
        cout << "Same Last Names!!!" << endl;
    } else {
        cout << "Not the same Last Names!!!" << endl;
    }
    }
    while (1);

}
<-- end SyntaxHighlightingPlugin -->

Name.h

<-- SyntaxHighlightingPlugin -->
#ifndef NAME_H
#define NAME_H

using namespace std;

class Name {
public:
    Name();
    Name(string f, string m, string l);
    void input(istream& s);
    bool equalLast(Name n);
    void output(ostream& s);
    string getLast();
    friend bool operator==(Name n1, Name n2);
    
private:
    string first;
    string middle;
    string last;    
};

#endif /* NAME_H */
<-- end SyntaxHighlightingPlugin -->

Name.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
#include "Name.h"

using namespace std;

Name::Name() {
}

Name::Name(string f, string m, string l) {
    first = f;
    middle = m;
    last = l;
}

void Name::output(ostream& s) {
    s << first + " " + middle + " " + last;
}

void Name::input(istream& s) {
    first = middle = last = "";
    string nameLine;
    getline(s,nameLine);
    cout << nameLine << endl;
    
    size_t pos1 = nameLine.find(' ');
    if (pos1 == string::npos) {
        if (nameLine.length()==0) {
            cout << "Nothing entered!!!!" << endl;
        } else {
            last = nameLine;
        }
        return;
    }
    // more than one name
    first = nameLine.substr(0,pos1);
    
    size_t pos2 = nameLine.find(' ',pos1+1);
    if (pos2 == string::npos) {
        // this is the last name (no middle name)
        last = nameLine.substr(pos1+1);
        return;
    }
    // There is a middle and last name
    middle = nameLine.substr(pos1+1,pos2-pos1-1);
    last = nameLine.substr(pos2+1);
            

 // ...
}

string Name::getLast() {
    return last;
}

bool Name::equalLast(Name n) {
    return (last == n.getLast());
}

bool operator==(Name n1, Name n2) {
   //return ( n1.getLast()==n2.getLast());
    return (n1.last==n2.last);
}
<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback