Help with Void function

Hi, can anyone provide a little insight on some help with a void function? The purpose of my code is to count the vowels in a string of random letters and then display it to the end user. I just however cant seem to get the void function right. Any help or clues would be greatly appreciated.

#include <iostream>
#include <string>

using namespace std;

//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);

int main()

{
//Declare variables
int r, s, t, u, v;

//Prompt for user input
cout << "Enter a string: ";

//Call to countString function
countString(r, s, t, u, v);

//System pause
system ("pause");

//End Program
return 0;
}

void countString()
{
//Declare string variable
string vowels;

//Accept user input
cin >> vowels;

//Declare variables
int vowel = 0;
int acount, ecount, icount, ocount, ucount;
int length;

//Initialize variable length
length = vowels.length();

//Keep count of letters in string
for(int y = 0; y <= length; y++)
{
//Decision if letter is a or A
if (y == 'a')
acount += 1;
else (y == 'A');
acount += 1;
//Decision if letter is e or E
if (y == 'e')
ecount += 1;
else (y == 'E');
ecount += 1;
//Decision if letter is i or I
if ( y == 'i')
icount += 1;
else (y == 'I');
icount += 1;
//Decision if letter is o or O
if (y == 'o')
ocount += 1;
else (y == 'O');
ocount += 1;
//Decision if letter is u or U
if (y == 'u')
ucount += 1;
else (y == 'U');
ucount += 1;
y++;

//Display results to the user
cout << "This is quite an interesting and challenging assignment from the book" << endl;
cout << "The number of a's: " <<acount << endl;
cout << "the number of e's: " <<ecount << endl;
cout << "The number of i's: " <<icount << endl;
cout << "The number of o's: " <<ocount << endl;
cout << "The number of u's:" <<ucount << endl;
}
}
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
#include <iostream>
#include <string>

using namespace std;

//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);

int main()

{
//Declare variables
int r, s, t, u, v;

//Prompt for user input
cout << "Enter a string: ";

//Call to countString function
countString(r, s, t, u, v);

//System pause
system ("pause");

//End Program
return 0;
}

void countString()
{
//Declare string variable
string vowels;

//Accept user input
cin >> vowels;

//Declare variables
int vowel = 0;
int acount, ecount, icount, ocount, ucount;
int length;

//Initialize variable length
length = vowels.length();

//Keep count of letters in string
for(int y = 0; y <= length; y++)
{
//Decision if letter is a or A
if (y == 'a')
acount += 1;
else (y == 'A');
acount += 1;
//Decision if letter is e or E
if (y == 'e')
ecount += 1;
else (y == 'E');
ecount += 1;
//Decision if letter is i or I
if ( y == 'i')
icount += 1;
else (y == 'I');
icount += 1;
//Decision if letter is o or O
if (y == 'o')
ocount += 1;
else (y == 'O');
ocount += 1;
//Decision if letter is u or U
if (y == 'u')
ucount += 1;
else (y == 'U');
ucount += 1;
y++;

//Display results to the user
cout << "This is quite an interesting and challenging assignment from the book" << endl;
cout << "The number of a's: " <<acount << endl;
cout << "the number of e's: " <<ecount << endl;
cout << "The number of i's: " <<icount << endl;
cout << "The number of o's: " <<ocount << endl;
cout << "The number of u's:" <<ucount << endl;
}
}
ico
Check your if, else-if statements. else (y == 'A');
Should be:
1
2
3
4
if(y == 'a')
  ucount += 1; //aka ucount++;
else if(y == 'A')
  ucount += 1;
Hi Mathhead200, I checked and tested that but it didn't help. I also get this when I run the program equal to how may letters are in the string. It is what i want to print out, but obviously something is missing.

This is quite an interesting and challenging assignment from the book
The number of a's: -858993460
the number of e's: -858993460
The number of i's: -858993460
The number of o's: -858993460
The number of u's:-858993460
Can you re-post your updated code. The stuff above has several problems with it and shouldn't compile, so I'm sure you changed it a bit. And, please post it between the code tags (see Format: on the right of your edit box.)

Also, that looks like your reading garbage data!
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
***Ok I did change it up a bit in trying to troubleshoot whats wrong with it. I know the loop is wrong, but I'm confused as to how to only make it go through once.
#include <iostream>
#include <string>

using namespace std;

//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);

int main()

{
	//Declare variables
	int r, s, t, u, v;

	//Prompt for user input
	cout << "Enter a string: "; 

	//Call to countString function
	countString(r, s, t, u, v);

	
	//System pause
	system ("pause");

	//End Program
	return 0;
}

void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount)
{
	//Declare string variable
	string vowels;
	
	//Accept user input
	cin >> vowels;

	//Declare variable
	int length;
	
	//Initialize variable length
	length = vowels.length();
	
	//Keep count of letters in string
	for(int y = 0; y <= length; y++)
	{ 
	//Decision if letter is a or A
	if ((y == 'a') || (y == 'A'))
		acount += 1;
	
	//Decision if letter is e or E
	else if ((y == 'e') || (y == 'E'))
		ecount += 1;
		
	//Decision if letter is i or I					
	else if (( y == 'i') || (y == 'I'))
		icount += 1;
		
	//Decision if letter is o or O								
	else if ((y == 'o') || (y == 'O'))
		ocount += 1;
		
	//Decision if letter is u or U							
	if ((y == 'u') || (y == 'U'))
		ucount += 1;
		
		//Display results to the user
		cout << "This is quite an interesting and challenging assignment from the book" << endl; 
		cout << "The number of a's: " <<acount << endl;
		cout << "the number of e's: " <<ecount << endl;
		cout << "The number of i's: " <<icount << endl;
		cout << "The number of o's: " <<ocount << endl;
		cout << "The number of u's: " <<ucount << endl;
		
	}
} 
take all the cout statements outside of the for-loop. that way it will only output the answer once. and it will be the final answer.


also, you must initialize int r, s, t, u, v;
all to 0, that way you don't get garbage values

in addition,

your if statements should be more like:


1
2
3
if ((vowels[y] == 'a') || (vowels[y] == 'A'))
		acount += 1;
	


that way you are looking at a specific letter in the string 'vowels'

the way you have it, is comparing the number y to a character, which can never be true
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
Hi Zap, thanks for your suggestions. It now does look for the actual letter now. However, if I move the cout statements, I get an immediate error of "Subscript out of range" after the input is accepted.

#include <iostream>
#include <string>

using namespace std;

//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);

int main()

{
	//Declare variables
	int r, s, t, u, v, y;

	string vowels;
	int acount, ecount, icount, ocount, ucount;

	//Prompt for user input
	cout << "Enter a string: "; 

	//Call to countString function
	countString(r, s, t, u, v);

	//Display results to the user 
	    cout << vowels.at(y) <<endl;
		cout << "The number of a's: " <<acount << endl;
		cout << "the number of e's: " <<ecount << endl;
		cout << "The number of i's: " <<icount << endl;
		cout << "The number of o's: " <<ocount << endl;
		cout << "The number of u's: " <<ucount << endl;

	
	//System pause
	system ("pause");

	//End Program
	return 0;
}

void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount)
{
	//Declare string variable
	string vowels;
	acount = 0;
	ecount = 0;
	icount = 0;
	ocount = 0;
	ucount = 0;
	
	//Accept user input
	cin >> vowels;

	//Declare variable
	int length;
	
	
	//Initialize variable length
	length = vowels.length();
	
	//Keep count of letters in string
	for(int y = 0; y <= length; y++)
	{ 
	//Decision if letter is a or A
	if ((vowels[y] == 'a') || (vowels[y] == 'A'))
		acount += 1;
	
	//Decision if letter is e or E
	else if ((vowels[y] == 'e') || (vowels[y] == 'E'))
		ecount += 1;
		
	//Decision if letter is i or I					
	else if ((vowels[y] == 'i') || (vowels[y] == 'I'))
		icount += 1;
		
	//Decision if letter is o or O								
	else if ((vowels[y] == 'o') || (vowels[y] == 'O'))
		ocount += 1;
		
	//Decision if letter is u or U							
	if ((vowels[y] == 'u') || (vowels[y] == 'U'))
		ucount += 1;	
	}
}
yes, so your problem is, you are trying to cout variable in main. where they are undefined. I'm sorry i guess i was a little unclear, what i meant was take the cout statement outside of the for loop, but keep it in the function. like this:

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

using namespace std;

//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);

int main()

{
	//Declare variables
	int r, s, t, u, v, y;

	string vowels;
	int acount, ecount, icount, ocount, ucount;

	//Prompt for user input
	cout << "Enter a string: "; 

	//Call to countString function
	countString(r, s, t, u, v);
	
	//System pause
	system ("pause");

	//End Program
	return 0;
}

void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount)
{
	//Declare string variable
	string vowels;
	acount = 0;
	ecount = 0;
	icount = 0;
	ocount = 0;
	ucount = 0;
	
	//Accept user input
	cin >> vowels;

	//Declare variable
	int length;
	
	
	//Initialize variable length
	length = vowels.length();
	
	//Keep count of letters in string
	for(int y = 0; y <= length; y++)
	{ 
	//Decision if letter is a or A
	if ((vowels[y] == 'a') || (vowels[y] == 'A'))
		acount += 1;
	
	//Decision if letter is e or E
	else if ((vowels[y] == 'e') || (vowels[y] == 'E'))
		ecount += 1;
		
	//Decision if letter is i or I					
	else if ((vowels[y] == 'i') || (vowels[y] == 'I'))
		icount += 1;
		
	//Decision if letter is o or O								
	else if ((vowels[y] == 'o') || (vowels[y] == 'O'))
		ocount += 1;
		
	//Decision if letter is u or U							
	if ((vowels[y] == 'u') || (vowels[y] == 'U'))
		ucount += 1;	
	}

//Display results to the user:
		cout << "The number of a's: " <<acount << endl;
		cout << "the number of e's: " <<ecount << endl;
		cout << "The number of i's: " <<icount << endl;
		cout << "The number of o's: " <<ocount << endl;
		cout << "The number of u's: " <<ucount << endl;

}
Last edited on
Ah ok, I should have understood that. I didn't think it all the way through. I moved it. How about the subscript out of range error? That seems to have suddenly popped up. Thanks again!
I think that just had to do with the cout statements being in the wrong place.

just so you know if you are doing a lot of cout statements one after the other you can also do it like this:
1
2
3
4
5
6
7
8
9
cout << "The number of a's: " <<acount << endl
     << "the number of e's: " <<ecount << endl
     << "The number of i's: " <<icount << endl
     << "The number of o's: " <<ocount << endl
     << "The number of u's: " <<ucount << endl;

/*
notice there is only one ';' at the last one this is because formatting it this way is essentially writing, the whole cout statement one one line, but breaking it up for easier reading
*/
Hey, yeah that looks a lot cleaner. Thanks for that. I still have the subscript error though. Any ideas? I didnt really change anything except for formatting. But now this error pops up.
not sure, i dont get that error when i copy paste into Devcpp.
do you by any chance still have :

 
cout << vowels.at(y) <<endl;


in your code anywhere, because i deleted that, as it gave me an error. after that everything worked fine.
Yeah I need to leave that one in so the program displays to user what the string entered was. I need to I guess Google the subscript error a little more. Thanks again!
you should be able to show what the user put in with just vowels


 
cout<<vowels<<endl;
Normally I would, but my professor wants me to use the str.at command. I don't prefer to, I swear.
don't worry about it. it doesn't make any difference to me :p
I was just offering another approach.
Topic archived. No new replies allowed.