Verb, Noun Checking Program Memory error

Hey I am having trouble with this code. I am pretty sure the code is right but when I run it, the following error "Unhandled Excpetion at 0x76A24B32 in'consoleapplication4.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0065FB14." I need some help from you seniors. Please help me!

Here my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "std_lib_facilities.h"

string word() {
    string temp;
    if (cin >> temp)
        return temp;
    else
        return "";
}

bool noun() {
    string next = word();
    if (next == "the") next = word();
    return (next == "birds" || next == "fish" || next == "C++");
}

bool verb() {
    string next = word();
    return (next == "rules" || next == "fly" || next == "swim");
}

bool conjunction(string next) {
    return (next == "and" || next == "or" || next == "but");
}


string sentence() {
    while (true) {
        if (! noun())
            error ("Noun expected\n");
        if (! verb())
            error ("Verb expected\n");
        string next = word();
        if (! conjunction(next)) return next;
    }
}

int main () {
    string next;
    next = sentence();
    if (next != ".")
        error ("Sentence not terminated with a period\n");
    else if (word() != "")
        error ("Sentence followed by unexpected text\n");
    cout << "That appears to be a sentence!\n";

    // keep_window_open();
    return 0;
}

Thanks in advance!
Last edited on
What is the content of "std_lib_facilities.h"?
Sounds rather like Stroustrup's file!

Old version, with support for pre-C++11 compilers
http://www.stroustrup.com/Programming/std_lib_facilities.h

Newer version (from 2d Ed of book), without support for pre-C++11 compilers
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

It's used with examples and exercises from his book "Programming: Principles and Practice using C++" And as Stroustrup says in the comment at the top of the file:

Students: please don't try to understand the details of headers just yet. All will be explained. This header is primarily used so that you don't have to understand every concept all at once.


Andy
Last edited on
Your code is calling the function error(), which throws an exception...

From std_lib_facilities.h

1
2
3
4
5
// error() simply disguises throws:
inline void error(const string& s)
{
	throw runtime_error(s);
}


BUT your code it not handling (catching, etc) the error, so the unexpected exception handler is called which displays the message "Unhandled Excpetion at ..." and then terminates the program. (It's not an actual memory error. The memory location is where in your program the error occured, or rather, the exception was thown.)

If I add a try-catch block to your main(), i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main () {
    try {
        string next;
        next = sentence();
        if (next != ".")
            error ("Sentence not terminated with a period\n");
        else if (word() != "")
            error ("Sentence followed by unexpected text\n");
        cout << "That appears to be a sentence!\n";
    }
    catch(const exception& e) { // std::exception is std::runtime_error's base class
        cout << "error! " << e.what() << "\n";
    }

    // keep_window_open();
    return 0;
}


I now get a better error message (one of your own!)

But it doesn't quite work for me? (The code looks like it wants "[the]<noun><verb>[<conj>[the]<noun><verb>[...]]." ??)

Attempt #1

C++ rules.
error! Verb expected


Attempt #2

the fish swim.
error! Verb expected


Attempt #3

C++ rules
the fish swim
error! Sentence not terminated with a period


Attempt #4

C++ rules and the fish swim.
error! Verb expected


Hmmm... not sure your logic is quite right? (But haven't looked into it.)

Andy
Last edited on
PS

Just occured to me, as you're using cin for input, which breaks strings on whitespace...

If I put a space between the last word and the full stop/period then your program's happy! :-)

1
2
the fish rules .
That appears to be a sentence!


So it looks like you might need to modify your word() function if you want to allow people to follow normal punctuation conventions? (You need a basic sentence tokenizer!)

You might also think about moving the string next = word(); calls out of the noun() and verb() functions and into sentence() so all part of speech are treated consistently? This may well make it easier to extend things in the future.

Similarly, the test for the final '.' looks like it should probably be handled by sentence() rather than main().

Andy

PS On a slightly related note, a common convention is for function names to generally be verbs, so you might want to consider altering the existing functions so their names say what they do. e.g. word() -> get_next_word() (or getNextWord(), etc) and noun() -> is_noun() (or isNoun(), etc, inspired by the C standard library's isdigit, etc.) Not everyone follows this convention, but I find this helps me understand how my programs work.
Last edited on
Topic archived. No new replies allowed.