c++ array help

Discussion in 'OT Technology' started by MP, Sep 9, 2005.

  1. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    well I think it's something really stupid.

    first off here's the code...

    Code:
    int main()
    {
            int i = 0;
            string title;
            double rating;
            int year;
    
            Movie myMovies[100];
    
            while (i < 101)
            {
                    getline(cin, title);
                    if (i > 99)
                    {
                            cerr << "Too many movies specified.\n"
                            << "The system can only handle 100 movies.\n"
                            << "Giving up..."
                            << endl;
                            exit(0);
                    }
    
                    cin >> year;
                    cin >> rating;
                    cin.ignore();
                    if (cin.eof()) break;
    
    that's the array n'stuff that I think is messed up.

    basically my .err file will look like this...

    the input file is 101 objects, and the fact that my cerr message is printing shows me it's working, but it seems like my program is trying to access the array one more time or something. Really wierd.

    any input would be great
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Please post all of your code. On a side note, I would use a std::vector instead of a C-style array.
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    i would say the same thing, but i have a feeling it's a school assignment.
     
  4. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    Maybe I am missing something but from what you posted it seems like it is working.

    You have an input file of 101 objects and you have an if statement giving an error when more than 99 objects is read. Since your counter variable i begins at 0 you can read in 100 things before i > 99 is true.
     
  5. MP

    MP New Member

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

    Code:
    #include "stdlib.h"
    #include "movie.h"
    #include "movie_db.h"
    
    bool testList(string in_title, int in_year, double in_rating, Movie myMovies[], int i)
    {
    	for(int j = 0; j < i; j++)
    	{
    		if(in_title == myMovies[j].getTitle() && in_year == myMovies[j].getYear() )
    		{
    			myMovies[j].updateRating(in_rating);
    			return true;
    		}
    	}
    	return false;
    }
    
    int main()
    {
    	int i = 0;
    	string title;
    	double rating;
    	int year;
    	
    	Movie myMovies[100];
    	
    	while (i < 101)
    	{
    		getline(cin, title);
    		if (i > 99)
    		{
    			cerr << "Too many movies specified.\n"
    			<< "The system can only handle 100 movies.\n"
    			<< "Giving up..."
    			<< endl;
    			exit(0);	
    		}
    
    		cin >> year;
    		cin >> rating;
    		cin.ignore();
    		if (cin.eof()) break;
    	
    	if( testList(title, year, rating, myMovies, i) )
    	{
    	}
    	else
    	{
    		myMovies[i].add(title, year, rating);
    		i++;
    	}
    	}
    		for (int j = 0; j < i; j++)
    		{
    			myMovies[j].print();
    		}
    	return 0;
    }
    

    movie.cpp
    Code:
    #include "movie.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    void Movie::add(string in_title, int in_year, double 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/NumOfTimesUpdated << endl;
    } 
    
    movie.h
    Code:
    #ifndef MOVIE.H
    #define MOVIE_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Movie
    {
    
    public:
    	void print();
    	void add(string in_title, int in_year, double in_rating);
    	Movie() { NumOfTimesUpdated = 1; }
    
    	string getTitle()	{ return m_title; }
    	int getYear()		{ return m_year;  }
    	void updateRating(double in_rating)
    	{
    		m_rating = m_rating + in_rating;
    		NumOfTimesUpdated++;
    	}
    
    private:
    	double m_rating;
    	int m_year;
    	string m_title;
    	int NumOfTimesUpdated;
    };
    
    #endif
    

    my movie_db's are just blanke files right now. I know I need to move my array over to it but for right now I'm just trying to figure out this bug.
     
  6. MP

    MP New Member

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

    it does work. The fact that my err file is being outputted tells me it's working. It's that extra line that I keep getting I can't have.

    this is what I'm getting...

    Code:
    0a1,3
    > Too many movies specified.
    > The system can only handle 100 movies.
    > Giving up...
    
    this is what I need it to display...
    Code:
    > Too many movies specified.
    > The system can only handle 100 movies.
    > Giving up...
    

    I'm not sure what's going on or what it's doing exactly. To me it seems like it's dipping back into the array an extra time.

    thanks for your help
     
  7. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    can't right now
     
  8. MP

    MP New Member

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

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    The code you posted is not writing anything to a file. Perhaps the added line in your err file is an operating system specific detail depending on how you are doing this. Try stepping through the code using a debugger to see where the extra line is being added. When I did this on my powerbook I did not see any extra lines on stderr.
     
  10. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    when compiled and ran it against the file with 101 entries it writes to whatever file I want. In this case I have it go to file.myerr

    when I do a diff of that file and what it's supposed to look like that extra line gets added. I'll keep looking to see where it's messed up.

    thanks :)
     
    Last edited: Sep 12, 2005
  11. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    i got it working


    im dumb


    the end
     

Share This Page