String & File input

So i'm new to C++ programming, and have a project in where the user inputs a sentence, and based on 5 grammar rules (Ex. <sentence> ::= <noun><verb><noun>), determines whether the sentence is legal or not. Also the words derive from different text files, (Ex. nouns.txt). I have looked at the strings & input . output tutorials here, but i am stuck and can't figure out what to start. I know i also have to include loops to test that the sentence follows all 5 rules. I was just wondering if anyone could help me start of.
Is this the same project you raised here?
http://www.cplusplus.com/forum/general/211359/
It is, but i figured this would be easier to work with since I'm just looking for help on how to start it.
which part of mbozzi and my suggestions did you find difficult to start the project with?
Uhh it's just more of a concept based thing. I just don't know how to assign variables to the string, or even how to make my program check a sentence using those rules. If i could see how the code works using one of the rules, i would be able to figure out how to apply it to the rest of the rules. Just don't get how to write the rules as code is all.
sorry for being hard on you but if you start one thread and then completely disregard whatever's been put on that thread or give no feedback upon those suggestions and come back later with another thread describing the same problem it just seems like a waste of everyone's time. perhaps someone will give you a few leads
haha no it's totally fine man. I just figured I would simplify it down to what i actually need help with, and then just ask questions as i got along. didn't mean to waste your time! i appreciated your response.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main ()
{
bool isNoun {
fstream inFile("nouns.txt"); /* Opens up noun txt file */

if (inFile.fail()) return false;

bool nounFound = false

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

nounFound = true
}

}
return nounFound;
};
{
bool isVerb {
fstream inputFile ("verbs.txt"); /* Opens up verbs txt file */

if (inFile.fail()) return false;

bool verbFound = false

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

verbFound = true
}
}
return verbFound;
};
int findGrammar {};
};
{
bool isAdjective {
fstream inputFile ("adjectives.txt"); /* Opens up adjectives txt file */

if (inFile.fail()) return false;

bool adjectiveFound = false

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

adjectiveFound = true
}
}
return adjectiveFound;
};
int findGrammar {};
};
{
bool isPronoun {
fstream inputFile ("pronouns.txt"); /* Opens up pronouns txt file */

if (inFile.fail()) return false;

bool pronounFound = false

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

pronounFound = true
}
}
return pronounFound;
};
int findGrammar {};
};

so far i have that ^^ to check my files for the words inputted, but whenever i go to run it, it just says "unexpected type name 'fstream': expected expression" , is this happening because i haven't written the code for applying the grammar rules yet?
you're trying to define functions within main() which is not allowed. http://stackoverflow.com/questions/4324763/c-can-we-have-functions-inside-functions
uhh, this is what i have so far but it's not working. Not sure where i am going wrong.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main(){
string isnoun;
string isverb;
string ispronoun;
string isadjective;
}

{ cout << "Please write a sentence!" << endl;
return 0;
}
bool isNoun(string word) {
ifstream inFile ("nouns.txt"); /* Opens up noun txt file */

if (inFile.fail()) return false;

bool nounFound = false;

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

nounFound = true;
}

}
return nounFound;
}

bool isVerb(string word) {
ifstream inFile ("verbs.txt"); /* Opens up noun txt file */

if (inFile.fail()) return false;

bool verbFound = false;

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

verbFound = true;
}
}
return verbFound;
}

bool isAdjective(string word) {
ifstream inFile ("adjectives.txt"); /* Opens up noun txt file */

if (inFile.fail()) return false;

bool adjectiveFound = false;

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

adjectiveFound = true;
}
}
return adjectiveFound;
}

bool isPronoun(string word) {
ifstream inFile ("pronouns.txt"); /* Opens up noun txt file */

if (inFile.fail()) return false;

bool pronounFound = false;

while (!inFile.eof()) {

string x;
inFile >> x;

if (word == x) {

pronounFound = true;
}
}
return pronounFound;
}

int findGrammar(string sentence) {
string A, B, C, D;
int wordNumber = 1;
for (int i = 0; i < sentence.size(); i++){
if (sentence [i] == '')
wordNumber++;
else {
if (wordNumber == 1)
A += sentence [i];

if (wordNumber == 2)
B += sentence [i];

if (wordNumber == 3)
C += sentence [i];

if (wordNumber == 4)
D +=sentence [i];
}
}
}
string isnoun;
string isverb;
string ispronoun;
string isadjective;
if (isNoun(A)==true && isVerb(B)==true && isNoun(C)==true){
cout << "Your sentence is a legal sentence by rule 1." <<
endl;
}
else if (isNoun(A)==true && isVerb(B)==true && isAdjective(C)==true && noun(D)==true){
cout << "Your sentence is a legal sentence by rule 2." <<
endl;
}
else if (isPronoun(A)==true && isVerb(B)==true && isNoun(C)==true){
cout << "Your sentence is a legal sentence by rule 3." << endl;
}
else if (isPronoun(A)==true && isVerb(B)==true && isAdjective(C)==true && isNoun(D)==true){
cout << "Your sentence is a legal sentence by rule 4." << endl;
}
else if (isPronoun(A)==true && isVerb(B)==true && isPronoun(C)==true){
cout << "Your sentence is a legal sentence by rule 5." << endl;
}
else
cout << "Your sentence is not a legal sentence." << endl;{
return 0;
}
uhh, this is what i have so far but it's not working. Not sure where i am going wrong.

Basic syntax errors. When you're confronted with a compiler error and you want to ask for help with it, supply the error you encountered.

Typically such lists of errors are large because the compiler is confused by the first (or first few) so supplying the first few is sufficient.

Use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Your entire main function:
1
2
3
4
5
6
int main(){
    string isnoun;
    string isverb;
    string ispronoun;
    string isadjective;
}


These lines:
1
2
3
{ cout << "Please write a sentence!" << endl;
    return 0;
}
occur at file scope where it is illegal for them to occur.
Last edited on
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main(){
    {

    cout << "Please enter a sentence: " << endl;
    cout << "Word1: " << endl;
    cout << "Word2: " << endl;
    cout << "Word3: " << endl;
    cout << "Word4: " << endl;
}
    string isnoun;
    string isverb;
    string ispronoun;
    string isadjective;

    bool isNoun {
        ifstream inputnouns;
        inputnouns.open("nouns"); /* Opens up noun txt file */
        
        if (inputnouns.fail()) return false;
        
        bool nounFound = false;
        
        while (!inputnouns.eof()) {
            
            string x;
            inputnouns >> x;
            
            if (word == x) {
                
                nounFound = true;
            }
            
        }
        return nounFound;
    };

        bool isVerb {
            ifstream inFile ("verbs"); /* Opens up noun txt file */
            
            if (inFile.fail()) return false;
            
            bool verbFound = false;
            
            while (!inFile.eof()) {
                
                string x;
                inFile >> x;
                
                if (word == x) {
                    
                    verbFound = true;
                }
            }
            return verbFound;
        };

            bool isAdjective {
                ifstream inFile ("adjectives"); /* Opens up noun txt file */
                
                if (inFile.fail()) return false;
                
                bool adjectiveFound = false;
                
                while (!inFile.eof()) {
                    
                    string x;
                    inFile >> x;
                    
                    if (word == x) {
                        
                        adjectiveFound = true;
                    }
                }
                return adjectiveFound;
            };

                bool isPronoun {
                    ifstream inFile ("pronouns"); /* Opens up noun txt file */
                    
                    if (inFile.fail()) return false;
                    
                    bool pronounFound = false;
                    
                    while (!inFile.eof()) {
                        
                        string x;
                        inFile >> x;
                        
                        if (word == x) {
                            
                            pronounFound = true;
                        }
                    }
                    return pronounFound;
                };
                
    int findGrammar;
    int sentence;
        string A, B, C, D;
    {
                    int wordNumber = 1;
                    for (int i = 0; i < sentence.size(); i++){
                        if (sentence [i] == ' ')
                            wordNumber++;
                        else {
                            if (wordNumber == 1)
                                A += sentence [i];
                            
                            if (wordNumber == 2)
                                B += sentence [i];
                            
                            if (wordNumber == 3)
                                C += sentence [i];
                            
                            if (wordNumber == 4)
                                D +=sentence [i];
                                }
                    }
            
string isnoun;
string isverb;
string ispronoun;
string isadjective;
                    if (isNoun(A)==true && isVerb(B)==true && isNoun(C)==true){
                        cout << "Your sentence is a legal sentence by rule 1." <<
                        endl;
                    }
                    else if (isNoun(A)==true && isVerb(B)==true && isAdjective(C)==true && isNoun(D)==true){
                        cout << "Your sentence is a legal sentence by rule 2." <<
                        endl;
                    }
                else if (isPronoun(A)==true && isVerb(B)==true && isNoun(C)==true){
                    cout << "Your sentence is a legal sentence by rule 3." << endl;
                }
                else if (isPronoun(A)==true && isVerb(B)==true && isAdjective(C)==true && isNoun(D)==true){
                    cout << "Your sentence is a legal sentence by rule 4." << endl;
                }
                else if (isPronoun(A)==true && isVerb(B)==true && isPronoun(C)==true){
                    cout << "Your sentence is a legal sentence by rule 5." << endl;
                }
                else
                        cout << "Your sentence is not a legal sentence." << endl;{
return 0;
                        }};}
Last edited on
Topic archived. No new replies allowed.