Help With Unhandled Exception

This is a discussion on Help With Unhandled Exception within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I'm new to this forum and I have a problem that I can't figure out what is wrong. ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Help With Unhandled Exception

    Hey everyone. I'm new to this forum and I have a problem that I can't figure out what is wrong. This program is for a USACO program called Name That Number, which makes you search through a dictionary of names for a number 1-12 digits in length. I think it would work if I didn't get this error.

    Code:
    #include <vector.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    using namespace std;
    const char let2[]={'A','B','C'},let3[]={'D','E','F'},let4[]={'G','H','I'},
    		   let5[]={'J','K','L'},let6[]={'M','N','O'},let7[]={'P','R','S'},
    		   let8[]={'T','U','V'},let9[]={'W','X','Y'};
    //ifstream cin("namenum.in");
    ifstream dic("c:\\namenumdic.txt");
    //ofstream cout("namenum.out");
    void letter(vector <char>, char[3], int);
    void change(char[3], const char[3]);
    bool test(string, char[3], int);
    int main()
    {
    	string word;
    	char num1[12];
    	vector <string> ans;
    	vector <char> num;
    	int x;
    	char letter1[3],letter2[3],letter3[3],letter4[3],letter5[3],letter6[3],
    		 letter7[3],letter8[3],letter9[3],letter10[3],letter11[3],letter12[3];
    	
    	cin >> num1;
    	//cout << sizeof(num1);
    	for(x=0;x<13;x++)
    	{
    		if(num1[x]!='\0')
    			num.push_back(num1[x]);
    		else
    			break;
    	}
    	x=0;
    	letter(num, letter1, x);
    	while(!dic.eof())
    	{
    		dic >> word;
    		if(test(word,letter1,x) && word.length()==num.size())
    			ans.push_back(word);
    	}
    	if(num.size()>1)
    	{
    		x++;
    		letter(num, letter2, x);
    		for(int c=0;c<ans.size();c++)
    			if(!test(ans[c],letter2,x))
    				ans[c].erase();
    	}
    	if(num.size()>2)
    	{
    		x++;
    		letter(num, letter3, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter3,x))
    					ans[c].erase;
    	}
    	if(num.size()>3)
    	{
    		x++;
    		letter(num, letter4, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter4,x))
    					ans[c].erase;
    	}
    	if(num.size()>4)
    	{
    		x++;
    		letter(num, letter5, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter5,x))
    					ans[c].erase;
    	}
    	if(num.size()>5)
    	{
    		x++;
    		letter(num, letter6, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter6,x))
    					ans[c].erase;
    	}
    	if(num.size()>6)
    	{
    		x++;
    		letter(num, letter7, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter7,x))
    					ans[c].erase;
    	}
    	if(num.size()>7)
    	{
    		x++;
    		letter(num, letter8, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter8,x))
    					ans[c].erase;
    	}
    	if(num.size()>8)
    	{
    		x++;
    		letter(num, letter9, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter9,x))
    					ans[c].erase;
    	}
    	if(num.size()>9)
    	{
    		x++;
    		letter(num, letter10, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter10,x))
    					ans[c].erase;
    	}
    	if(num.size()>10)
    	{
    		x++;
    		letter(num, letter11, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter11,x))
    					ans[c].erase;
    	}
    	if(num.size()>11)
    	{
    		x++;
    		letter(num, letter12, x);
    		for(int c=0;c<ans.size();c++)
    			if(ans[c]!="0")
    				if(!test(ans[c],letter12,x))
    					ans[c].erase;
    	}
    	
    	for(int c=0;c<ans.size();c++)
    		cout << ans[c] << endl;
    			
    	return 0;
    }
    void letter(vector <char> num, char let[3], int x)
    {	
    	switch(num[x])
    	{
    		case '2':change(let,let2);break;
    		case '3':change(let,let3);break;
    		case '4':change(let,let4);break;
    		case '5':change(let,let5);break;
    		case '6':change(let,let6);break;
    		case '7':change(let,let7);break;
    		case '8':change(let,let8);break;
    		case '9':change(let,let9);break;
    	} 
    }
    void change(char let[3], const char constant[3])
    {
    	for(int c=0;c<3;c++)
    		let[c]=constant[c];
    }
    bool test (string word, char let[3], int x)
    {
    	for(int c=0;c<3;c++)
    		if(word[x]==let[c])
    			return true;
    	return false;
    }
    It's a little long... but I get this error:
    Unhandled Exception: c000005
    At address: 00405a61
    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    I don't know if any of these cause your error, but here are a few things that should be fixed. You should remove the .h from all your headers, e.g. <vector> instead of <vector.h>. Your for loop (for(x=0;x<13;x++)) should run from 0 to less than 12, not 13, since there are only 12 characters in the num1 array. Finally, you shouldn't use eof() to control a loop: while(!dic.eof()), you should use the input function instead: while (dic >> word).

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    c000005 is an access violation on windows. Probably the for loop on the num1 array is the problem. The debugger is a great way to find these sorts of things, because it will break right on the line where the access violation occurs.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Perhaps the best piece of advice I can give you is to save this piece of code. Don't lose it. Because in 5+ years time you'll be able to look back on it and realise how amazingly far you've come.
    Of course by then you'll be able to write this whole thing in about 30 lines.

    I don't think I know anyone who wouldn't choose to rewrite this from scratch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2006, 05:46 PM
  2. I got an unhandled exception!!
    By LegendBreath in forum Game Programming
    Replies: 7
    Last Post: 04-19-2005, 01:55 PM
  3. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 05:54 PM
  4. Unhandled exception
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2002, 06:03 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21