arrgg what does this compiling error mean?

Discussion in 'OT Technology' started by MP, Sep 18, 2004.

  1. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    arrgg what does this c++ compiling error mean?

    Code:
    /*
    
    This program demonstrates using the C++ new operator.
    
    
    */
    
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    class Movie
    {
        public:
        	Movie(char title, int year, int rating);	
    	void print();
        
        private:
          char m_title;
        	int m_year;
        	int m_rating;
    };
    
    Movie::Movie(char title, int year, int rating)
    {
        m_year = year;
        m_rating = rating;
        m_title = title;
    }
    
    void
    Movie::print()
    {
        cout << "Year: " << m_year << "," << m_title << ", rating = " << m_rating << endl;
    }
    
    
    int
    main()
    {
        Movie *data[100];	// array to hold the dynamically created Pair objects
        int num_movies = 0;	// the number of pair objects we have created
        char title;
        int year, rating;		// hold values read from cin
    
        // as long as there is an integer to read, read it (as long as not EOF)
        while (cin >> title) 
        gets(title);
        {
    	//read 2nd integer in pair (dangerous assumption, what if no more nums?)
            cin >> year; 
            
            //
            cin >> rating;
    
    	// instantiate a new Pair object using the new operator
    	data[num_movies] = new Movie(title, year, rating);
    
    	// we have read another pair of integers, update our counter
    	num_movies++;  
        }
    
        // print out all the pairs pointed to by the data array
        for (int i = 0; i < num_movies; i++)
        {
        	cout << i << endl;
        	data[i]->print();
        }
    
    }
    
    error:
    g++ new.cpp -o new
    new.cpp: In function `int main()':
    new.cpp:60: warning: invalid conversion from `char' to `char*'


    arggg...I don't see what's wrong with my code :wtc:
     
    Last edited: Sep 18, 2004
  2. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Well, there's a difference between a char and a pointer to a char (char *). When you declare a pointer for a variable type, you're essentially creating an array of that variable type. So, a pointer to a char would be like declaring a string.

    In your main() function, you declared title as a char. Then you try to do a gets() on it. That's where the error is. You have to declare title as char *.

    EDIT: By the way, there are more things that need to change in the code for it to compile, but you should be able to get those on your own.
     
  3. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    okay so I changed char *title

    and I also changed it for the instantiate. Compiles but gives me a crazy output.

    year: -23234534 , rating = -34354356

    stuff like that

    edit: do i have to change all the titles to *title?
     
  4. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
  5. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Don't use gets(), this is a c++ program so stick with cin. Also, your constructor is fucked. As the previous poster said, use pointers to arrays of chars to hold strings if you must. But why not just use the string class? It's much easier. If not, change your constructor to this:

    Code:
    Movie::Movie(const char *title, int year, int rating)
    {
        m_year = year;
        m_rating = rating;
        m_title = new char[strlen(title) + 1];
        sprintf(m_title, "%s", title);
    }
    
    Your while loop is messed up too. How long do you want to loop? Until the user enters a certain number of movies?
     
  6. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley

    cool thanks, how should my cin statement look? would I need to use cin.getline? or just something like while (cin >> title) ?????

    the loop just has to end on ctrl+d for now.

    also would I need to change any of the other char titles or just the one would listed?
     
    Last edited: Sep 18, 2004
  7. Apheleon

    Apheleon okay

    Joined:
    Apr 5, 2002
    Messages:
    1,539
    Likes Received:
    0
    Location:
    Royal Oak, MI
    cin >> title would be easiest if the user is inputing stuff from the command line. if you're reading a file cin.getline and all that jazz is best.
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    cin >> title won't handle whitespace, if that's a concern. If you need to capture spaces (which movie titles will most likely contain), then cin.getline is more appropriate.
     
  9. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    okay so i finally got it to at least take in everything. Here's my code...

    Code:
    /*
    
    */
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Movie
    {
        
    public:
        void print();
        void add(string in_title, int in_year, int in_rating); 
    
    private:
        int m_rating;
        int m_year;
        string m_title;
    
    };
    
    int main()
    {
        int i = 0;
        string title;
        int rating;
        int year;
        
        Movie myMovies[100];
        
        while (i < 100)
            {
                 getline(cin, title);
                 cin >> year;
                 cin >> rating;
                 cin.ignore();
                
                myMovies[i].add(title, year, rating);
                
                i++;
            }
                for (int j = 0; j<i; j++)
                {
                    myMovies[j].print();
                }
                
            return 0;
    }
    
    void Movie::add(string in_title, int in_year, int in_rating)
    {
    
    m_title = in_title;
    m_year = in_year;
    m_rating = in_rating;
    
    }
    void Movie::print()
    {
    cout << m_year << ": " << m_title << ", " << "rating = " << m_rating << endl;
    }
    
    the problem i'm having now is this...

    sample input:
    star wars
    1981
    6

    ctrl + d (stops program)

    output:

    1981: star wars, rating = 6
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    .
    .
    .
    .
    .
    .
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003
    9: , rating = 2003

    any idears on why

    edit: I know It has to do with the Movie myMovies[100] I'm just not sure how to get around the problem. Obviously it can't be 0. WOuld I just do a simply for statement or something?
     
    Last edited: Sep 18, 2004
  10. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    You are printing 100 entries from your movies array regardless if 100 of them were entered by the user or not. I would suggest creating a flag of some kind to tell if a particular entry is valid. If it isn't valid, do not print it.

    Alternatively, you could subtract i from 100, that would tell you how many movies the user entered. Just print the first (100-i) entries in your for loop.
     
  11. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Your loop isn't stopping when you press CTRL-D because you're not telling it to stop. After each line where you gather input, check for end-of-file by adding the line:

    if (cin.eof()) break;
     
  12. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley

    nice, works perfectly :bowdown:
     

Share This Page