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

Thread Solved
Reply

Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster

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

 
0
  #1
Jul 2nd, 2008
Hi guys

I'm working on a project ( a game )

this is the code
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster

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

 
0
  #2
Jul 2nd, 2008
I tried also If statement, but with no success !!

What should I do, any suggestions?

thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,381
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 112
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #3
Jul 2nd, 2008
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("....");
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

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

 
1
  #4
Jul 2nd, 2008
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:
  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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster

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

 
0
  #5
Jul 3rd, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC