Hey all I have another riveting problem from my genius professor T_T. I have to make a program using functions, reference Parameters and full string words. Now if it were just a single letter at a time this would be easy. But it wants us to let the user enter in a whole word, and I have to have the program look at each letter and test it. It give me these operators to use string.str , string.length Kinda clueless on this sense she did her usual and didnt tell us how to do this stuff before hand. Here is my code:

#include <iostream>
#include <string>

using namespace std;

void countVowels(string str, int& aCt, int& eCt, int& iCt, int& oCt, int& uCt,int& nV);
 
int main()
{
    string inputString;
	cout << "Please enter a word and we will tell you how many vowels are in it!" << endl; 
getline(cin,inputString);

countVowels(inputString);

}

void countVowels (string s, int& aCont, int& eCont; int& iCont; int& oCont; int& uCont int& nonVowl)
{
	string.str(s)
		while (string.str(s) <= s)
		{
			if (string.at(s) == 'a')
	aCont++; 
	else
		if (string.at(s) =='e')
			eCont++;
		else
			if(string.at(s) == 'i')
				iCont++;
			else
				if(string.at(s) == 'o')
				oCont++;
				else
					if (string.at(s) == 'u') 
					else nonVowl++;		
			string.str(s)++; 


		}

}

Forgive me if its 100 percent off, but as ive said im quite clueless on what I have to do.

str() is a method used with stringstreams to get the string portion. It doesn't seem like you need stringstreams or str() here.

Remember that you can access strings as an array, so string[0] is the first character of the string. string.str(s) <= s doesn't make any sense. You were on to something when you were talking about the length() method. So you know how long the string is once you've read it in.

If you are calling your method in main, you need to pass in all of the parameters including the reference ones.

hi....fo r your reference purpose here is a site whereby you can learn on some of your problem regarding string functions. what you have done doesn't really make sense to me. some function which you need to look on is str.length, str.find, str.erase and string::npos class. you will find evething on this site http://www.cplusplus.com/reference/string/string/erase

if any further queries let me know.

Make use of the string functions like find_first_of and so one.
For example you can do something like this :

string vowels = "aeiou";
string sentence = "hello baby!";
if(sentence.find_first_of(vowels) != string::npos){
 cout << "There is a vowel in " << sentence << endl;
}

here work with this

i haven't used c++ to compile so mite have some silly errors but shudnt be much good luck and enjoy the programme you just have to some additions and use it as functions for eg 1 function to find all 'a' and other to find 'e' and so on. note i have not catered for uppercase in this which u will need to do it urself and also have to add lots of code. before u understand this prog you will need to do research. then only you will get it. this was purticurlarly the most basic concept i came up with

#include <iostream.h>
#include <stdlib.h>
#include <string>
int main()
{

    int length;
    int a_count= 0;

    string str ;
    str = "The quick brown fox jumps over the lazy dog";
    length = str.length();
    string temp;
    temp = str;
     for (int pos = 0;pos!=length;pos++)
     {

      int curpos;

      curpos = temp.find('a');
      if (curpos != string::npos)
       {
       a_count ++;
       temp.erase(curpos,1);
      }

      else
      break;


     }

     cout <<a_count<<"\n\n\n\n";
      system("PAUSE");
      return 0;
}

Ok so I decided sense the other route wasn't working I wanted to go with using another. So I tried using a switch statement, although I still don't think I am using the loop correctly. Heres my new code:

#include <iostream>
#include <string>

using namespace std;

void countVowels(string str, int& aCt, int& eCt, int& iCt, int& oCt, int& uCt);
 
int main()
{
    string inputString;
	cout << "Please enter in a word" << endl;
	cout << "We will tell you if it's a vowel or not" << endl; 
	getline(cin,inputString);
	countVowels(inputString,0,0,0,0,0);
	cout << "The word has:" << endl;
	cout << "A's:" << aCt << endl; 
	cout << "E's:" << eCt << endl;
	cout << "I's:" << iCt << endl;
	cout << "O's:" << oCt << endl;
	cout << "U's:" << uCt << endl;
}

void countVowels(string a,int& aCont, ,int& eCont,int& iCont, int& oCont, int& uCont)
{
	int nonVowl=0;
	string.str(a)
		
		for(i=0;i<=string.length();i++)
		{
			switch(string.at(i))
				case 'a':
					aCont++;
					break;
				case 'e':
					eCont++;
					break;
				case 'i': 
					iCont++;
					break;
				case 'o':
					oCont++;
					break;
				case 'u':
					uCont++;
				default:
					nonVowel++;





					

		}

}

agian I'm trying to get the program to read one letter out of a whole word at a time. Anyone help me fit this into a switch statement?

string.str(a) So you're still not doing anything with that statement. You have almost everything you need, but treat your string like an array a[0] is the first character a[a.length() - 1] is the last.

Also, how are your results getting back to main? Look up what it means to pass something by reference. You have it the right way in your method, just not in main.

And what's with the mega-indenting? What are you going to do when you have 4 nested loops with IF statements?

4 characters is enough, not 4 tabs:

void countVowels(string a,int& aCont, ,int& eCont,int& iCont, int& oCont, int& uCont)
{
    int nonVowl=0;
    string.str(a)

    for(i=0;i<=string.length();i++)
    {
        switch(string.at(i))
          case 'a':
            aCont++;
            break;
          case 'e':
            eCont++;
            break;
          case 'i': 
            iCont++;
            break;

ya u will need to keep track of ur vowels so u can display em at a later time in main. what your currently doing is tat ur using ur function to change the values but as soon as u leave your function your values are reset to 0. that is why you need to get a pass by reference.its not a hard thing to do.

all your got to do is get variables in main to pass through the function.you have pass by reference indicated in your function already.

your requirements as of now would be :-
1) create int variables to keep track of vowels(you will need 5 for a,e,i,o,u)
and initialize it to zero
2) pass them in the function call instead of the 0.

your your new function call in your main would look like countVowels(inputString,a,e,i,o,u); intead of countVowels(inputString,0,0,0,0,0); where aeiou are declared int variables initialised to 0

Thanks guys it took forever, but i finally got it :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.