![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2010
Posts: 8
Rep Power: 0
![]() |
Hello everyone,
i'm trying to get this program, it searches for the final 'a' in an word inputted by the user, to terminate normally with a predefined error message ("This word is too long") if a word longer than 20 chars is entered. I've used a try throw catch statement and I seem to be able to get the program to terminate but I cant get the error message to appear. Can someone help me out with where i'm going wrong please? code and output below /*
#include <iostream>
#include <cstdlib>
using namespace std;
int main( )
{
const int WORD_LENGTH = 20 ;
const int INVALID_INDEX = -1 ;
const char NULL_CHAR = '\0' ;
const char SEARCH_CHAR = 'a' ;
char word[ WORD_LENGTH ] ;
char* word_ptr = word ;
int indexOf_char = INVALID_INDEX ;
try
{
cout << "Input a word: " ;
cin >> word ;
if (word[ WORD_LENGTH] > 20 )
{
throw "Incorrect Input - Word Too Long";
}
}
catch ( const int exception[] )
{
cout << exception
<< "Array Out Of Bounds"
<< endl ;
exit( 0 ) ;
}
for ( int i = 0 ; *word_ptr != NULL_CHAR ; word_ptr++, i++ )
{
if ( *word_ptr == SEARCH_CHAR )
{
indexOf_char = i ;
}
}
if ( indexOf_char == INVALID_INDEX )
{
cout << "There is no '"
<< SEARCH_CHAR
<< "' in "
<< word
<< endl ;
}
else
{
cout << "Last '"
<< SEARCH_CHAR
<< "' in "
<< word
<< " has index "
<< indexOf_char
<< endl ;
}
}Output At Runtime: Input a word: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa terminate called after throwing an instance of 'char const*' Aborted Thanks for any help! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 17
![]() ![]() |
Re: C++ Exception Handling
You've only allowed 20 characters for the word. What do you think happens if you exceed that length? Bad things, some more visibly damaging than others.
Since this is C++, why don't you use the string class and avoid the problem sensibly? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2010
Posts: 8
Rep Power: 0
![]() |
Re: C++ Exception Handling
Hi Dawei,
The brief i have is to alter the original program (which is the code above minus the try throw catch) so that it terminates with the error message "Array Out Of Bounds" if a word longer than 20 characters is ended. As you can tell I'm pretty new to C++ and although I've covered some of classes its not within the scope of this assignment. A throw function with this termination statement seemed the best (in my limited knowledge) way of doing this and I have managed to make it terminate but I cant get it to display the error message |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 17
![]() ![]() |
Re: C++ Exception Handling
The problem is that you're invoking undefined behavior BEFORE you reach your throw. In this case you're clobbering other things on the stack.
In a typical implementation that would begin with SEARCH_CHAR and continue on, depending upon the length entered by the user. If you continue beyond your own variables and clobber the stack frame used by "main" your system is going to run off into the weeds and puke on its shoes. If you MUST use a C-style string, then use an input function that allows you to restrict the input length, and declare enough chars to hold input of that length. See, for instance, "getline." |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2010
Posts: 8
Rep Power: 0
![]() |
Re: C++ Exception Handling
thanks Dawei, i'll look into getline
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: here
Posts: 440
Rep Power: 8
![]() |
Re: C++ Exception Handling
You should be catching what you throw. Consider the following:
cpp Syntax (Toggle Plain Text)
terminate called after throwing an instance of 'char const*' Aborted cpp Syntax (Toggle Plain Text)
Got: Some error message
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] My Blog |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2010
Posts: 8
Rep Power: 0
![]() |
Re: C++ Exception Handling
Thanks for the help on the above query guys, I've got that working nicely now but I also have a related problem on a similar subject.
I've another program which takes 5 in-putted numbers and displays them in reverse order. In its original state it has a cassert statement to terminate the program if anything but a digit is in-putted. My job is to get it to repeatedly ask for a digit until one is entered rather than just terminating. I have tried using another try throw catch with a for loop inside the catch statement but I cant seem to get it to work. I'd appreciate any comments/abuse on what I've got so far!
#include <iostream>
using namespace std;
int main( )
{
const int NUMB_DIGITS = 5 ;
int digits[ NUMB_DIGITS ];
cout << "Enter "
<< NUMB_DIGITS
<< " digits, each on a new line:"
<< endl ;
for ( int i = 0; i < NUMB_DIGITS ; i++ )
{
try
{
cin >> digits[ i ] ;
if( ( digits[ i ] >= 0 ) && ( digits[ i ] <= 9 ) ) ;
{
throw "Thats Not A Digit";
}
}
catch ( const char exception[] )
{
for ( int i = 0; i < NUMB_DIGITS ; i++ );
{
cin >> digits[ i ];
}
}
}
cout << "Digits in reverse order:"
<< endl ;
for ( int j = NUMB_DIGITS - 1 ; j >= 0 ; j-- )
{
cout << digits[ j ]
<< endl ;
}
} |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 17
![]() ![]() |
Re: C++ Exception Handling
You're laboring under a misperception. If you ask the extraction operator (>>) for an integer and you give it something else it will not store that something else in some location for you to magically test for type. It will, rather, break the stream. The stream will then not function again until you have taken it down to the local repair shop and had it fixed.
Newbies don't know this st*ff, of course. They would actually have to read and understand the documentation for the functions they use. See the methods, "good()," "bad()," "fail()," et. al. |
|
|
|
|
|
#9 |
|
Achieved Level 70
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,091
Rep Power: 10
![]() ![]() |
Re: C++ Exception Handling
>>They would actually have to read and understand the documentation for the functions they use.
And that's one of the problems with today's modern programming -- no printed documentation. When you and I started program we had no choice but to buy programming books and read the printed documentation that came with our compilers -- Microsoft provided a shelf full of small to large programming technical books with their compilers, which is probably one reason their compilers were always to expensive. We did not have an internet on which we could post our problems -- we had to do all the research and tedious analysis ourselves. We quickly learned how to read documentation and knew where to find answers to questions. Today's new programmers normally don't know diddly shit about those things. Instant gratification is all they want. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2010
Posts: 8
Rep Power: 0
![]() |
Re: C++ Exception Handling
/me hides
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Painting simple ellips in C++ Builder. Exception raising. | hazamin | C++ | 5 | Mar 13th, 2009 1:15 PM |
| Query regarding extending Exception classes | arj | Java | 1 | Apr 25th, 2008 4:31 PM |
| exception handling | l2u | C++ | 2 | Mar 3rd, 2007 5:47 PM |
| coldfire assembly - rts causes Illegal Instruction Exception | mika | Assembly | 4 | Jun 18th, 2006 3:35 PM |
| AhhH!!! I can't find anything on this exception... | stakeknife | ASP | 2 | Sep 26th, 2005 7:48 AM |