help!

im doing an assignment for college and i need some help im on assignment 2 but you will need the info from 1 also
assignment 1 Write a value returning function isVowel that returns the value true if a given character is a vowel and otherwise returns false also write a program to test your function.

assignment 2 write a program that prompts the user to unput a sequence of characters and outputs the number of vowels (use the function isVowel written in programming assignment 1.)

heres my code:

#include <iostream>
using namespace std;

bool isVowel(char ch);

int main ()
{
char ch;
string test = "Enter a series of integers";\
cin >> ch;
for(int i = 0; i < test.size(); i++){ //here i get an error saying obsolete binding at i
if(isVowel(test[i])){
cout << test[i] << " ";
}
}
cout << endl;
cout<<"Enter a series of integers: "<<endl;
cin>>ch;
cin.ignore(300,'\n');
cout << "the number of vowels is " << i << " " << endl; //here i get an error saying name lookup of i changed for new ISO "for" scoping
cout << "Press any key to exit" << endl;
cin.get();
return 0;
}

bool isVowel (char ch)
{
return (ch== 'a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u');
}



thanks
Your function from assignment 1 is sound, but your implementation is off.

A few things:
- use a string to store the sequence of characters
- use getline() for user string input
- treat your string as a char array and send each char to isVowel() for checking using a for loop that is limited to the string length; if it returns 1, then add 1 to a vowelCount variable
- output the vowelCount variable per the requirements

You have a lot going on in your post that I can't really decipher. I recommend starting over (less your isVowel function) and go through the checklist I wrote above. Does any of what I said make sense?

Also, variable i here:

cout << "the number of vowels is " << i << " " << endl;

is out of scope because its life was limited to your for loop. Declare the suggested vowelCount variable at the beginning of your program and add 1 to it each time isVowel() returns true. Then you pull off what you wanted:

cout << "the number of vowels is " << vowelCount << " " << endl;

My comments below might help a bit more:
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
int main () {
char ch; //this should be a string named test as you imply below
	 //also, declare a vowel counter as mentioned

string test = "Enter a series of integers"; //don't you want sequence of characters?
cin >> ch; //use getline() with string test

for(int i = 0; i < test.size(); i++){ 
	if(isVowel(test[i])){
	cout << test[i] << " ";
		// add 1 to your vowel counter
}

cout << endl;

//What is this prompt for?
cout<<"Enter a series of integers: "<<endl; 
cin>>ch;
cin.ignore(300,'\n');

//i should be replaced with your vowel counter
cout << "the number of vowels is " << i << " " << endl; 

cout << "Press any key to exit" << endl;

cin.get();
Last edited on
okay i have edited this was before i changed alot so some of it didnt make sense however here is my new code and now it says the number of vowels is 0 whats the problem?


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

bool isVowel(char ch);

int main ()
{
int vowelCnt;
string test;
test = "Enter a series of characters";
getline (cin,test);
for(int i = 0; i < test.size(); i++){ //here i get an error saying obsolete binding at i
if(isVowel(test[i])){
cout << test[i] << " ";
vowelCnt++;
}
}
cout << endl;

cout << "the number of vowels is " << vowelCnt << " " << endl; /
cout << "Press any key to exit" << endl;
cin.get();
return 0;
}

bool isVowel (char ch) //im not sure what to do with this now
{
return (ch== 'a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u'); //or this
}
@megawolf15

The program fine for me. You may be getting a zero count for vowels, if you have caps on. I added ch = tolower(ch); in the isVowel function just above the return command, so that vowels are recognized in upper or lower case.
there was a problem with my compiler now i inputted the word trey and it said their were 149 vowels heres the3 code


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

bool isVowel(char ch);

int main ()
{
int vowelCnt;
string test;
test = "Enter a series of characters";
getline (cin,test);
for(int i = 0; i < test.size(); i++){ //here i get an error saying obsolete binding at i
if(isVowel(test[i])){
cout << test[i] << " ";
vowelCnt++;
}
}
cout << endl;

cout << "the number of vowels is " << vowelCnt << " " << endl;
cout << "Press any key to exit" << endl;
cin.get();

}

bool isVowel (char ch) //what do i do with this
{
return (ch== 'a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u'); //what do i do with this
}
This actually works, though it has some syntax errors and quirky code. For instance:
test = "Enter a series of characters";
What is this?

Along with initializing your variables, it should instead look like this:
1
2
3
4
int vowelCnt = 0;
string test = "";

cout << "Enter a series of characters ";

The rest you can settle by looking at the error list the compiler throws at you.

Are you still getting that binding error? As for your question on what to do with your isVowel function, it is defined and operates as it should. Do nothing to it.

But the good news is that I input my first name with your core code and it reported correctly, so you essentially got it.


okay now it works fine except for instance i enter trey and i get e the number of vowels is 1 how can i keep it from saying e? and also i have 2 other assignments i need help with could you help?
Lol. I posted a solution. But then thought, "It would be bad if someone did your work for you. Especially if its for a college course. I wouldn't want you to go through college cheating and then getting a job someone else, who actually knows what they are doing, should have gotten." - Then I thought that sounds harsh. So instead of just leaving, I wanted to give some advice.

So yeah. I deleted that post, sorry.

Here's some help though:

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
#include <iostream>
#include <string>

using namespace std;

bool isVowel ( char ch );

int main ( )
{
    int vowelCnt = 0;
    // Don't forget to set any integers to 0 if you are going to use them.
    // C++ doesn't automatically know that You want 'vowelCnt' to start with 0.

    string test;
    test = "Enter a series of characters";

    // getline ( cin, test );
    // getline does not put 'test' into 'cin' or whatever you thought it did.
    // getline puts whatever is in 'cin' into 'test' including any newlines.

    // if you want to display the text in 'test' then do this:
    cout << test << endl;
    // then do put your line here:
    getline ( cin, test );
    // now the user can enter stuff.

    for ( int i = 0; i < test.size ( ); i++ )
    {
        if ( isVowel ( test[i] ) )
        {
            //cout << test[i] << " ";
            // if you don't want it to return the vowels, then get rid of the cout.

            vowelCnt++;
        }
    }

    //cout << endl;

    cout << "the number of vowels is " << vowelCnt << " " << endl;
    cout << "Press any key to exit" << endl;

    cin.get ( );
    return 0;
}

bool isVowel ( char ch )
{
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' );
}
Last edited on
Alright. Read my post above. It should answer your questions.

Sorry cantide, I forgot to refresh before posting.
Last edited on
@vlykarye i made those changes and it works but it still says e or whatever the vowel is and then tells me how many vowels their are wouldnt the problem be with the last return statement? heres the code



#include <iostream>
#include <string>

using namespace std;

bool isVowel ( char ch );

int main ( )
{
int vowelCnt = 0;

string test;
test = "Enter a series of characters";
cout << test << endl;

getline ( cin, test );


for ( int i = 0; i < test.size ( ); i++ )
{
if ( isVowel ( test[i] ) )
{
cout << test[i] << " ";
vowelCnt++;
}
}

cout << endl;

cout << "the number of vowels is " << vowelCnt << " " << endl;
cout << "Press any key to exit" << endl;
cin.get ( );
return 0;
}

bool isVowel ( char ch )
{
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' );
}
Vlykarye, how is your post not the solution? You pretty much just did all the work for the OP! :O

OP, the vowels are printed in your if block. I think it is a nice touch, but you can comment it out there.
Last edited on
lol i dont want the work done for me i just want some help if you dont care here is the third assignment i need some help on it because i dont really know where to start
assignment 3 the function printGrade in example 5-10 is written as a void function to compute and output the course grade the course score is passed as a parameter to the function printGrade rewrite the function printGrade as a returning function so that it computes and returns the course grade. (the course grade must be output in the function main.) also change the name of the function to calculateGrade.

here is the 5-10 example
http://books.google.com/books?id=4Fn_P7tdOZgC&pg=PA279&lpg=PA279&dq=introduction+to+c%2B%2B+example+5-10+ds+malik&source=bl&ots=gTtNYGmwtL&sig=Ntfld3AQqE-AMlf_yY8KPTw0nCU&hl=en&sa=X&ei=QXGUT8fPFYPN6QH_4K25BA&ved=0CCIQ6AEwAA#v=onepage&q&f=false
1
2
3
4
void function(parameter list) {
   process code
   print solution
}


Above states that function() returns nothing and specific to your case would process and output the argument on its own. It would be called in your program by its name. Rewriting it as:

1
2
3
4
int function(parameter list) {
    process code
    return solution
}


It could be used like the following:
 
cout << function()


So make it return something just like your isVowel function above.

And btw, I was just being cranky about Vlykarye's post. You were well on your way and his post provided some good stuff.
would i rewrite printgrade as a char value returning function?
That is correct. Make sure to also modify it so it won't output on its own, allowing something else that takes that functions return as an argument to print it out.
i have to go somewhere but i will reply to this post later tonight so if you could check back i would appreciate it
#include <iostream>
using namespace std;

void getScore (int& score);
calculateGrade (char ch);

is this how i would rewrite printGrade? as calculateGrade?
bump
Lol. Ok. Maybe I sorta did the work, but I think of it more as teaching~~

void getScore (int& score); <----- the return type is void here.
??? calculateGrade (char ch); <----- this needs a return type.
I don't know where you are storing the grade. I don't know if you want to print the actual numerical grade or the letter grade. Let's assume you want a letter grade.

The "char ch" in your 'calculateGrade' function is a parameter. I think you want this function to return a letter grade, correct? So, you need to make the return type a character. If you are storing the numerical grade in some variable as an integer, then the 'calculateGrade' function needs access to it. Look at the 'getScore' function. "int& score" is how it gets access to the numerical grade. You can do this for 'calculateGrade' function, too.

Your finished 'calculateGrade' function should take a grade, convert it to a letter (char), and return it. Like cantide5ga said, by simply calling this function, nothing should be output to the screen. Once you get it to work like this, you can finish the program.
Topic archived. No new replies allowed.