出于某种原因,我试图提供代码的每个文件都说它不存在。我已经制作了几个文件来尝试并提供它,但没有一个工作。那么我做错了什么或是别的什么?
//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));
}