6 Replies - 336 Views - Last Post: 19 November 2012 - 01:58 PM Rate Topic: -----

#1 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

What is wrong with my code? C++?

Posted 19 November 2012 - 08:59 AM

After reading in u.data, printing out the ratings information to ratings.txt, and reading in u.item information, your program should now go into a loop, prompt user for a keyword (a string with no spaces). Your program will search for all the movies that contain the keyword in the title (case-sensitive search - it is simpler) and print out a table with rows in the following format (there should be no extra spaces or tabs on each line besides those specified):

SEARCH OUTPUT:<SPACE><movie id>|<movie title>|<number of ratings>|average rating><NEWLINE>

This is a subset of the kind of data that IMDB or Netflix prints out when you search for a movie on their web sites.

Here is the snippet of code for this specific part:


//STEP 3
void read_uitem() {   //Reading file u.item
  ifstream read_stream;
  string input_line;
  read_stream.open("u.item");

  if(read_stream.is_open() == false) {
    cout << "Error!";
    exit(-1);
  } 
  string movieIDString;
  int movieID = 0;
  string title;
  string line;

  while(read_stream) {  //Running loop to find the MovieID
    getline(read_stream, line);
    int start = 0;
    int end = (int)line.find("|");
    movieIDString = line.substr(start, end);

    start = end;
    start++;
    end = (int)line.find("|", start);
    title = line.substr(start, end - start); 
    movieID = atoi(movieIDString.c_str());
    movietitles[movieID] = title;
  }
}

void getInput() { 
  while(1) { //IS IT JUST A 1
    cout << ">>>"; //Prompt for SEARCH OUTPUT: 
    int line;
    cin >> line;
    cout << "SEARCH OUTPUT: " << line << endl;  
  }
}

void read_tabular_file() { //Is this correct?
  ifstream read_stream;
  string inputline;
  cout << "Reading data" << endl;
  read_stream.open("ratings.txt");
  if (read_stream.is_open() == false) {
    cout <<"Error in opening file!" << endl;
    exit(-1);
  }
  while(true) {
    int movieID;
    string title;
    int rating;
    int average_rating;
    bool readstatus = (read_stream >> " " >> movieID >> "|" >> title >> "|" >> rating >> "|" >> average_rating >> "\n"); 
    if (readstatus == false) break;
  }
  read_stream.close();
}




Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with my code? C++?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 09:14 AM

What's with the title? Is your code C++ or python?
Was This Post Helpful? 0
  • +
  • -

#3 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 09:21 AM

C++
Was This Post Helpful? 0
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 09:22 AM

cout in Python -> False
cout in C -> False
cout in C++ -> True
Was This Post Helpful? 0
  • +
  • -

#5 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 09:23 AM

How does that information help?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 09:24 AM

Okay I'll move this out of Python then. For optimum subject experts eyeballs on your topic - be more careful about where you post threads.

Also what is wrong with your code? All I see is a copy/paste of the homework and a blurb of code. No error text, no error line, or explanation of how things are not working contrasted at what "working" means. You should add that. You know the whole "help us to better help you" sort of thing. ;)
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: What is wrong with my code? C++?

Posted 19 November 2012 - 01:58 PM

Hi,

line 16 while(read_stream) {

this is wrong..

line 32 while(1) {

this is wrong... its an infinite loop.

line 49 while(true) {

this is wrong its the same as saying while(1)

std::ifstream has member functions inherited from ios

one of those is good
and the other is eof

they might have a use in this code....

Snoopy.

PS Read up on std::ifstream please...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1