Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
For my assignment, I'm trying to make a program that stores my name as a variable and then displays "Hello! My name is <whatever value is stored in the variable". The problem is that my program won't function properly.
What I have so far:
Code: #include iostream
using namespace std;
int main () {
string my_name = "Jonathan Chiou";
cout << "Hello! My name is " << my_name;
return 0;
}
What I get:
Line 9: error: #include expects "FILENAME" or <FILENAME>
What am I doing wrong, and what does this error mean?
Posting Freak
Posts: 4,171
Threads: 58
Joined: 2010-09
Gender: Male
Sexual Orientation: Bi
Country Flag: venezuela
IGN: xxxxFenixR
Server: Bera
Level: Mix
Job: Severals
Guild: None
Guild Alliance: Nada
Farm: Wut?
C++ right? if i remember correctly C++ Doesn't have the "String" Variable type. Try using Char instead.
Know what nvm it does recognize it, its #Include <iostream> with the "<>"
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
FenixR Wrote:C++ right? if i remember correctly C++ Doesn't have the "String" Variable type. Try using Char instead.
Know what nvm it does recognize it, its #Include <iostream> with the "<>"
That worked, thanks.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:For my assignment, I'm trying to make a program that stores my name as a variable and then displays "Hello! My name is <whatever value is stored in the variable". The problem is that my program won't function properly.
What I have so far:
Code: #include iostream
using namespace std;
int main () {
string my_name = "Jonathan Chiou";
cout << "Hello! My name is " << my_name;
return 0;
}
What I get:
Line 9: error: #include expects "FILENAME" or <FILENAME>
What am I doing wrong, and what does this error mean?
A couple of pointers. Pun not intended.
1. You need to put brackets (< and >) around each library inclusion.
2. std:  tring is part of of the <string> library so you should probably include it.
3. return 0 is entirely optional in C++ but required in C.
4. Try to avoid using namespace directives (using namespace std) as in the future you could mess something up without knowing. Try using namespace declarations instead (e.g. using std::cout
Posting Freak
Posts: 4,840
Threads: 407
Joined: 2008-09
Gender: Female
Sexual Orientation: Bi
Country Flag: Nebraska
IGN: MariettaRC
Server: Windia
Level: 200
Job: Bowmistress
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Imagine Wrote:Line 9: error: #include expects "FILENAME" or <FILENAME>
What am I doing wrong, and what does this error mean?
To explain the error, it basically means "we're expecting this line to have this specific thing but it doesn't have it."
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Bumping this thread cause I don't want to start a new thread.
I have this code:
Code: #include <iostream>
#include <iomanip> // for fixed and setprecision
#include "assn.h" // for is_vowel function
using namespace std;
int main () {
int i = 0;
string word = "regal";
char ch = word.at(i);
int wordsize = word.size();
int syllables = 1;
while (i < wordsize) {
if (is_vowel(ch)) {
cout << ch << " is a vowel." << endl;
if (i > 0) {
syllables = syllables + 1;
}
}
else {
cout << ch << " is not a vowel." << endl;
}
ch = word.at(++i);
}
cout << "Syllables: " << syllables; //ignore this
return 0;
}
For references:
No, I cannot change:
#include <iostream>
#include <iomanip> // for fixed and setprecision
#include "assn.h" // for is_vowel function
using namespace std;
The grading program will check to see if I specifically have this header. It will not take anything else.
The error I get is:
std :: out of range.
what(): basic string at
I figured out that this is caused by i < wordsize, as if I have i < wordsize - 1, I do not get this error; however, it will only analyze "r e g a".
The is_vowel function is a function provided by my profs for this assignment. If needed, I can post the code for the function.
So, my question is, how do I prevent this error from happening while having my program analyze all the letters of any inputted word?
Posting Freak
Posts: 4,171
Threads: 58
Joined: 2010-09
Gender: Male
Sexual Orientation: Bi
Country Flag: venezuela
IGN: xxxxFenixR
Server: Bera
Level: Mix
Job: Severals
Guild: None
Guild Alliance: Nada
Farm: Wut?
Try using == or <= Wordsize. I recommend being more precise with == since you really don't have a range you want the program to recognize before stopping but a exact number. Although i always forget if while is the one that evaluates if the condition its true or false, so == may or may not stop prematurely.
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Your problem is that you are using i before checking it, at the bottom of the loop.
Normally this loop would look like
Code: i = 0;
while (i < wordsize)
{
ch = word.at(i);
... if vowel ... blah blah...
i++;
}
I don't know if you're supposed to know "for" loops yet, but if you do, the canonical form would be:
Code: for (int i = 0; i < wordsize; i++)
{
ch = word.at(i);
.... checking ch ...
}
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
I know for loops, it's just that I prefer to use standard while loops.
That looks much neater than my solution of branching into a break if i == wordsize -1, so I'll use that instead.
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Imagine Wrote:I know for loops, it's just that I prefer to use standard while loops.
That's a matter of personal taste, I suppose. 'for' is just as standard as 'while'.
Personally I prefer to use 'for' when the loop is a basic progression from 0 to N-1, because it is clear at a glance that that is what the loop does.
When you write "while (i < N)" it implies, to me, that there is some more complicated logic to i. That depending on the logic inside the loop, i might go back, or skip forward more than one, for example.
(Of course, the language doesn't enforce either. I've seen some pretty crazy code in 'for' loops.)
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Another note, .at() has bound checking since it throws std::out_of_range compared to [] so it might end up slower in the long run.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Used [] instead of (), it seems to working better so far.
I'm running into another problem with my code.
(Different code, but it's part of the same assignment.)
Code: #include <iostream>
#include <iomanip> // for fixed and setprecision
#include "assn.h" // for is_vowel function
using namespace std;
int main () {
string wordfile;
int wordcount;
int lettercount = 0;
//int size = wordfile.size();
int sentencecount = 0;
int i = 0;
char ch = wordfile[i];
while (cin >> wordfile) {
++wordcount;
lettercount = lettercount + wordfile.size();
if (ch == '?' || '.' || ';' || ':' || '!') {
++sentencecount;
++i;
}
else {
++i;
}
}
cout << wordcount << endl;
cout << lettercount << endl;
cout << sentencecount << endl;
return 0;
}
What I'm trying to with this one is to determine the number of words, letters, and sentences there are in a document.
Words and letters works fine if there isn't any other code, but once I implement my code for sentence count, it starts to go wrong from there.
If I take out my code for sentence counting/displaying it, and run the program with text file that says "I like food.", it will display:
10
3
Which is what I want.
But if I include my sentence counting code in my program (see the code I posted above), I get:
64
10
3
I notice that the second and third values correctly display word count and letter count, but they're being displayed in the wrong position. Also, where did that 64 come from?
How is the code I have created for sentence counting affecting the other code?
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
That's not how you do an or.
Must write
Code: if (ch == '?' || ch == '.' || ch == ';' || ch == ':' || ch == '!')
or, even clearer
Code: if ((ch == '?') || (ch == '.') || (ch == ';') || (ch == ':') || (ch == '!'))
C is quirky in that the way you wrote it compiles without warning, but it does not do what you think it does.
Also, in your loop, you advance i, but do not recompute ch.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
SaptaZapta Wrote:-snip-
After each iteration, wouldn't ch plug in the new value of i every time?
EDIT: After fixing my or statements, I get:
64
10
0
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Imagine Wrote:After each iteration, wouldn't ch plug in the new value of i every time?
No.
You didn't define ch to always be wordfile[i]. C++ doesn't even have an easy way to do that, except as a macro, which is very highly frowned upon. You could use a pointer (char*) instead of an index, if you've learned how to do that.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
SaptaZapta Wrote:No.
You didn't define ch to always be wordfile[i]. C++ doesn't even have an easy way to do that, except as a macro, which is very highly frowned upon. You could use a pointer (char*) instead of an index, if you've learned how to do that.
Haven't learned indexes yet. In order for ch to always be defined as wordfile[i], can I just define it before the if statement like:
Code: ch = wordfile[i];
if (ch == '?' || ch == '.' || ch == ';' || ch == ':'||
ch == '!' ) {
++sentencecount;
++i;
}
And is there a reason why this text is affecting wordcount and lettercount even though they aren't determined by the same variables?
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Imagine Wrote:Haven't learned indexes yet. In order for ch to always be defined as wordfile[i], can I just define it before the if statement like:
Code: ch = wordfile[i];
if (ch == '?' || ch == '.' || ch == ';' || ch == ':'||
ch == '!' ) {
++sentencecount;
++i;
}
And is there a reason why this text is affecting wordcount and lettercount even though they aren't determined by the same variables?
1. Yes. And that's not a definition, that's an assignment. You give it a value.
2. Does your input file have empty lines after the line of text?
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
SaptaZapta Wrote:1. Yes. And that's not a definition, that's an assignment. You give it a value.
2. Does your input file have empty lines after the line of text?
Yeah, an empty line always appears after the text whenever I save it.
EDIT: Went to talk to my prof, ended up finding out that my program is only reading the 1st letter of every word. Edited my program, and problem solved.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Code: while (cin >> wordfile)
That's an infinite loop.
Code: if (ch == '?' || '.' || ';' || ':' || '!') {
++sentencecount;
++i;
}
Why not use a switch statement? Or even, std::ispunct
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Professors don't want me to using switch, and haven't learned std:: ispunct.
The point of the cin function is to read the document word by word, so it isn't infinite as long as a text document is inputted
|