Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. #include<fstream>
  6. #include <stdio.h>
  7. #define filmsnum 51
  8. using namespace std;
  9. string filmname();
  10. bool compare(string x1,string x2);
  11.  
  12.  
  13. int i = 0;
  14.  
  15. int main()
  16. {
  17. string fromfile,displayed;
  18. int counter=0;
  19. fromfile = filmname();
  20. displayed = fromfile;
  21.  
  22.  
  23. for(int j=0;j<displayed.size()-1;j++) // string initialization
  24. {
  25. if(fromfile[j]==' ')
  26. displayed[j] = ' ';
  27. else
  28. displayed[j]='-';}
  29.  
  30.  
  31. cout<<"guess the name of the movie"<<endl;
  32. cout<<displayed<<endl;
  33. char x;
  34. char z[100]; //our test array
  35. for(int i=0;i<100;i++)
  36. {
  37. z[i]='/';
  38. }
  39. int n = 0; //test array iterator
  40. int l =0; // choice checker
  41. string test;
  42.  
  43. while(1)
  44. { label:
  45. cout<<"enter 1 to enter the full name or 2 to enter it letter by letter"<<endl;
  46. cin>>l;
  47. cout<<displayed<<endl;
  48. if(l==2)
  49. {
  50. counter = 0;
  51. cin>>x;
  52.  
  53. cout<<endl;
  54. for(int j=0;j<100;j++) //check if the letter is entered before
  55. {
  56. if(z[j]==x)
  57. {
  58. cout<<"entered before"<<endl;
  59. goto label;
  60. }
  61.  
  62. else if((j==99))
  63. {
  64. z[n] = x;
  65. n++;
  66. }
  67. }
  68.  
  69.  
  70.  
  71. for(int j=0;j<displayed.size()-1;j++) // check if letter exist in the film's name
  72. {
  73. if(x==fromfile[j])
  74. {
  75. displayed[j]=x;
  76. cout<<displayed<<endl;
  77. counter = 1;
  78. }
  79.  
  80. if((j==(displayed.size()-2))&&(counter == 0))
  81. { i++;
  82. cout<<"you have "<<4-i<<" trials"<<endl;
  83. }
  84. }
  85.  
  86. if(i>3)
  87. {
  88. cout<<"sorry you failed"<<endl<<"the name of the film is "<<fromfile<<endl;
  89. break;
  90. }
  91.  
  92. if(!fromfile.compare(displayed))
  93. {
  94. cout<<"good job the name is "<<fromfile<<endl;
  95. break;
  96. }
  97. }
  98.  
  99. else if (l==1)
  100. {
  101. cin.ignore();
  102. getline(cin,test);
  103.  
  104.  
  105. if(compare(fromfile,test))
  106. {cout<<"good job the name is "<<fromfile<<endl; break;}
  107. else
  108. {cout<<"you failed the name is "<<fromfile<<endl; break;}
  109. }
  110. else
  111. {cout<<"wrong choice"<<endl; cin.ignore();goto label;}
  112.  
  113.  
  114. }
  115.  
  116.  
  117. return 0;
  118. }
  119.  
  120. //the function responsible for picking random name;
  121. string filmname()
  122. {
  123.  
  124. string x;
  125. srand(time(0));
  126. ifstream in("/home/hesham/films.txt");
  127. for(int j=0;j<(rand()%filmsnum);j++) //picking random film
  128. {
  129. getline(in,x);
  130. }
  131. in.close();
  132. return x;
  133. }
  134.  
  135.  
  136. bool compare(string x1,string x2)
  137. { for(int i=0;i<x1.size()-1;i++)
  138. {
  139. if(x1[i]!=x2[i])
  140. return false;
  141. }
  142. return true;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement