New to Boolean; Need some help.

Pages: 1234

string input();
in the Gloabal Declarations.
Why does it not require: string input(string); instead?
That's because return values go before the function name (that's just how it is).

You don't need one inside the parentheses because the function doesn't take any arguments.
So when declaring a function, it's basically:

return value type -> function name -> specific parameter I wanted to work with
ex: int countVowels(string);

but because in:
string input();
I already declared the return type, I don't need to specify it again as the parameter (because it's the same)?
That's because return values go before the function name (that's just how it is).

You don't need one inside the parentheses because the function doesn't take any arguments.


Ohh, I get it. The input only returns a value, it doesn't swap anything in or out of it as an argument. I get it!!!
I already declared the return type, I don't need to specify it again as the parameter


You don't because the return value *isn't* a parameter.
Is it just me, or is there quite a bit of lag between posting a new message and seeing messages posted before yours on this site?
You don't see the other messages until you refresh the page.
Okay.. I had to swap some variable declarations to get this, but I think I've got 90% of it. I'm going to try to explain it the way I see it. Tell me if this is correct using the reworded code below:

01) Main function attempts to assign value to the string (X).
02) Main function goes to function input(); to find it.
03) User inputs value for Y.
04) function input() returns value Y and assigns it to X in main.
05) Main function attempts to assign value to the integer vCount.
06) Main function goes to function countVowels() to find it, and since X is in the parenthesis, main sens the value X to be acquired by the countVowels() function (X being what Y was, Y being whatever I inputted for the word).
07) In function countVowels, X is assigned to the string (Z). Z now equaling my inputted word.
08) The counter algorithm goes through the boolean true/false system and adds the numbers of Vowels in the word to the NewCount integer.
09) When NewCount is complete (i being equal to the word's length), function countVowels sends the NewCount back to main as the new vCount. (vCount is now identified in Main).
10) Program outputs specified message.

I think I get all of that now!
If I am correct on all of the above, then the only thing I still don't understand is why the global declaration of int countVowels() requires (string) in it.
:D :D :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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string input();
int countVowels(string);


// Main Function
int main(){

	string X = input() ;
	int vCount = countVowels(X);

	cout << "There are " << vCount << " vowels in " << X << " which has " << X.length() << " letters." << endl; 
	return 0;
}


// Input Function
string input(){

	string Y;

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

	return Y;
}

// Count Vowel Function
int countVowels(string Z){
	int    NewCount = 0;
	int    i = 0;

	while (i < Z.length()){
		char Let = Z [i];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if ( isVowel )
			++NewCount;
		++i;}
	return NewCount;
}
In functions, I need to use an identifier to be able to accept variables.
(in countVowels (string Z) accepts any string, then Z now equals that string).

when calling a function, I only need to specify the variable, not the variable type, to send it to that function.
(int vCount = countVowels(X) sends X to countVowels)

!! I really am getting it.. I think!
So, I'll try to explain why I don't understand the Global Declaration part.

In the Global Declaration, this program only works if:
int countVowels(string) has the (string) inside of it.

Well, the declaration isn't really sending anything to the countVowels function, is it? It's just declaring it for usage.

the countVowels function already specifies that it will accept the string value Z.
Is the purpose of (string) in the global declarations merely to point out to the function that its purpose is to accept only strings?
If you leave it out:

int countVowels()

That as the same as saying the function countVowels returns an int and takes no arguments, essentially you're lying to the compiler. Since you're telling the compiler this is so and it doesn't see anything to contradict that or suggest another overload takes a string before you call it, when you try to call it with a parameter it thinks you're doing the wrong thing (and, indeed, you are doing the wrong thing.)

The compiler knows what you tell it. Tell it all you can.
cire and firedraco, can I hire you two as private tutors? You have honestly been a godsend to me.
Last edited on
Anyway, I'm pretty comfortable with all of this now, and I've gotten it to work in various other functions I've tested. So here's a big shout-out to everyone who had the patience to help me learn: THANK YOU! *hugs*
Topic archived. No new replies allowed.
Pages: 1234