CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2007
    Posts
    10

    help w/ my prog : i just cant get the hang of these pointers :(

    Code:
    #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!
    Last edited by elninio; August 7th, 2007 at 04:12 AM.

  2. #2
    Join Date
    Aug 2005
    Location
    India
    Posts
    67

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    What a mesh is this?? Is this the whole code that you r compiling.

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    Use code tags to format your code. It´s unreadable..
    - Guido

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    Hard to read it without code tags... just a few quick things.
    Code:
    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.

  5. #5
    Join Date
    Jul 2007
    Posts
    10

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    what are code tags and where do i get them?, sorry

  6. #6
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    It's code with square brackets. What is the problem you are facing in the code?
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  7. #7
    Join Date
    Jul 2007
    Posts
    10

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    Code:
    #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)
    Last edited by elninio; August 7th, 2007 at 04:10 AM.

  8. #8
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: help w/ my prog : i just cant get the hang of these pointers :(

    The code is, um, a little bit... bizarre?

    Code:
    struct movies
    {
       string title;
       int year;
    } movie[max];
    max is local in main() and out of scope.

    Code:
    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.

    Code:
    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.

    Code:
    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.
    Last edited by GNiewerth; August 7th, 2007 at 04:42 AM.
    - Guido

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured