User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,837 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,583 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 207 | Replies: 4 | Solved
Reply
Join Date: Jun 2008
Posts: 69
Reputation: Q8iEnG is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster in Training

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

  #1  
Jul 2nd, 2008
Hi guys

I'm working on a project ( a game )

this is the code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//####### Begin Class Node ###########
class Node
{
public:
	string data;
	Node* link;

	Node(string x)
	{
		data = x;
		link = NULL;
	}
};

//####### Begin Class LinkList ###########
class LinkList
{
private:
	Node* head,*end,*temp,*trav;
	int count;
	void PrintBack(Node* x)
	{
		if (!x)
		{
			PrintBack(x->link);
			cout<<x->data<<"  ";
		}
	}
	
public:

	LinkList()
	{
		head = end = NULL;
		temp = NULL;
		count = 0;
	}

	bool Insert(string x)
	{
		if (!(temp = new Node(x)))
			return false;

		count++;

		if (!head)
		{
			head = end = temp;
			return true;
		}

		end->link = temp;
		end = temp;
		temp = NULL;
		return true;

	}

	bool FindX(string x)
	{
    bool found;
	found = false; 

    trav = head;  
                    
    while(trav != NULL && !found)	
        if(trav->data == x)    
           found = true;
        else
           trav = trav->link;  
                                   
     
     return found;
	}

	bool Remove(string &x)
	{
		if (!head)
			return false;

		if (count == 1)
		{
			x = head->data;
			delete head;
			head = end = NULL;
			count--;
			return true;
		}

		x = head->data;
		temp = head;
		head = head->link;
		delete temp; temp = NULL;

		return true;
	}

	bool delX(string x)
	{
		string u;

		if (!head)
			return false;

		if ( head->data == x)
		{
			Remove(u);
			return true;
		}

		for ( temp = head, trav = head->link; trav ; temp = trav,trav = trav->link)
		{
			if (trav->data == x)
			{
				temp->link=trav->link;
			
				if(trav==end)
					end = temp;

				delete trav;
				trav = NULL;

				return true;
			}
		}
		return false;
	}

	void PrintLinkList()
	{
		if(!head)
			cout<<"\nLinkList is Empty ... "<<endl;
		else
		for ( trav = head ; trav ; trav = trav->link)
		{
			cout<<trav->data<<" ";	
		}
	}

	void PrintBackword()
	{
		temp = head;

		PrintBack(head);
	}


	~LinkList()
	{
		while(head != NULL)    
		{
			temp = head;        
		    head = head->link; 
		    delete temp;        
		}

		end = head = temp = NULL;	
		count = 0;
	
	}

	void printNode(string &x)
	{
		trav = head;
		x = trav->data;
		trav=trav->link;
	}

	int counter()
	{
		return count;
	}

};

//####### Begin Function Main ###########
int main()
{
	string A[5000];
	string x;
	string p;
	int choose;
	LinkList lib; // Creating Object from the class LinkList

	cout << "Choose Which file you want to try the game on?\n";
	cout << "1 - madlib1.txt\n";
	cout << "2 - madlip2.txt\n";
	cin >> choose;


	// Switch loop to choose more than one file to play *FOR THE BONUS* xD
	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
	while( fin>>x )
	{
		A[i] = x;
		i++;
	}

	// For Loop to insert the Keywords to the LinkList.
	for( int j = 0; j < i; j++ ) 
	{
		if (A[j] == "adjective")
			lib.Insert("Adjective");
		
		if (A[j] == "verb")
			lib.Insert("Verb");

		if (A[j] == "name")
			lib.Insert("Noun");

		if (A[j] == "adverb")
			lib.Insert("Adverb");
	}

	int y = lib.counter();

	while (y != 0)
	{
		cout<<"Enter an ";
		lib.printNode(x);
		cout<<x;
		cout<<" ";
		lib.Remove(x);
		cin>>p;
		lib.Insert(p);
		cout<<endl;
	
		y--; // Decrement y until we finish from all the Keywords, to reach 0.

	}

	// For loop to replace each Keyword with the suitable word from the user.
	for(int r = 0; r < i; r++) 
	{
		if (A[r] == "adjective")
		{
			lib.printNode(x);
			A[r] = x;
			lib.Remove(x);
		}
		
		if (A[r] == "verb")
		{
			lib.printNode(x);
			A[r] = x;
			lib.Remove(x);
		}

		if (A[r] == "name")
		{
			lib.printNode(x);
			A[r] = x;
			lib.Remove(x);
		}

		if (A[r] == "adverb")
		{
			lib.printNode(x);
			A[r] = x;
			lib.Remove(x);
		}
	}
	
	// For loop to output the new updated paragraph
	for(int k = 0; k < i ; k++)
	{
		if(A[k] =="!")
			cout<<"\n\n\n";
		
		cout<<A[k]<<" ";
	}
	

	return 0; // To indicate successfully termination

}
//####### End Function Main ###########

See the MAIN

I did tried Switch Loop but it doesn't work

Any good Plans? :/ it'll be appreciated
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 69
Reputation: Q8iEnG is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster in Training

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

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

What should I do, any suggestions?

thanks
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 347
Reputation: williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about williamhemsworth has a spectacular aura about 
Rep Power: 3
Solved Threads: 36
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Whiz

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

  #3  
Jul 2nd, 2008
The variable fin is destructed before you call 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("....");
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 234
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
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?

  #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:
ifstream fin;
switch(choose) {
case 1:
    fin.open("whatever.txt");
    // ...
}

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 4: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  
Join Date: Jun 2008
Posts: 69
Reputation: Q8iEnG is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster in Training

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC