0

Hi, I've been trying to catch up on my class and pointer code (both are weak areas for me at the moment). I tried researching as much of the code as possible and getting some in-class help, **Edit Changed program, no longer gives build error**.

This is designed to take in peoples' names and best friends and return each person's "popularity count." The class must include a string for name, pointer for the person's best friend, and an int for popularity.

The program must also take multiple "Person" variables into a vector.

Person.h:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <stdlib.h> // for srand ( ) and rand ( ) 
#include <time.h> // for time ( ) and time_t 
#include <string>
#include <vector>
using std::rand;
using std::srand;
using std::fixed;
using std::setprecision;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace std;

class Person
{
public:
	Person(int initialPopularity); // Declare Person class

    double getPopularity(); // Declare getPopularity function
    
    void setName(string myName); // Declare setName function
    
    void favoritePerson(); // Declare favoritePerson function

    void popularityTally();
    
    void getBestFriend();
private:
	string name; // declare the name of the person
	Person *bestFriend;
	int popularityCount;
	string bestFriendQuery;
};

Person.cpp (functions)

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <stdlib.h> // for srand ( ) and rand ( ) 
#include <time.h> // for time ( ) and time_t 
#include <string>
#include <vector>
using std::rand;
using std::srand;
using std::fixed;
using std::setprecision;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace std;

#include "Person.h"   // Include definition of class Account

int main()
{
    vector<Person> names;
    int count = 0;
    string tempName = "";
    int escape = 0;
    
    cout << "Enter name (-1 to stop): ";
    cin >> tempName;
    if (tempName != "-1"){
       names.push_back (1);
       names[count].setName(tempName);
       count++;
    }
    
    while (tempName != "-1"){
          cout << "Enter name (-1 to stop): ";
          cin >> tempName;
          
          while (escape == 0){
                escape = 1;
          
                for (int i; i < count; i++){
                    if (tempName == names[i].name){
                       cout << "No duplicate names please." << endl;
                       
                       cout << "Enter name (-1 to stop): ";
                       cin >> tempName;
                       
                       escape = 0;
                    }
                }
          }
          
          if (tempName != "-1"){
             names.push_back (1);
             names[count].setname(tempName);
             count++;
          }
    }
    
    for (int i; i < count; i++){
        names[i].favoritePerson();
    }
    
   	for (int i; i < count; i++){
        for (int j; i < count; j++){
	        if (names[j].name == names[i].bestFriendQuery){
               names[i].bestFriend = &names[j];
            }
        }
        names[i].popularityTally();
    }
    
    for (int i; i < count; i++){
        cout << "name: " << names[i] << endl;
        cout << "best friend: " ;
        
        names[i].getBestFriend();
        cout << "" << endl;
        
        cout << "popularity count: " << names[i].getPopularity << endl;

    }
    
    system("PAUSE");  //Prompt for enter.
    return 0;
}

I'm thinking I called a function incorrectly or used a pointer wrong.

Edited by NEMESISD: n/a

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by NEMESISD
0

Fixed some things in the Lab 4.1.cpp

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <stdlib.h> // for srand ( ) and rand ( ) 
#include <time.h> // for time ( ) and time_t 
#include <string>
#include <vector>
using std::rand;
using std::srand;
using std::fixed;
using std::setprecision;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace std;

#include "Person.h"   // Include definition of class Account

int main()
{
    vector<Person> names;
    int count = 0;
    string tempName = "";
    int escape = 0;
    
    cout << "Enter name (-1 to stop): ";
    cin >> tempName;
    if (tempName != "-1"){
       names.push_back (1);
       names[count].setName(tempName);
       count++;
    }
    
    while (tempName != "-1"){
          cout << "Enter name (-1 to stop): ";
          cin >> tempName;
          
          while (escape == 0){
                escape = 1;
          
                for (int i; i < count; i++){
                    if (tempName == names[i].getName()){
                       cout << "No duplicate names please." << endl;
                       
                       cout << "Enter name (-1 to stop): ";
                       cin >> tempName;
                       
                       escape = 0;
                    }
                }
          }
          
          if (tempName != "-1"){
             names.push_back (1);
             names[count].setName(tempName);
             count++;
          }
    }
    
    for (int i; i < count; i++){
        names[i].favoritePerson();
    }
    
   	for (int i; i < count; i++){
        for (int j; i < count; j++){
	        if (names[j].getName() == names[i].bestFriendQuery){
               names[i].bestFriend() = &names[j];
            }
        }
        names[i].popularityTally();
    }
    
    for (int i; i < count; i++){
        cout << "name: " << names[i].getName() << endl;
        cout << "best friend: " ;
        
        names[i].getBestFriend();
        cout << "" << endl;
        
        cout << "popularity count: " << names[i].getPopularity() << endl;

    }
    
    system("PAUSE");  //Prompt for enter.
    return 0;
}

Now there are two errors where I set bestFriend and compare with bestFriendQuery (lines 69 & 70) and one more with line 70

Edited by NEMESISD: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.