Hey, im trying to use pointers with a struct. I am having an issue in the function searchData. I can not get the pointer to compair with the string, it is still just compairing the actual adress or something else. Not sure if i even passed it right. Please help you can, thank you
-Scott

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

enum mediaType {CD, TAPE, MP3};
struct Song
{char title[20];
 char artist[20];
 mediaType medium;
 float cost;
};

Song *getData(ifstream&);
void searchData(Song*[200], string,  int);

int main()
{
  ifstream indata;
  indata.open("song.txt");
  string artistName;
  Song *songs[200];
  int x=0;

  cout << "Input Artist Name.\n";
  cin >> artistName;

  *songs = getData(indata);

  searchData(&songs[200], artistName, x);

  system("PAUSE");

  return 0;
}


//*************************************************************


Song *getData(ifstream &indata)
{
    Song *songs = new Song[200];                     // points variable songs to a new array -> Song[]
    Song info;
    int x=0;
    char tempMedium;

    while(indata.eof())                              // reads song info
    {
        indata >> info.artist;
        indata >> info.title
               >> tempMedium
               >> info.cost; 

        switch( tempMedium )                         // converts media type 
        {
            case 'C':
                info.medium = CD;
                break;
            case 'T':
                info.medium = TAPE;
                break;
            case 'M':
                info.medium = MP3;            
                break;
        }
        songs[x] = info;                             // writes info as a line in songs[]
        x++;
    }
    return songs;
}
//**********************************************************************


void searchData(Song *songs[200], string artistName, int x)
{

  if (songs[x]->artist == artistName)
    {
      cout << x << endl;

     // searchData(songs[200], artistName, x++);
    }    
}

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

Use strcmp to compare strings

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

enum mediaType {CD, TAPE, MP3};
struct Song
{char title[20];
char artist[20];
mediaType medium;
float cost;
};

Song *getData(ifstream&);
void searchData(Song*[200], string, int);

int main()
{
	ifstream indata;
	indata.open("song.txt");
	string artistName;
	Song *songs[200];
	int x=0;

	cout << "Input Artist Name.\n";
	cin >> artistName;

	*songs = getData(indata);

	searchData(&songs[200], artistName, x);

	system("PAUSE");

	return 0;
}


//*************************************************************


Song *getData(ifstream &indata)
{
	Song *songs = new Song[200]; // points variable songs to a new array -> Song[]
	Song info;
	int x=0;
	char tempMedium;

	while(indata.eof()) // reads song info
	{
		indata >> info.artist;
		indata >> info.title
			>> tempMedium
			>> info.cost;

		switch( tempMedium ) // converts media type
		{
		case 'C':
			info.medium = CD;
			break;
		case 'T':
			info.medium = TAPE;
			break;
		case 'M':
			info.medium = MP3;
			break;
		}
		songs[x] = info; // writes info as a line in songs[]
		x++;
	}
	return songs;
}
//**********************************************************************


void searchData(Song *songs[200], string artistName, int x)
{

	if (songs[x]->artist == artistName)
	{
		cout << x << endl;

		// searchData(songs[200], artistName, x++);
	}
}

I couldn't test it because you didn't provide the input file. It compiled fine. I'm guessing that "string" refers to C++ style strings. You are comparing a string to a character array with the == operator. That can lead to problems. Not sure if you are using anything from the string.h/cstring library. I didn't see any. If you aren't , take out line 3, though keeping it won't hurt anything. Call it cstring rather than string.h to avoid confusion. Regardless, if you're using C++-strings, you should add this:

#include <string>

at the top. You might want to try the "compare" function from the string library:
http://www.cplusplus.com/reference/string/string/compare.html

Something like this on line 79:

if (artistName.compare (songs[x]->artist))

You might need to typecast songs[x]->artist to a const char* or something. I haven't tried it. The function specification says it wants a const char*, but it's been my experience that you can often get away with a plain old character array. Try it out. See if it makes a difference.

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.