1 Replies - 180 Views - Last Post: 19 November 2012 - 01:32 PM Rate Topic: -----

#1 minaj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 05-November 12

What Structure and Methods to use?

Posted 19 November 2012 - 01:12 PM

Hello there everyone.

I'm trying to create a program that can:
- save movie titles in an array with 5 (five) entries / movie spots
- print the current movie list
- add a movie to the list

I think it sounds pretty handy. :)

Anyway, I'm having some big trouble with the structure of the code
and I suspect I haven't written the "LinearSearch" function correctly either.
Examples that I've found online about it only show how it works with
pre-made int searches in the code and not user inputted char searching,
I've tried to simply change it from int to char and add a "cin >>" without success.

I did go to the library and picked up a book about C++ in the hopes of
getting somewhere with this but the examples aren't dealing with strings
(names and / or titles) only numbers, and the examples doesn't show where
certain code goes either, if it should be in the main() part or somewhere else.
(It confuses my tiny little head)

The following code shows what I want to do, it has an error in it at line 49
where I tried to create a way for the list to be printed.

So yes, pay attention to the structure, I want to make sure that's correct.
Should the array be placed elsewhere? Any help and solutions are very much appreciated.

#include <iostream>
using namespace std;

class Movie
{
public:
	char* title; // Movie title.

	void SetInfo(char* _title)
	{
		title = _title;
	}
};

char LinearSearch(Movie* movieArray, char* key) // don't know if I've handled the key-variable correctly.
{
	for (int i = 0; i < 5; i++) // Looks through the entire list.
	{
		if (movieArray[i].title == key)
			return i;
	}
	return -1; // No results in the search.
}

int main()
{
	Movie myList[5];
	myList[0];
	myList[1];
	myList[2];
	myList[3];
	myList[4];

	cout << "  1. Display current movie list.";
	cout << "\n  2. Add a movie to the list.";
	cout << "\n  3. Search for a movie.";
	cout << "\n4-0. Quit.";
	cout << endl;
	cout << "\nYour choice: ";
	int choice;
	cin >> choice;

	if(choice == 1)
	{
		// Code for displaying current movie list. This FOR-loop didn't work out.
		cout << endl;
		for(int a = 0; a <= 5; a++)
		{
			 cout << myList[a];
		}
	}
	else if(choice == 2)
	{
		// Code for adding a new movie. What would be the best method to get this to work?
		cout << endl;
		cout << "Name of movie to add: ";
	}
	else if(choice == 3)
	{
		// Code for searching after a movie, somethings wonky with this...
		cout << "\nName of movie: ";
		char searchMovie;
		cin >> searchMovie;

		char index = LinearSearch(myList, (char*)searchMovie);

		if (index == -1)
		{
			cout << "Couldn't find the movie...";
		}
		else
		{
			cout << "The movie you're searching for is " << myList[index].title;
		}
		cout << endl;
		return(main()); // Makes the program automatically quit.
	}
}



That's as "close" as I've gotten so far.
I hope you can help me to eventually put this monster to rest.

Thank you in advance!

-minaj

Is This A Good Question/Topic? 0
  • +

Replies To: What Structure and Methods to use?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: What Structure and Methods to use?

Posted 19 November 2012 - 01:32 PM

Is there a reason you are not using a std::string for your variable title?

If you must stick with the character string I recommend you use a static size for that string, plus you will need to use the C-string functions to compare and and assign these strings. You can't use the assignment operator= and the comparison operator== with C-strings. You will need to look up strcpy() and strcmp() for these operations.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1