Click to See Complete Forum and Search --> : help w/ my prog : i just cant get the hang of these pointers :(


elninio
August 6th, 2007, 04:41 AM
#include <iostream>
#include <string>

using namespace std;

struct movies{
string title;
int year;
} movie[max];

void listmovies(int& max){
int i=0;
while (i<max){
cout<<movie[i]->title<<" ("<<movie[i]->year<<")\n";
i++;
}
}

void entermovies(){
movie = new (nothrow) int[count];
for (int i=0;movie[i]==0;i++){
cout << "Error: memory could not be allocated";
system("pause");
}
// movies * pmovie;
// pmovie = &p[m];
cout<<"Enter the name of a movie: ";
cin>>movie[count]->title;
cout<<"Enter that movie's year: ";
cin>>movie[count]->year;
cout<<"\n\n You have entered: \n"<< movie[count]->title<<" ("<<movie[count]->year<<")\n\n";
count++;
cout<<"Would you like to enter another movie (m) or view the list (l) or exit (e)? \n";
int input;
cin>>input;
switch (input){
case 'm':
entermovies();
case 'l':
listmovies();
case 'e':
return 0;
}
}

int main(){
string * p;
int max = 0, count = 0;
movies * movie[m];
system("TITLE Movie Database");
// system("CLS"); clears the screen
cout<<"Movie Database \n\n";
entermovies();
system("pause");
return 0;
}




its supposed to store movies that are ented in the program (title and year), and allocates memory accordingly, everytime the user wants to imput a new movie
here is the devcpp log:

Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\c++ programming\moviesdatabasepointers.cpp" -o "D:\c++ programming\moviesdatabasepointers.exe" -I"lib\gcc\mingw32\3.4.2\include" -I"include\c++\3.4.2\backward" -I"include\c++\3.4.2\mingw32" -I"include\c++\3.4.2" -I"include" -L"lib"
D:\c++ programming\moviesdatabasepointers.cpp:9: error: size of array `movie' has non-integral type `<unknown type>'
D:\c++ programming\moviesdatabasepointers.cpp: In function `void listmovies(int&)':
D:\c++ programming\moviesdatabasepointers.cpp:14: error: base operand of `->' has non-pointer type `movies'
D:\c++ programming\moviesdatabasepointers.cpp:14: error: base operand of `->' has non-pointer type `movies'
D:\c++ programming\moviesdatabasepointers.cpp: In function `void entermovies()':
D:\c++ programming\moviesdatabasepointers.cpp:20: error: expression in new-declarator must have integral or enumeration type
D:\c++ programming\moviesdatabasepointers.cpp:21: error: no match for 'operator==' in 'movie[i] == 0'
D:\c++ programming\moviesdatabasepointers.cpp:28: error: invalid types `movies[1][<unknown type>]' for array subscript
D:\c++ programming\moviesdatabasepointers.cpp:30: error: invalid types `movies[1][<unknown type>]' for array subscript
D:\c++ programming\moviesdatabasepointers.cpp:31: error: invalid types `movies[1][<unknown type>]' for array subscript
D:\c++ programming\moviesdatabasepointers.cpp:31: error: invalid types `movies[1][<unknown type>]' for array subscript
D:\c++ programming\moviesdatabasepointers.cpp:32: error: no post-increment operator for type
D:\c++ programming\moviesdatabasepointers.cpp:11: error: too few arguments to function `void listmovies(int&)'
D:\c++ programming\moviesdatabasepointers.cpp:40: error: at this point in file
D:\c++ programming\moviesdatabasepointers.cpp:42: error: return-statement with a value, in function returning 'void'

D:\c++ programming\moviesdatabasepointers.cpp: In function `int main()':
D:\c++ programming\moviesdatabasepointers.cpp:49: error: `m' undeclared (first use this function)
D:\c++ programming\moviesdatabasepointers.cpp:49: error: (Each undeclared identifier is reported only once for each function it appears in.)

im mainly having trouble with the 'undeclared's. I think if i knew exactly how pointers work i would be able to fix most of this mess up, yes * is value pointed by and & is address of, but im still clueless when to use which and how :(

offer me some advice please!

Mavens
August 6th, 2007, 06:25 AM
What a mesh is this?? Is this the whole code that you r compiling.

GNiewerth
August 6th, 2007, 06:36 AM
Use code tags to format your code. Itīs unreadable..

S_M_A
August 6th, 2007, 06:50 AM
Hard to read it without code tags... just a few quick things.struct movies{
string title;
int year;
} movie[max];A statical allocation need a constant number instead if a variable (max). However, it's seems like a static allocation is not what you want since in entermovies you have a dynamic allocation for movie. But, this new allocates an array of count int's that you try to assign to movie (an array of movies!)

The elements in movie[i] are not accessed using -> here you should use a dot instead.

Your switch statements in entermovies does not have any breaks, i.e. every entry will fall through and exit.

Your case 'l' calls listmovies without any parameter.
Your case 'm' calls entermovies, i.e. recursion.

elninio
August 6th, 2007, 07:00 PM
what are code tags and where do i get them?, sorry :(

sunnypalsingh
August 7th, 2007, 01:42 AM
It's code with square brackets. What is the problem you are facing in the code?

elninio
August 7th, 2007, 03:51 AM
#include <iostream>
#include <string>

using namespace std;

struct movies{
string title;
int year;
} movie[max];

void listmovies(int& max){
int i=0;
while (i<max){
cout<<movie[i]->title<<" ("<<movie[i]->year<<")\n";
i++;
}
}

void entermovies(){
movie = new (nothrow) int[count];
for (int i=0;movie[i]==0;i++){
cout << "Error: memory could not be allocated";
system("pause");
}
// movies * pmovie;
// pmovie = &p[m];
cout<<"Enter the name of a movie: ";
cin>>movie[count]->title;
cout<<"Enter that movie's year: ";
cin>>movie[count]->year;
cout<<"\n\n You have entered: \n"<< movie[count]->title<<" ("<<movie[count]->year<<")\n\n";
count++;
cout<<"Would you like to enter another movie (m) or view the list (l) or exit (e)? \n";
int input;
cin>>input;
switch (input){
case 'm':
entermovies();
case 'l':
listmovies();
case 'e':
return 0;
}
}

int main(){
string * p;
int max = 0, count = 0;
movies * movie[m];
system("TITLE Movie Database");
// system("CLS"); clears the screen
cout<<"Movie Database \n\n";
entermovies();
system("pause");
return 0;
}





in facing the errors mentioned above, i just need to get the program running , then i can handle the bugs (atleast i think)

GNiewerth
August 7th, 2007, 04:34 AM
The code is, um, a little bit... bizarre?


struct movies
{
string title;
int year;
} movie[max];

max is local in main() and out of scope.


void listmovies(int& max)
{
int i=0;
while (i<max)
{
cout<<movie[i]->title<<" ("<<movie[i]->year<<")\n";
i++;
}
}

You are treating movies entries as pointers, in fact they are objects, so the operator-> cannot be used.


void entermovies()
{
movie = new (nothrow) int[count];
for (int i=0;movie[i]==0;i++)
{
cout << "Error: memory could not be allocated";
system("pause");
}
cout<<"Enter the name of a movie: ";
cin>>movie[count]->title;
cout<<"Enter that movie's year: ";
cin>>movie[count]->year;
cout<<"\n\n You have entered: \n"<< movie[count]->title<<" ("<<movie[count]->year<<")\n\n";
count++;
cout<<"Would you like to enter another movie (m) or view the list (l) or exit (e)? \n";
int input;
cin>>input;
switch (input)
{
case 'm':
entermovies();
case 'l':
listmovies();
case 'e':
return 0;
}
}

The movie allocation using new is both incorrect and unecessary. Because your movie structure contains a non-POD member (std::string) you cannot just allocate a number of bytes, std::string needs its constructor called to be initialized correctly. But since your array movies doesnīt contain pointers but objects the allocation itself is pointless.

Then you are checking all array elements against 0, which obviously does not make sense for objects unless theyīve got an operator== overloading for integers.

Next you are reading an integer and compare it to a character. Though it is valid it is somewhat confusing, because you intend to read a character.

In your switch statement you did not use the break statement to end your case blocks, so all your cases fall through to case 'e' and return 0. In addition to that you donīt keep track of the total number of movies entered and so you can not pass a valid number of movies to listmovies(), whose parameter is missing.


int main()
{
string * p;
int max = 0, count = 0;
movies * movie[m];
system("TITLE Movie Database");
// system("CLS"); clears the screen
cout<<"Movie Database \n\n";
entermovies();
system("pause");
return 0;
}

Why do you declare a pointer to string? Then you use two local variables named max and count, I think they are intended to define the maximum array length and current number of entered movies, but due to their local usage they are unknown to everything except for main(). I do not know what the next line (movies * movie[m]) is supposed to do at all.

I think you wanted to use an array of pointers to movies structures and missed the asterisk.

I donīt want to be harsh, but you are lacking some understanding of programming concepts, it is not an issue of pointers. Maybe you should choose a simpler start (one movie maybe, with fixed data), build and run it successfully and then extend you project by little steps (a collection of movies instead of a single movie. Later, when you are more familiar with object construction/assignment you might want to use STL containers instead of raw arrays, etc.)

Good Luck.