943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 721
  • C++ RSS
Jul 2nd, 2008
0

How to make a choise for the user to choose a file to open?

Expand Post »
Hi guys

I'm working on a project ( a game )

this is the code
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. //####### Begin Class Node ###########
  7. class Node
  8. {
  9. public:
  10. string data;
  11. Node* link;
  12.  
  13. Node(string x)
  14. {
  15. data = x;
  16. link = NULL;
  17. }
  18. };
  19.  
  20. //####### Begin Class LinkList ###########
  21. class LinkList
  22. {
  23. private:
  24. Node* head,*end,*temp,*trav;
  25. int count;
  26. void PrintBack(Node* x)
  27. {
  28. if (!x)
  29. {
  30. PrintBack(x->link);
  31. cout<<x->data<<" ";
  32. }
  33. }
  34.  
  35. public:
  36.  
  37. LinkList()
  38. {
  39. head = end = NULL;
  40. temp = NULL;
  41. count = 0;
  42. }
  43.  
  44. bool Insert(string x)
  45. {
  46. if (!(temp = new Node(x)))
  47. return false;
  48.  
  49. count++;
  50.  
  51. if (!head)
  52. {
  53. head = end = temp;
  54. return true;
  55. }
  56.  
  57. end->link = temp;
  58. end = temp;
  59. temp = NULL;
  60. return true;
  61.  
  62. }
  63.  
  64. bool FindX(string x)
  65. {
  66. bool found;
  67. found = false;
  68.  
  69. trav = head;
  70.  
  71. while(trav != NULL && !found)
  72. if(trav->data == x)
  73. found = true;
  74. else
  75. trav = trav->link;
  76.  
  77.  
  78. return found;
  79. }
  80.  
  81. bool Remove(string &x)
  82. {
  83. if (!head)
  84. return false;
  85.  
  86. if (count == 1)
  87. {
  88. x = head->data;
  89. delete head;
  90. head = end = NULL;
  91. count--;
  92. return true;
  93. }
  94.  
  95. x = head->data;
  96. temp = head;
  97. head = head->link;
  98. delete temp; temp = NULL;
  99.  
  100. return true;
  101. }
  102.  
  103. bool delX(string x)
  104. {
  105. string u;
  106.  
  107. if (!head)
  108. return false;
  109.  
  110. if ( head->data == x)
  111. {
  112. Remove(u);
  113. return true;
  114. }
  115.  
  116. for ( temp = head, trav = head->link; trav ; temp = trav,trav = trav->link)
  117. {
  118. if (trav->data == x)
  119. {
  120. temp->link=trav->link;
  121.  
  122. if(trav==end)
  123. end = temp;
  124.  
  125. delete trav;
  126. trav = NULL;
  127.  
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133.  
  134. void PrintLinkList()
  135. {
  136. if(!head)
  137. cout<<"\nLinkList is Empty ... "<<endl;
  138. else
  139. for ( trav = head ; trav ; trav = trav->link)
  140. {
  141. cout<<trav->data<<" ";
  142. }
  143. }
  144.  
  145. void PrintBackword()
  146. {
  147. temp = head;
  148.  
  149. PrintBack(head);
  150. }
  151.  
  152.  
  153. ~LinkList()
  154. {
  155. while(head != NULL)
  156. {
  157. temp = head;
  158. head = head->link;
  159. delete temp;
  160. }
  161.  
  162. end = head = temp = NULL;
  163. count = 0;
  164.  
  165. }
  166.  
  167. void printNode(string &x)
  168. {
  169. trav = head;
  170. x = trav->data;
  171. trav=trav->link;
  172. }
  173.  
  174. int counter()
  175. {
  176. return count;
  177. }
  178.  
  179. };
  180.  
  181. //####### Begin Function Main ###########
  182. int main()
  183. {
  184. string A[5000];
  185. string x;
  186. string p;
  187. int choose;
  188. LinkList lib; // Creating Object from the class LinkList
  189.  
  190. cout << "Choose Which file you want to try the game on?\n";
  191. cout << "1 - madlib1.txt\n";
  192. cout << "2 - madlip2.txt\n";
  193. cin >> choose;
  194.  
  195.  
  196. // Switch loop to choose more than one file to play *FOR THE BONUS* xD
  197. switch( choose )
  198. {
  199. case 1:
  200. // Constructor that opens the file
  201. ifstream fin("madlip1.txt");
  202.  
  203. // If statement to check if the file can be opened or not.
  204. if( !fin )
  205. {
  206. cerr << "\nError, Can't open the File.!\n";
  207. exit(1);
  208.  
  209. } // end if statement
  210.  
  211. break; // to exit switch
  212.  
  213. case 2:
  214. // Constructor that opens the file
  215. ifstream fin("madlip2.txt");
  216.  
  217. // If statement to check if the file can be opened or not.
  218. if( !fin )
  219. {
  220. cerr << "\nError, Can't open the File.!\n";
  221. exit(1);
  222.  
  223. } // end if statement
  224.  
  225. break; // to exit switch loop
  226.  
  227. } // end switch
  228.  
  229. int i =0; // Initiate this variable for the use of the array.
  230.  
  231. // While Loop to read the File, and save the content in the array
  232. while( fin>>x )
  233. {
  234. A[i] = x;
  235. i++;
  236. }
  237.  
  238. // For Loop to insert the Keywords to the LinkList.
  239. for( int j = 0; j < i; j++ )
  240. {
  241. if (A[j] == "adjective")
  242. lib.Insert("Adjective");
  243.  
  244. if (A[j] == "verb")
  245. lib.Insert("Verb");
  246.  
  247. if (A[j] == "name")
  248. lib.Insert("Noun");
  249.  
  250. if (A[j] == "adverb")
  251. lib.Insert("Adverb");
  252. }
  253.  
  254. int y = lib.counter();
  255.  
  256. while (y != 0)
  257. {
  258. cout<<"Enter an ";
  259. lib.printNode(x);
  260. cout<<x;
  261. cout<<" ";
  262. lib.Remove(x);
  263. cin>>p;
  264. lib.Insert(p);
  265. cout<<endl;
  266.  
  267. y--; // Decrement y until we finish from all the Keywords, to reach 0.
  268.  
  269. }
  270.  
  271. // For loop to replace each Keyword with the suitable word from the user.
  272. for(int r = 0; r < i; r++)
  273. {
  274. if (A[r] == "adjective")
  275. {
  276. lib.printNode(x);
  277. A[r] = x;
  278. lib.Remove(x);
  279. }
  280.  
  281. if (A[r] == "verb")
  282. {
  283. lib.printNode(x);
  284. A[r] = x;
  285. lib.Remove(x);
  286. }
  287.  
  288. if (A[r] == "name")
  289. {
  290. lib.printNode(x);
  291. A[r] = x;
  292. lib.Remove(x);
  293. }
  294.  
  295. if (A[r] == "adverb")
  296. {
  297. lib.printNode(x);
  298. A[r] = x;
  299. lib.Remove(x);
  300. }
  301. }
  302.  
  303. // For loop to output the new updated paragraph
  304. for(int k = 0; k < i ; k++)
  305. {
  306. if(A[k] =="!")
  307. cout<<"\n\n\n";
  308.  
  309. cout<<A[k]<<" ";
  310. }
  311.  
  312.  
  313. return 0; // To indicate successfully termination
  314.  
  315. }
  316. //####### End Function Main ###########

See the MAIN

I did tried Switch Loop but it doesn't work

Any good Plans? :/ it'll be appreciated
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Jul 2nd, 2008
0

Re: How to make a choise for the user to choose a file to open?

I tried also If statement, but with no success !!

What should I do, any suggestions?

thanks
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Jul 2nd, 2008
0

Re: How to make a choise for the user to choose a file to open?

The variable fin is destructed before you use it. For this to work you would have to arrange it slightly differently.

switch( choose )
	{
	case 1:
		// Constructor that opens the file
		ifstream fin("madlip1.txt");

		// If statement to check if the file can be opened or not.
		if( !fin )
		{
			cerr << "\nError, Can't open the File.!\n";
			exit(1);

		} // end if statement
	
	break; // to exit switch

	case 2:
		// Constructor that opens the file
		ifstream fin("madlip2.txt");

		// If statement to check if the file can be opened or not.
		if( !fin )
		{
			cerr << "\nError, Can't open the File.!\n";
			exit(1);

		} // end if statement

		break; // to exit switch loop

	} // end switch

	int i =0; // Initiate this variable for the use of the array.

	// While Loop to read the File, and save the content in the array


	// Too late, its already been destructed
	while( fin>>x )
	{
		A[i] = x;
		i++;
	}

for this to work, construct the variable fin before the switch statement and open the file like this:
fin.open("....");
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jul 2nd, 2008
1

Re: How to make a choise for the user to choose a file to open?

In order to use the ifstream fin variable that you declare inside the switch statement, you need to declare it outside the switch statement. For example:
C++ Syntax (Toggle Plain Text)
  1. ifstream fin;
  2. switch(choose) {
  3. case 1:
  4. fin.open("whatever.txt");
  5. // ...
  6. }
  7.  
  8. while(fin >> whatever) // ...

[edit] Wow, I was beaten by over half an hour. Sorry.

Just one other thing to add: why not let the user enter the name of the file themselves? That way you don't have to worry about the switch statement or anything . . . . [/edit]
Last edited by dwks; Jul 2nd, 2008 at 5:20 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Jul 3rd, 2008
0

Re: How to make a choise for the user to choose a file to open?

Well, thanks a lot for trying to help

I just did create a function, and I implement all things in main into it, so that when the user choose 1, from a switch loop in main, it'll call the function xD

thanks
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: While(Holding Button)
Next Thread in C++ Forum Timeline: Windows GUI book.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC