Accelerated C++ Chapter 3 exercise 3.4

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    Accelerated C++ Chapter 3 exercise 3.4

    Hi

    I'm very new to programming and have purchased the book by Andrew Koenig and Barbara Moo. I am a tad frustrated at the moment because I cannot fiigure out the following from Q3.4 in the exercises:

    -> How do I calculate the size of the strings in a vector that I've created?
    -> How do I then keep the maximum and minumum of the strings the user
    enters to ouput the respective longest and shortest strings?

    Here is my code thus far :

    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <string>
    #include <vector>

    using std::cin; using std::sort;
    using std::cout; using std::endl;
    using std::string; using std::streamsize ;
    using std::vector; using std::setprecisi on;

    int main()
    {
    // ask for and read the user's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;

    // ask for and read family member's names
    cout << "Please enter all your family members names, "
    "followed by end-of-file: ";
    vector<string> family;
    string x;
    // invariant:famil y contains all family names read so far
    while (cin >> x)
    family.push_bac k(x);

    // check that the user entered some names
    typedef vector<string>: :size_type vec_sz;
    vec_sz size = family.size();
    if (size == 0) {
    cout << endl << "You must enter two or more family names. "
    "Please try again." << endl;
    system("PAUSE") ;
    return 1;
    }
    // sort the names
    sort(family.beg in(), family.end());

    // run through the vector elements using r rows of input
    int max = family[0].size();
    int r = 0;
    for (int r = 0; r != family.size(); ++r) {
    while (cin >> x)
    string::size_ty pe name_size = family[x].size();
    ??Aggghhh! Please help.}

    system("PAUSE") ;
    return 0;
    }


    If anyone can help I'd really really appreciate it.

    Thanks

    M
    Last edited by Madmartigan; Dec 15 '06, 03:42 PM. Reason: mistake in code
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by Madmartigan
    -> How do I calculate the size of the strings in a vector that I've created?
    -> How do I then keep the maximum and minumum of the strings the user
    enters to ouput the respective longest and shortest strings?
    Code:
    // run through the vector elements using r rows of input
    int max = family[0].size(); 
    int r = 0;
    for (int r = 0; r != family.size(); ++r) {
    while (cin >> x) 
    string::size_type name_size = family[x].size();
    ??Aggghhh! Please help.}
    The method you are using should be working - I think the problem you have is the while loop in the middle. Should you be using r to access each member of the vector? Once you get the size of a string in the vector, you can compare it to the value you have in max (and a min value) to update the size - or even better, the position of the string with the longest/shortest length.

    int max = 0;
    int min = 0;
    for (int r = 1; r < family.size(); r++) {
    if (family[r].length() > family[max].length()) max = r;
    if (family[r].length() < family[min].length()) min = r;
    }
    // max holds the position of the longest string in family
    // min holds the position of the shortest string in family

    Comment

    • Madmartigan
      New Member
      • Dec 2006
      • 23

      #3
      Thanks for the reply Ganon

      I think you put me on track with regards to using "r" to access each member
      of the vector. I eventually came up with the following which runs and does what I need it to, although I'm sceptical that its the most effcient way to compile this code :

      #include <iostream>
      #include <algorithm>
      #include <iomanip>
      #include <ios>
      #include <string>
      #include <vector>

      using std::cin; using std::sort;
      using std::cout; using std::endl;
      using std::string; using std::streamsize ;
      using std::vector; using std::setprecisi on;

      int main()
      {
      // ask for and read the user's name
      cout << "Please enter your first name: ";
      string name;
      cin >> name;
      cout << "Hello, " << name << "!" << endl;

      // ask for and read family member's names
      cout << "Please enter all your family members names, "
      "followed by end-of-file: ";
      vector<string> family;
      string x;
      // invariant:famil y contains all family names read so far
      while (cin >> x)
      family.push_bac k(x);

      // check that the user entered some names
      typedef vector<string>: :size_type vec_sz;
      vec_sz size = family.size();
      if (size == 0) {
      cout << endl << "You must enter two or more family names. "
      "Please try again." << endl;
      system("PAUSE") ;
      return 1;
      }
      // sort the names
      sort(family.beg in(), family.end());
      cout << endl;

      // declare variable of type iterator to read through all elements
      // declare min and max variables setting min to 100 and max to 0
      int max = 0;
      int min = 100;
      for (vector<string> ::iterator i = family.begin(); i != family.end(); ++i)
      {
      if ((*i).length() > max) { // work out the longest name by testing
      max = (*i).length(); //the length of each input against the last
      }
      if ((*i).length() < min) { //work out the shortest name by testing the
      min = (*i).length(); //the length of each input against the last
      }
      }
      cout << "Shortest: " << min << endl;
      cout << "Longest: " << max << endl;


      system("PAUSE") ;
      return 0;
      }

      Thanks again, been stuck on this one for a while.

      Cheers

      M

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, I'm not sure if you needed to use iterators, but that certainly works, and isn't that all that matters?

        Glad to have helped!

        Comment

        Working...