One last Function question!

My program works great, but I want to put the output in its own function as well (to keep int main() clean. When I set up the function output(), I try to send vCount to it. The problem is, the program doesn't even execute the output function now. This is probably a tiny mistake, but if someone could point it out, I'd appreciate it. :)

Code:

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
86
87
88
89
90
#include "stdafx.h"
#include <iostream>
#include <string>
using    namespace std;

string   input();                // Function: input
int      countVowels( string );  // Function: countVowels
bool     reRun();                // Function: reRun
int      output( int );          // Function: output

// ==========================
// Function: main
// ==========================
int main(){
	bool run = true;

	while(run){

    string X = input();

	int vCount = countVowels(X);

    int output(vCount);

	run = reRun();
	}

	return 0;
}

// ==========================
// Function: input
// ==========================
string input(){

	string Y;

	cout << "Please type word: ";
	cin  >> Y;
	cout << endl;

	return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z) {
	int    NewCount = 0;
	int    L = 0;

	while ( L < Z.length() )
	{
		char Let = Z [L];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if ( isVowel == true )
		{
		++NewCount;
		}
		++L;
	}
	return NewCount;
}

// ==========================
// Function: reRun
// ==========================
bool reRun() {
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;
   if ( run2 != 'y' || run2 != 'Y' )
       return false;
   else
	   return true;
}

// ==========================
// Function: output
// ==========================
int output(int B){

	cout << "There are " << B << " vowels in your word.";
    cout << endl; cout << endl;

	return 0;
}
closed account (10oTURfi)
What is line 23 supposed to do? It actually assigns value of vCount to variable int output;
Also I do not see a reason why output() is returning int.
Try removing lines 21 and 23 and replace them with:
output(countVowels(X));
Last edited on
We haven't learned about void in class yet, and we're not supposed to use it.
I understand that void is simply a function that doesn't return a value (research online), but for now, it's against class rules. Silly, I know.

I'll get back to you when I test that. thanks.
To answer your question: Line 23 was supposed to call function output() and send the value "vCount" to it. int B (in function output) grabs vCount, and outputs the text. Obviously it didn't work like I thought it would. :)
Tried your method, and it works, though I don't understand why mine doesn't.

1
2
int vCount = countVowels(X);
int output(vCount);


looks the same as your:

int output(countVowels(X));



I don't get why yours works and mine doesn't. Mine is just a longer way of writing it.
Gah, I found the problem. I was declaring the output function twice... same problem I had before.


Fixed Code working 100%:

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
86
87
88
89
90
#include "stdafx.h"
#include <iostream>
#include <string>
using    namespace std;

string   input();                // Function: input
int      countVowels( string );  // Function: countVowels
bool     reRun();                // Function: reRun
int      output( int );          // Function: output

// ==========================
// Function: main
// ==========================
int main(){
	bool run = true;

	while(run){

    string X = input();

	int vCount = countVowels(X);

    output(vCount);

	run = reRun();
	}

	return 0;
}

// ==========================
// Function: input
// ==========================
string input(){

	string Y;

	cout << "Please type word: ";
	cin  >> Y;
	cout << endl;

	return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z) {
	int    NewCount = 0;
	int    L = 0;

	while ( L < Z.length() )
	{
		char Let = Z [L];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if ( isVowel == true )
		{
		++NewCount;
		}
		++L;
	}
	return NewCount;
}

// ==========================
// Function: reRun
// ==========================
bool reRun() {
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;
   if ( run2 != 'y' || run2 != 'Y' )
       return false;
   else
	   return true;
}

// ==========================
// Function: output
// ==========================
int output(int B){

	cout << "There are " << B << " vowels in your word.";
    cout << endl; cout << endl;

	return 0;
}


Sorry about all these questions guys; I'm still learning the basics. Thanks everyone. :)
Anyway...

Wooo!! I finally finished my program! =D
Okay, I tidied up the code a bit, and I figured I'd share it with everyone who helped out so you can see how I did. Comments and critiques are welcomed and requested. =D


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
86
87
88
89
90
// ==========================
// Global Declarations
// ==========================
#include "stdafx.h"
#include <iostream>
#include <string>
using    namespace std;

string   input();                // Function: input
int      countVowels( string );  // Function: countVowels
bool     reRun();                // Function: reRun
int      output( int );          // Function: output

// ==========================
// Function: main
// ==========================
int main(){
   bool run = true;

   while(run){
   string X = input();           // Get Input
   int vCount = countVowels(X);  // Count Vowels
   output(vCount);               // Display Output
   run = reRun();                // Loop Program

   }

   return 0;
}

// ==========================
// Function: input
// ==========================
string input(){
   string Y;

   cout << "Please type word: ";
   cin  >> Y;
   cout << endl;

   return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z) {
   int    NewCount = 0;
   int    L = 0;

   while ( L < Z.length() )
   {
      char Let = Z [L];
      bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' ||
                    Let == 'o' || Let == 'u' || Let == 'A' || 
                    Let == 'E' || Let == 'I' || Let == 'O' ||
                    Let == 'U');
      if ( isVowel == true ){
         ++NewCount;
      }
      ++L;
   }
   return NewCount;
}

// ==========================
// Function: reRun
// ==========================
bool reRun() {
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;
   if ( run2 != 'y' || run2 != 'Y' )
      return false;
   else
      return true;
}

// ==========================
// Function: output
// ==========================
int output(int B){

   cout << "There are " << B << " vowels in your word.";
   cout << endl; cout << endl;

   return 0;
}


[P.S.: Mr. Derkacht, if you are checking to see if your students copied their code from a website and end up seeing this, please note that I (crystalgem) am one of your students, not someone that your students copied this code from. ;) ]
Last edited on
Topic archived. No new replies allowed.