Thread: Exception error

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    Exception error

    Hi!
    Below code works fine when it’s run within MSVC. But when run directly, it gives an unhandled exception error. Why is this? Can anyone please point out the place that the exception occur?

    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    void main()
    {
    	ifstream fs;
    	fs.open("lib.txt", ios::in);
    	
    	while(true)
    	{
    		char input[20]="\0";
    		cout<<"English word(exit-'x'): ";
    		cin>>input;
    		if(!strcmp(input, "x"))
    		{ 
    			cout<<"\nBye!\n"<<endl;
    			exit(0);
    		}
    		
    		while(fs.peek()!='~')//check data end
    		{
    			char wrd[20]="\0";
    			int i=0;
    			char let='\0';
    			while(fs.peek()!=':')//check word end
    			{
    				let = fs.get();
    				wrd[i] = let;//create word
    				i++;
    			}
    			
    			bool found = false;
    			fs.get();//skip ':'
    			
    			//identical: display non-english word(s)
    			if(!strcmp(input, wrd))
    			{
    				found = true;
    				while(fs.peek()!='.')
    				{
    					cout<< char(fs.get());
    				}	
    			}
    			
    			//not identical: skip non-english word(s)
    			if(found==false)
    			{
    				while(fs.peek()!='.')
    				{
    					fs.get(); 
    				}	
    			}
    			
    			fs.get();//skip '.'
    		}
    		fs.seekg(0);
    		cout<<'\n'<<'\n';
    	}
    }
    Thanks.

    -geek@02

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    from first glance i'm gussing you need to create a bigger array for wrd - also, check to make sure 'i' is not greater than 19
    Code:
    char wrd[20]="\0";
    int i=0;
    char let='\0';
    while(fs.peek()!=':')//check word end
    {
    let = fs.get();
    wrd[i] = let;//create word
    i++;
    }
    edit: oh yea, look up ifstream->getline(). you can have it extract tokens delimited by any character (':') rather than the default '\n' character
    Last edited by misplaced; 01-11-2005 at 12:30 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM