Menu

Apvector problems.

2003-02-03
2012-09-26
  • Nobody/Anonymous

    This program is supposed to read from a file, store in a vector. Then I am supposed to display them and say if the names from a file is Greater, lesser or equal to the average of the lengths of the three name sumed up. However, when I do this the names comes up as cut off and I'm sure the statement next to them is not correct. SO what the hell am I doing wrong here??? Note: XXXX is my dir. I have the lastest update and I'm running on WinXP. Anyone care to help me?

    #include <iostream.h>
    #include <fstream.h>
    #include "apstring.h"
    #include "apvector.h"
    #include <stdlib.h>

    apvector <apstring> Names(ifstream & in)
    {

        {
            apvector <apstring> namegoeshere(30);
            for (int k = 0; k < 3; k++)
            {
              getline(in, namegoeshere[k]);
              in.ignore('\n', 100);
            }
            return namegoeshere;
        }
    }

    int AverageLength(const apvector <apstring> & names)
    {
      int num = names[0].length() + names[1].length() + names[2].length();
      int average = num / 3;
      return average;    
    }

    void Print(const apvector <apstring> & names)
    {
      int number = AverageLength(names);
      for (int k = 0; k < 3; k++)
        {
            if (names[k].length() == number)    
                    cout << names[k] <<"    Equal to average"<<endl;
            if (names[k].length() > number)
                    cout << names[k] <<"     Greater than average"<<endl;
            else
                    cout<< names[k] <<"      Less than average"<<endl;
        }
    }

    int main()
    {
      ifstream in;
      in.open("C:\\Documents and Settings\\XXXXX\\Desktop\\XXXX.txt");
      if (in.fail())
        cout<<"Failed to open file."<<endl;
      else
      {
      apvector <apstring> names(Names(in));
      Print(names);
      system("PAUSE");
      }
      return 0;
    }

     
    • Nobody/Anonymous

      Well. I fixed part of the problem now. I took the in.ignore. Don't know why it was there in the first place. Now I have a new problem. It get the last names of the names I give it and I do not know why.

      #include <iostream.h>
      #include <fstream.h>
      #include "apstring.h"
      #include "apvector.h"
      #include <stdlib.h>

      int count = 0;

      apvector <apstring> Names(ifstream & in)
      {

          {
              int k = 0;
              apvector <apstring> namegoeshere(4);
              while (in>>namegoeshere[k])
              {
                getline(in, namegoeshere[k]);
                k++;
                count++;
              }
              return namegoeshere;
          }
      }

      int AverageLength(const apvector <apstring> & names)
      {
        int num = names[0].length() + names[1].length() + names[2].length();
        int average = num / 3;
        return average;    
      }

      void Print(const apvector <apstring> & names)
      {
        int number = AverageLength(names);
        for (int k = 0; k < count; k++)
          {
              if (names[k].length() == number)    
                      cout << names[k] <<"    Equal to average"<<endl;
              if (names[k].length() > number)
                      cout << names[k] <<"     Greater than average"<<endl;
              else
                      cout<< names[k] <<"      Less than average"<<endl;
          }
      }

      int main()
      {
        ifstream in;
        in.open("C:\\Documents and Settings\\XXXX\\Desktop\\XXX.txt");
        if (in.fail())
          cout<<"Failed to open file."<<endl;
        else
        {
        apvector <apstring> names(Names(in));
        Print(names);
        system("PAUSE");
        }
        return 0;
      }
      can anyone help me???

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.