2012-10-23, 04:23 PM
Bumping this thread cause I don't want to start a new thread.
I have this code:
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?
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?

