0

For some reason every file I try to feed the code it says it does not exist. I have made several files to try and feed it however none have worked. So did I do something incorrectly or is it something else?

//Program will read in a list of movies with ratings from a file supplied 
//by the user and then ask the user what rating they would like to see. 
//Then it will give the user the option of seeing all the movies with 
//that rating or a random movie with that rating.



#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<fstream>
#include <string>
#include <vector>
#include<time.h>
#include<stdlib.h>
//Namespace declaration
using namespace std;

int returnRandom(int range);

//starting of main function
int main()
{

//Varibale data type declarations
int i, count = 0, num;
string word1, word2;

//array declarations
string m_name[100];
string m_rating[100];
string name;
string rate;

//filestream declaration
ifstream infile;
string x;
cout << "Please type the name of the file you would like to use. " << endl;
cin >> x;

//open file for reading
infile.open(x.c_str());

//error checking if file does not exist
if (!infile)
{
    cout << "can not open the file." << endl;
}

//start reading the data from the file
while (infile >> word1)
{

    m_name[count] = word1;
    infile >> word1;
    m_rating[count] = word1;
    count++;
}

infile.close();

//asking for user input
int choice;

cout << "Please enter the rating of the movie to watch: ";
getline(cin.ignore(), rate);
cout << "Type 1 to print all the movies of the input rating." << endl;
cout << "Type 2 to print a random movie of input rating: " << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;

//search movies which has the rating as input given by the user
if (choice == 1)
{
    for (i = 0; i<count; i++)
    {
        if (m_rating[i] == rate)
        {
            //Display movies
            cout << "Movie " << m_name[i] << " has " << rate << " ratings." << endl;
        }
    }
}
else
{
    if (choice == 2)
    {
        int index = returnRandom(count);
        while (m_rating[index] != rate)
            index = returnRandom(count);
        cout << "Movie " << m_name[index] << " has " << rate << " ratings." << endl;
    }
    else
        cout << "Invalid Input!";
}

return 0;
}//end of main

//function to return the random index of input rating
int returnRandom(int range)
{
srand(time(NULL));
return 0 + (rand() % (0 - range + 1));
} 
  • Could you post a screenshot of the directory setup? The ifstream will be relative to the path of the executable... – Felix Guo Apr 30 '17 at 06:19
  • 1
    Where is the file located? if you just feed it the name of the file, then the file should be located in the program directory (where .exe of your project exists). Else, you need to type the full path of the file `C:\\ .. \\ .\\ ..\\ ..\\` – Khalil Khalaf Apr 30 '17 at 06:20
  • I believe that is where I was messing up then because I was typing in just the title of the file. I'll try that out and let y'all know if that works. – Garrett Richards Apr 30 '17 at 06:23
  • 1
    `using namespace std;` [is a bad practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – tambre Apr 30 '17 at 06:26
  • I've been told that before. Honestly this will probably be the last code I ever write though. I'm going to school for Mechatronics and the only coding it really requires is ladder logic which is a lot different from c++. – Garrett Richards Apr 30 '17 at 06:29
  • Did you consider that, if you use VisualStudio, it sets the default working directoy to the project's one? – The Techel Apr 30 '17 at 08:19

0 Answers0