OOD and Arrays

I've got a program I'm working on the uses OOD in order to build an array of website information that a user enters. The program has to take a URL that a user enters, compare it to what is already in the array, tell the user whether it is in the array or not and then accept new input from the user (URL, byte size of page, MIME type, and date accessed.)

I'm not very experienced wit this kind of stuff and I'm having trouble connecting all the functions from the header and implementation files to the main program.

If anyone can shed some light on this and maybe give me some pointers that'd be great!

CODE:

MAIN PROGRAM:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** 
 * cachemain.cpp
 * <p>
 * To create a searchable cache of info from webpages
 * visited by a user
 *
 * author: Jordan Warnecke
 * email: jwarneck@umw.edu
 * language: C++
 * pledge: I pledge. JHW 11/12/2013
 *
 */
//Purpose: This program uses object oriented design to enter information into
//         an array about a webpage, print out and display, and sort the information
//         about the webages that have been entered into the array

#include "cache.h"	   //Implementation of CacheType functions
#include <iostream>
#include <string>

using namespace std;

const int MAX_SIZE = 100;

int main ( )
{
   //Declare variables
   CacheType cache;
   CacheType pageURL;
   CacheType pageArray[MAX_SIZE];
   string newURL;
   string newPage;
   char choice;
   int pageComp;
   
   cout << "Enter your choice from the ones below: " << endl;
   cin >> choice;

   if ((choice == "R")|| (choice == "r")){
      cout << "Enter the URL you want to search for: " << endl;
      cin >> newURL;
      bool pageComp = pageURL.compare(newURL);
      }
      if (pageComp == -1){
         cout << "This webpage has not been visited before." << endl;
         string newPage = newURL.enterPages (pageURL, byteSize, mimeType, dateAcc);
         pageArray[MAX_SIZE]= newPage;
         
      }
      
      
      else
         cout << "This URL has been visited previously." << endl;
      
   else if ((choice == "S")|| (choice == "s"))
      cache.searchPages();
         
   else if ((choice == "P")|| (choice == "p"))
      cache.displayPages();
      
   else if ((choice == "Q")|| (choice == "q"))
      return 0;
   
   else
      cout << "Entry error, try again." << endl;
   
} // end main

   
//sort function
void sortPages(CacheType pageArray[], int size){

   int temp;
   int passCount;
   CacheType searchIndex;
   cacheType miniIndex;
   
   for (passCount =0; passCount < size - 1; passCount++){
      
      miniIndex = passCount;
      
      for (searchIndex = passCount +1; searchIndex < size; searchIndex++){
         bool int compInd = pageArray[searchIndex].compare(pageArray[miniIndex]);
         
         if (compInd == -1)
            miniIndex = searchIndex;
            
   
      }
      
      temp = pageArray[miniIndex];
      pageArray[miniIndex] = pageArray[passCount];
      pageArray[passCount] = temp;
    }
}


HEADER FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//***************************************************************************
//cache.h 
//
//Jordan Warnecke
// 10/24/2013
// <ETC HEADER INFO>
// class WPageType
// This class specifies data that should be stored with a webpage including the URL,
// MIME type, byte size, and date accessed. The class also specifies operations that 
// are defined on the type including enterPages, displayPages, and sortPages.
//***************************************************************************

#include <iostream>
#include <string>


using namespace std;

class CacheType {
   public:
   
   void enterPage(string pageURL, int byteSize, string mimeType, string dateAcc); //reads in info for webpage
   void displayPages (); //display the pages that had been entered into the array
   void sortPages (); // sorts array data
   bool compare (CacheType X, CacheType Y); //searches data to compare what user enters
   
   
   private:
   
   string URL;   //URL variable is a string
   int pageSize; //byteSize is an int
   string MIME; //MIME Type variable is a string
   string date; //date the webpage is accessed is stored as string
   
};



IMPLEMENTATION FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "cache.h"

//**************************************************************************
// void CacheType::searchPages( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to enter a URL and search the array
//          to see if the URL has already been stored in the array
//**************************************************************************
void CacheType::searchPages()
{
   cout << "Enter the URL of the webpage to search for: " << endl;
   cin >> string pageURL;

   int i= 0;
   int result= -1;

   while ((result= -1) && (i < size)){
      if (pageArray[i] == pageURL)
         result= i;
      
      else
         i ++;
         }
   return result;
      
}

//**************************************************************************
// void CacheType::enterPage( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to enter information and store it in an object
//          of type cacheType
//**************************************************************************

void CacheType::enterPage(string pageURL, int byteSize, string mimeType, string dateAcc)
{

    cout << "Please enter the information associated with the page." << endl;
      cout << " " << endl;
      cout << "Page URL: "
      cin >> pageURL;
      cout << "Byte size of page: " << endl;
      cin >> byteSize;
      cout << "MIME Type of page: " << endl;
      cin >> mimeType;
      cout << "Date the page was accessed: " << endl;
      cin >> dateAcc;
      
   string URL = pageURL;
   int pageSize = byteSize;
   string MIME = mimeType;
   string date = dateAcc;
   
   
      
}//end enterPage()

//**************************************************************************
// void CacheType::displayPages( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to display all information in array
//**************************************************************************

void CacheType::displayPages()
   cout << pageURL << byteSize << mimeType << dateAcc;


}//end displayPages()

//**************************************************************************
// void CacheType::displayPages( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to compare URLs to see if it needs to be added
//**************************************************************************

bool CacheType::compare(CacheType newURL)
{
   int pageComp;
   if (pageURL == newURL.pageURL)
      pageComp = 0;
   else if (pageURL < newURL.pageURL)
      pageComp = -1;
   else
      pageComp = 1;
}
Check for missing braces and semicolons:
1
2
3
void CacheType::displayPages()  // <== Oops!
   cout << pageURL << byteSize << mimeType << dateAcc;
}

1
2
3
4
5
6
7
8
void CacheType::enterPage(string pageURL, int byteSize, string mimeType, string dateAcc)
{
      cout << "Please enter the information associated with the page." << endl;
      cout << " " << endl; 
      cout << "Page URL: " // <== Oops!
      cin >> pageURL;
      // ...
}

Declare return type for functions that return sth. Declare variables that are used in member functions - you can pass them as arguments, declare them as class fields, or declare as local variables, but in function below you do none of these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CacheType::searchPages()
{
    cout << "Enter the URL of the webpage to search for: " << endl;
    cin >> string pageURL;  // <== what is 'string pageURL' ? 
                            // Should it be just URL, a class member string?
    int i= 0;
    int result= -1;
    while ((result= -1) && (i < size)){ // <= 'result = -1' or 'result == -1' ?
        if (pageArray[i] == pageURL)    // Also, what is size? A class member?
            result= i;                  // What is pageArray[i]? Also a class member?
        else
            i ++;
    }
    return result;    
}

Should all these variables be class members? They are not in class declaration in header file:
1
2
3
4
void CacheType::displayPages()
{
   cout << pageURL << byteSize << mimeType << dateAcc;
}

All these methods must be found in class declaration (header file). If you change return type or argument list in one place - don't forget to update the other!
1
2
3
4
5
void CacheType::enterPage(string pageURL, int byteSize, string mimeType, string dateAcc);
void CacheType::displayPages();
void CacheType::searchPages();            // <== Oops! I'm not in header file!
bool CacheType::compare(CacheType newURL);// <== I am in implementation
bool compare (CacheType X, CacheType Y);  // <== and I am in header. We are different functions! 

Pay attention to letter capitalization:
1
2
3
4
5
void sortPages(CacheType pageArray[], int size){
   int temp;
   int passCount;
   CacheType searchIndex;
   cacheType miniIndex; // <== Oops! No such type. 

This fragment of main has several mistakes:
1
2
3
4
5
6
7
8
9
10
11
12
CacheType searchIndex; // searchIndex is CacheType...
for (passCount =0; passCount < size - 1; passCount++){
    miniIndex = passCount;
    // ... but in the next line you try to assign it a value of int with '='. 
    // Then you compare it with another int with '<'
    // And increment with '++'
    // Did you plan to overload these operators?
    // Or (more likely) is searchIndex supposed to be an int?
    for (searchIndex = passCount +1; searchIndex < size; searchIndex++){
        
    // Bool int? More like Bull Sh*t : http://instantrimshot.com/index.php?sound=rimshot :]
        bool int compInd = pageArray[searchIndex].compare(pageArray[miniIndex]);x]

Look out for single and double quotes, and again, don't mix data types:
1
2
3
4
5
6
7
8
9
10
11
string newURL;
char choice;
int pageComp;
cout << "Enter your choice from the ones below: " << endl;
cin >> choice; 
if ((choice == "R")|| (choice == "r")) {    // chars use ' ', " " are for strings
    cout << "Enter the URL you want to search for: " << endl;
    cin >> newURL;
    bool pageComp = pageURL.compare(newURL); // Oops! Compare wants CacheType, 
                                             // but gets std::string!
}


Pair your if-elses. They mismatch in main().

After fixing these problems, your code should compile, so we might start finding bugs related to OOD :D
Last edited on
Thanks for getting back to me!!!

I think I made all of the changes you suggested? Like I said I am very new to using these array functions and still figuring out how OOD works...all this has been very frustrating to me and I have spent many hours going through textbook, googling, and simply trial and error.

Took out the second compare function that took two params (arguments?) because I think I had just confused myself and added an extraneous function...

Here is updated code:

MAIN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** 
 * cachemain.cpp
 * <p>
 * To create a searchable cache of info from webpages
 * visited by a user
 *
 * author: Jordan Warnecke
 * email: jwarneck@umw.edu
 * language: C++
 * pledge: I pledge. JHW 11/12/2013
 *
 */
//Purpose: This program uses object oriented design to enter information into
//         an array about a webpage, print out and display, and sort the information
//         about the webages that have been entered into the array

#include "cache.h"	   //Implementation of CacheType functions
#include <iostream>
#include <string>

using namespace std;

const int MAX_SIZE = 100;

int main ( )
{
   //Declare variables
   CacheType cache;
   CacheType pageURL;
   CacheType pageArray[MAX_SIZE];
   CacheType newURL;
   CacheType newPage;
   char choice;
   int pageComp;
   int i;
   
   cout << "Enter your choice from the ones below: " << endl;
   cin >> choice;

   if ((choice == 'R')|| (choice == 'r')){
      cout << "Enter the URL you want to search for: " << endl;
      cin >> newURL;
      bool pageComp = pageURL.compare(newURL);

      if (pageComp == -1){
         cout << "This webpage has not been visited before." << endl;
         newPage = newURL.enterPages(pageURL, int byteSize, mimeType, dateAcc);
         pageArray[i]= newPage; 
      }
      
      else if (pageComp == 1)
         cout << "This URL has been visited previously." << endl;
   }
      
   else if ((choice == 'S')|| (choice == 's'))
      pageArray[i].searchPages();
         
   else if ((choice == 'P')|| (choice == 'p'))
      pageArray[i].displayPages();
      
   else if ((choice == 'Q')|| (choice == 'q'))
      return 0;
   
   else
      cout << "Entry error, try again." << endl;
   
} // end main

   
//sort function
void sortPages(CacheType pageArray[], int size){

   CacheType temp;
   CacheType passCount;
   CacheType searchIndex;
   CacheType miniIndex;
   
   for (passCount =0; passCount < size - 1; passCount++){
      
      miniIndex = passCount;
      
      for (searchIndex = passCount +1; searchIndex < size; searchIndex++){
         
         
         if (pageArray[searchIndex].compare(pageArray[miniIndex])
            miniIndex = searchIndex;
            
   
      }
      
      temp = pageArray[miniIndex];
      pageArray[miniIndex] = pageArray[passCount];
      pageArray[passCount] = temp;
    }
}


HEADER:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//***************************************************************************
//cache.h 
//
//Jordan Warnecke
// 10/24/2013
// <ETC HEADER INFO>
// class WPageType
// This class specifies data that should be stored with a webpage including the URL,
// MIME type, byte size, and date accessed. The class also specifies operations that 
// are defined on the type including enterPages, displayPages, and sortPages.
//***************************************************************************

#include <iostream>
#include <string>


using namespace std;

class CacheType {
   public:
   
   void enterPage(string pageURL, int byteSize, string mimeType, string dateAcc); //reads in info for webpage
   void displayPages (); //display the pages that had been entered into the array
   void searchPages();
   void sortPages (); // sorts array data
   bool compare (string newURL); //searches data to compare what user enters
   
   
   private:
   
   string URL;   //URL variable is a string
   int pageSize; //byteSize is an int
   string MIME; //MIME Type variable is a string
   string date; //date the webpage is accessed is stored as string
   
};


IMPLEMENTATION:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "cache.h"

//**************************************************************************
// void CacheType::searchPages( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to enter a URL and search the array
//          to see if the URL has already been stored in the array
//**************************************************************************
void CacheType::searchPages()
{
   cout << "Enter the URL of the webpage to search for: " << endl;
   cin >> URL;

   int i= 0;
   int result= -1;

   while ((result == -1) && (i < size)){
      if (pageArray[i] == pageURL)
         result= i;
      
      else
         i ++;
         }
   return result;
      
}

//**************************************************************************
// void CacheType::enterPage( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to enter information and store it in an object
//          of type cacheType
//**************************************************************************

void CacheType::enterPage(string pageURL, int byteSize, string mimeType, string dateAcc)
{

    cout << "Please enter the information associated with the page." << endl;
      cout << " " << endl;
      cout << "Page URL: " << endl;
      cin >> pageURL;
      cout << "Byte size of page: " << endl;
      cin >> byteSize;
      cout << "MIME Type of page: " << endl;
      cin >> mimeType;
      cout << "Date the page was accessed: " << endl;
      cin >> dateAcc;
      
   string URL = pageURL;
   int pageSize = byteSize;
   string MIME = mimeType;
   string date = dateAcc;
   
   
      
}//end enterPage()

//**************************************************************************
// void CacheType::displayPages( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to display all information in array
//**************************************************************************

void CacheType::displayPages(){
   cout << URL << pageSize << MIME << date;


}//end displayPages()

//**************************************************************************
// void CacheType::compare( ) 
// Precondition : none
// Postcondition: values have been assigned to all data members
// Purpose: Allows someone to compare URLs to see if it needs to be added
//**************************************************************************

bool CacheType::compare(newURL)
{
   if (pageURL == newURL.pageURL)
      return true;
   else if (pageURL < newURL.pageURL)
      return false;
}
Topic archived. No new replies allowed.