Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include<fstream>
- #include <stdio.h>
- #define filmsnum 51
- using namespace std;
- string filmname();
- bool compare(string x1,string x2);
- int i = 0;
- int main()
- {
- string fromfile,displayed;
- int counter=0;
- fromfile = filmname();
- displayed = fromfile;
- for(int j=0;j<displayed.size()-1;j++) // string initialization
- {
- if(fromfile[j]==' ')
- displayed[j] = ' ';
- else
- displayed[j]='-';}
- cout<<"guess the name of the movie"<<endl;
- cout<<displayed<<endl;
- char x;
- char z[100]; //our test array
- for(int i=0;i<100;i++)
- {
- z[i]='/';
- }
- int n = 0; //test array iterator
- int l =0; // choice checker
- string test;
- while(1)
- { label:
- cout<<"enter 1 to enter the full name or 2 to enter it letter by letter"<<endl;
- cin>>l;
- cout<<displayed<<endl;
- if(l==2)
- {
- counter = 0;
- cin>>x;
- cout<<endl;
- for(int j=0;j<100;j++) //check if the letter is entered before
- {
- if(z[j]==x)
- {
- cout<<"entered before"<<endl;
- goto label;
- }
- else if((j==99))
- {
- z[n] = x;
- n++;
- }
- }
- for(int j=0;j<displayed.size()-1;j++) // check if letter exist in the film's name
- {
- if(x==fromfile[j])
- {
- displayed[j]=x;
- cout<<displayed<<endl;
- counter = 1;
- }
- if((j==(displayed.size()-2))&&(counter == 0))
- { i++;
- cout<<"you have "<<4-i<<" trials"<<endl;
- }
- }
- if(i>3)
- {
- cout<<"sorry you failed"<<endl<<"the name of the film is "<<fromfile<<endl;
- break;
- }
- if(!fromfile.compare(displayed))
- {
- cout<<"good job the name is "<<fromfile<<endl;
- break;
- }
- }
- else if (l==1)
- {
- cin.ignore();
- getline(cin,test);
- if(compare(fromfile,test))
- {cout<<"good job the name is "<<fromfile<<endl; break;}
- else
- {cout<<"you failed the name is "<<fromfile<<endl; break;}
- }
- else
- {cout<<"wrong choice"<<endl; cin.ignore();goto label;}
- }
- return 0;
- }
- //the function responsible for picking random name;
- string filmname()
- {
- string x;
- srand(time(0));
- ifstream in("/home/hesham/films.txt");
- for(int j=0;j<(rand()%filmsnum);j++) //picking random film
- {
- getline(in,x);
- }
- in.close();
- return x;
- }
- bool compare(string x1,string x2)
- { for(int i=0;i<x1.size()-1;i++)
- {
- if(x1[i]!=x2[i])
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement