HELP PLEASE ME CREATE A VALID SENTENCES

Write your question here.



C++
I am required to write a program that recognises valid sentences in a limited language.

The elements of the language are defined as follows:

Article { a | the }

Adjective { large | brown | red | blue }

Noun { fox | bridge | dog | field }

Preposition { near | across }

Verb { runs }

A valid sentence has the form:

[article] [adjective] [noun] [verb] [preposition] [article] [adjective] [noun]

Therefore examples of a valid sentence are:

 A large dog runs across the red bridge

 The red fox runs near the large field

 A blue bridge runs across the brown dog

 A red field runs near a blue fox

You are to produce a C++ program which analyses sentences in this

language to see if they are valid or not.


Please help me i've been struggling with this for a while now and just cant move forward.

thank you very much
Got any code to show? Gotta make an attempt :P.
We're not going to write your program for you.

Here some hints:
- You need a loop to read in words of a sentence
- You need a function to classify each word read as a part of speech. Enums come to mind here.
- You need some logic to check that the order of the parts of speech match what is specified in the assignment.
Last edited on
Not sure if you are still stuck on this or not anymore @OP?
Try a solution yourself first.
If you are still stuck on it this is something I whipped something up. Although you should always try and not just wait for someone else.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Example program
#include <iostream>
#include <string>
#include <cstdio>  
using namespace std;
int main()
{
    char response;
    while (true) {
        short unsigned int amount[5] = { 0 };
        //Get a dictionary of the words that will be used, and thus will be checked for relation
        //Made them all hold 4 elements for simplicity reasons, down below
        const string article[4] = { "A", "THE", "", "" };
        const string adjective[4] = { "LARGE", "BROWN", "RED", "BLUE" };
        const string noun[4] = { "FOX", "BRIDGE", "DOG", "FIELD" };
        const string preposition[4] = { "NEAR", "ACROSS", "", ""};
        const string verb[4] = { "RUN", "", "", "" };
        
        string result = "";
        string check = "";
        string sentence;
        cout << "Enter a sentence ";
        getline(cin, sentence); //Take in whole sentence with spaces
        while(true) {
            for(unsigned int index = 0; index < sentence.size(); index++) {
                //Checks if it is not equal to a blank space in order to continue the same word.
                if(sentence[index] != ' ') {
                    sentence[index] = toupper(sentence[index]);
                    check += sentence[index];
                    //This is where setting all the arrays above to 4 come in handy.
                    for(unsigned int argCycle = 0; argCycle < 4; argCycle++) {
                        if (amount[0] < 2 && check == article[argCycle]) {
                            if(check == "A" && sentence[index + 1] == ' ') {
                                amount[0]++;
                                result += 'A';
                            }
                            else if(check == "THE") {
                                amount[0]++;
                                result += 'A';
                            }
                        }
                        else if(amount[1] < 2 && check == adjective[argCycle]) {
                            amount[1]++;
                            result += 'a';
                        }
                        else if (amount[2] < 2 && check == noun[argCycle]) {
                            amount[2]++;
                            result += 'n';
                        }
                        else if (amount[3] < 1 && check == verb[argCycle]) {
                            amount[3]++;
                            result += 'v';
                        }
                        else if (amount[4] < 1 && check == preposition[argCycle]) {
                            amount[4]++;
                            result += 'p';
                        }
                    }
                }
                else {
                    //Reset the word if it a blank char appears, because it means another word is next
                    check = "";
                }
            }
            break;
        }
        if (result == "AanvpAan") {
            cout << "\nA valid sentence\n";
        }
        else {
            cout << "\nNot a valid sentence\n";
        }
    }
    cin >> response;
    return 0;
}
Topic archived. No new replies allowed.