Hi, i need some help, i'm stuck with a program that takes that takes 4 object, sorts them out and prints them. I'm using pointers but now i'm stuck because i cannot out see what i'm doing wrong. I have a felling that at some point i'm not passing the value(s) but just can't figure it out.

Hi, i need some help, i'm stuck with a program that takes that takes 4 object, sorts them out and prints them. I'm using pointers but now i'm stuck because i cannot out see what i'm doing wrong. I have a felling that at some point i'm not passing the value(s) but just can't figure it out why

[LIST=1]
[*]// olympic.h

[*]#ifndef _OLYMPIC_H
[*]#define _OLYMPIC_H
[*]#include "competitor.h"
[*]#include "ranker.h"

[*]#endif
[/LIST][TEX][/TEX]
[LIST=1]
[*]// olympic.cpp

[*]#include <iostream>
[*]#include <fstream>
[*]#include "olympic.h"

[*]using namespace std;

[*]ofstream csis;

[*]int main() {
[*]    const int lanes = 4;
[*]    Ranker rank(lanes);
[*]    csis.open("csis.dat");

[*]    // First make a list of names and lane assignments
[*]    Competitor* starters[lanes];
[*]    starters[0] = new Competitor("EmmyLou Harris", 1);
[*]    starters[1] = new Competitor("Nanci Griffith", 2);
[*]    starters[2] = new Competitor("Bonnie Raitt",   3);
[*]    starters[3] = new Competitor("Joni Mitchell",  4);
[*] 
[*]    // The race is run; now assign a time to each person
[*]    starters[0]->setTime((double)12.0);
[*]    starters[1]->setTime((double)12.8);
[*]    starters[2]->setTime((double)11.0);
[*]    starters[3]->setTime((double)10.3);

[*]	// Put everyone into the ranker
[*]    for (int i = 0; i < lanes; i++)
[*]	{
[*]        rank.addList(starters[i]);
[*]	}
[*]    // Now print out the list to make sure it's right
[*]    cout << "Competitors by lane are:" << endl;
[*]    csis << "Competitors by lane are:" << endl;

[*]    for (int i = 1; i <= lanes; i++)
[*]	{
[*]        rank.getLane(i)->print();
[*]	} 

[*]    // Finally, show how they finished
[*]    cout << "Rankings by finish are:"  << endl;
[*]    csis << "Rankings by finish are:"  << endl;

[*]    for(int i = 1; i <= lanes; i++)
[*]	{
[*]        rank.getFinish(i)->print();
[*]	}
[*]    for(int i = 0; i < lanes; i++)
[*]	{
[*]        delete starters[i];
[*]	}
[*]     csis.close();
[*]} // myset's destructor will be called automatically here
[/LIST]
[LIST=1]
[*]// competitor.h
[*]#ifndef _COMPETITOR_H
[*]#define _COMPETITOR_H

[*]class Competitor
[*]{
[*]	private:
[*]		char *name;
[*]		double time;
[*]		int lanecmp;
[*]	public:
[*]		Competitor(char *nm, int i);
[*]		void print();
[*]		void setTime(double flt);
[*]		int getLane();
[*]		double getTimer();
[*]		friend class Ranker;
[*]};
[*]#endif
[/LIST]
[LIST=1]
[*]// competitor.cpp
[*]#include <stdio.h>
[*]#include <string.h>
[*]#include <iostream>
[*]#include <fstream>
[*]#include "competitor.h"

[*]using namespace std;

[*]extern ofstream csis;

[*]Competitor::Competitor(char *athlete, int i)
[*]{
[*]	name = new char[strlen(athlete) + 1];
[*]    strcpy_s(name, strlen(athlete) + 1, athlete);
[*]	lanecmp = i;
[*]}
[*]void Competitor::setTime(double flt)
[*]{
[*]	time = flt;
[*]}
[*]int Competitor::getLane()
[*]{
[*]	return lanecmp;
[*]}
[*]double Competitor::getTimer()
[*]{
[*]	return time;
[*]}
[*]void Competitor::print()
[*]{
[*]	cout << &name << endl;
[*]	csis << &name << endl;
[*]}
[/LIST]
[LIST=1]
[*]// ranker.h
[*]#ifndef _RANKER_H
[*]#define _RANKER_H

[*]class Ranker
[*]{
[*]	public:
[*]		 Competitor** rankList;
[*]		 //Competitor** list_possition;
[*]		 int index;
[*]		 int lanernk;
[*]		 int lanecnt;

[*]		Ranker(int i);
[*]		void addList(Competitor *cmp);
[*]		Competitor *getLane(int l);
[*]		Competitor *getFinish(int f);
[*]		//Competitor *getCompetitor(int cp);
[*]};
[*]#endif
[/LIST]
[LIST=1]
[*]// ranker.cpp
[*]#include <stdio.h>
[*]#include <string.h>
[*]#include <iostream>
[*]#include "competitor.h"
[*]#include "ranker.h"

[*]using namespace std;

[*]//extern ofstream csis;

[*]Ranker::Ranker (int i)
[*]{
[*]	rankList = new Competitor* [i]; 
[*]}
[*]void Ranker::addList(Competitor *cmp)
[*]{
[*]	//list_possition = new Competitor *cmp;
[*]	lanernk = 4;
[*]	for (int c = 0; c < lanernk; c++)
[*]	{
[*]		if (rankList[c] == NULL)
[*]		{
[*]			rankList[c] = cmp;
[*]			break;
[*]		}
[*]		else if (rankList[c]->getTimer() > cmp->getTimer())
[*]		{
[*]			index = lanernk - 1;
[*]			while (index > c)
[*]			{
[*]				rankList[index] = rankList[index - 1];
[*]				index--;
[*]			}
[*]			rankList[c] = cmp;
[*]			break;
[*]		}
[*]	}
[*]}
[*]Competitor *Ranker::getLane(int l)
[*]{
[*]	for (lanecnt = 0; lanecnt < lanernk; lanecnt++)
[*]	{
[*]		if (rankList[lanecnt]->getLane() == (l - 1))
[*]		{
[*]			break;
[*]		}
[*]	}
[*]	
[*]	return rankList[lanecnt];
[*]}
[*]Competitor *Ranker::getFinish(int f)
[*]{
[*]	return rankList[f];
[*]}
[/LIST]

You haven't said what's actually happening.
What is it doing wrong?
Is there any error messages?

yes you are right, well when i build the solution i do not get any error or warning messages but one a go step by step, when i get to initialize the Rank object with "rankList = new Competitor* " (line 11 of the rank.cpp) i get the pop up "There is no source code available for the current location." and if i just run the debug it will stop at the line 25 of the competitor.cpp
I presume that it is because somehow i'm not initializing the rank object properly which causes that i cannot add any competitor objects to it, but at the same time i cannot understand why or find what i'm doing wrong.

You need to set the rankList pointers to NULL in Ranker's constructor.

You need to remove the ampersands in front of name in Competitor::print.

You should have destructors for Competitor and Ranker since they both dynamically allocate some storage.

And you don't need the (double) in this (and similar) lines: starters[0]->setTime((double)12.0); That's all I can see at a glance. Fix those and let me know if it works.

Thanks for the help, it works not, just like you said, i needed to set the rankList pointer to null but also i did not realize that on the addList ang getLane i had the loops from 0 to 3 instead of 1 to 4

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.