New to Boolean; Need some help.

Pages: 1234
Here's an example of using a function.

1
2
3
4
5
6
7
8
9
10
11
int func(int n)
{
    return n + 2;
}

int main()
{
    int a = 6;
    int b = func(a);
    cout << b << endl;
}


Both main and func depend on the variable a. You could actually rename the variable in func from n to a without affecting the program. The value is passed to func as an argument without needing a global variable.
You have to declare it in both

like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int doubler (int x)
{
    int integer = x;
    return integer * 2;
}

int main ()
{
    int integer;
    std::cin >> integer;
    integer = doubler (integer);
    std::cout << "doubled number: " << integer << std::endl;

    return 0;
}


Local variables only exist in their own scope. Anything outside that scope does not know it exists.

FYI: Use code tags. They make code MUCH easier to read.
Yeah.. I thought I understood how to pass values between functions, but I'm clearly not getting it at all.


Even this code:

int func(int n)
{
return n + 2;
}

int main()
{
int a = 6;
int b = func(a);
cout << b << endl;
}

doesn't click with me as to how anything is passed between them.
Someone link me to a decent tutorial?
At first glance, this tutorial seems to explain them adequately (naming conventions aside):
http://www.learncpp.com/cpp-tutorial/14-a-first-look-at-functions/

Or the tutorial on this site:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Awesome! The tutorial on this site was a bit confusing, but the one on learncpp.com was absolutely great, and I think I'm understanding it with a lot more clarity!

next question:

Could someone explain to me the previously posted loop?

1
2
3
4
5
if (int i=0;i<Word.length();i++)
{
  char c=World[i];
  //do stuff with c
}


1- Why would I be set to 0?
2- How do I use a length function?
3- char c = world[i]; <- makes no sense to me. Why would one character equal the entire word?
I think I see it..

int i is a counter for the word length, starting at zero. I see.
++i until word length is reached.
Though to me, it seems that this would be better suited in a while loop.

the [i] still confuses me though. These brackets mean?
I feel that I'm getting closer...

Perhaps someone could guide me even further with information on what I'm doing wrong?

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
// Program to Count the Vowels in a Word.


// Declarations
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string input();
int countVowels();


// Main Function
int main(){

int vCount;

string input();
countVowels();

cout << "There are " << vCount << " vowels in your word." << endl; 
return 0;
}

// Input Function
string input(){

string word;

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

return word;
}

// Count Vowel Function
int countVowels(){
   bool   isVowel;
   char   Let;
   int    vCount = 0;
   int    i = 0;
   string word;

   while (i < word.length()){
   char Let = word [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 )
   ++vCount;
   ++i;
   }
   
   return vCount;
}


I got quite a few errors on this one. >_>
Last edited on
1>c:\projects\junk\junk\main.cpp(45): warning C4018: '<' : signed/unsigned mismatch
1>c:\projects\junk\junk\main.cpp(40): warning C4101: 'Let' : unreferenced local variable
1>c:\projects\junk\junk\main.cpp(39): warning C4101: 'isVowel' : unreferenced local variable
1>c:\projects\junk\junk\main.cpp(21): warning C4700: uninitialized local variable 'vCount' used


No errors, just warnings. On line 45: string::length() returns an unsigned value (after all, a string can't have a negative length) and you compare it to a signed value when you compare to i.

On line 40, You declare a variable 'Let'.. on line 46 you declare another variable named 'Let' within a different scope. The variable declared on line 46 hides the one declared on 40, so the variable on line 40 is never used.

The same goes for line 39 and isVowel. (The variable that hides it can be found in line 47.)

And, finally, at line 16 you declare the variable vCount but never assign any value to it, thus the warning when you use it at line 21.

You need a way for countVowels to know what word you want it to work on, and you need to use the return value from the function.

Ideally, your main would look something like:

1
2
3
4
5
6
7
8
9
// Main Function
int main(){

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

     cout << "There are " << vCount << " vowels in " << word << endl; 
     return 0;
}

Last edited on
cire, I love you! Thank you for pointing out my specific errors. I finally did it!

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
// Program to Count the Vowels in a Word.


// Declarations
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string input();
int countVowels();


// Main Function
int main(){

int vCount = countVowels();

cout << "There are " << vCount << " vowels in your word." << endl; 
return 0;
}

// Input Function
string input(){

string word;

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

return word;
}

// Count Vowel Function
int countVowels(){
   int    vCount = 0;
   int    i = 0;
   string word = input();

   while (i < word.length()){
   char Let = word [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 )
   ++vCount;
   ++i;
   }
   
   return vCount;
}


..and it works correctly! =D

I suppose I should ask if there are any other ways to tidy this up a bit. It seems a bit cluttered.
Also, I need to understand how line 41 of my code works.
I understand everything except for that.

char Let = word [i];

1) How is this pulling out each letter from the word?
2) What do the square brackets accomplish?
The array subscript operator (the []) is defined by the string class to get the i-th character of the string. It's used so you can treat the string like an array of characters.
Got it.

Oh! One last question; What sort of loop would you guys suggest I use to repeat this entire program without it ending?

I typically use something like:

while (1<2){
int main();
other function;
other function;

ask user if they want to quit: if yes, exit command.
}

It's basically an infinite loop that can be terminated by request at the end of each loop. I'm sure there's an easier way. Any suggestions?
I lied; here's a second question:

If I wanted to add:

cout << "There are " << word.length << " characters in your word." << endl;

to the int main(), how would I call it from the countVowels function?
I've already called the vCount, and from what I understand, you can only call 1 return from each function. Can I call both vCount and word.length?
Well, right now countVowels is calling your input function, which, although there's nothing wrong with it technically speaking, it isn't really great from a design perspective. If your main looked as I said your main would look like "ideally" you'd be able to add that with no problem (provided, of course, you used word.length() as opposed to the same without parentheses.)

You should design code to be reused. If countVowels took a string (or const reference to a string) as a parameter, it would be decoupled from the input function and so be useful in a wider variety of circumstances. Say you wanted to get your 'word' from a file? Right now you'd have to change the input function so it got the string from a file. But, what if you wanted to mix words from a file and an active user? It's better, generally speaking, to decouple input from processing.
Last edited on
My 2 questions still stand.

On a side note, I found an easier way to accomplish everything without using sub functions:

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

int main(){

string word;
int    vCount = 0;
int    i = 0;

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

   while (i < word.length()){
   char Letter = word [i];
   bool isVowel (Letter == 'a' || Letter == 'e' || Letter == 'i' || 
                 Letter == 'o' || Letter == 'u' || Letter == 'A' ||
                 Letter == 'E' || Letter == 'I' || Letter == 'O' ||
                 Letter == 'U');
   if (isVowel)
   ++vCount;
   ++i;
   }

cout << "Your " << word.length() << " letter word has " << vCount << " vowels in it." << endl; 
return 0;
}


If sub functions weren't required, I wouldn't even bother using them. They seem to add nothing but clutter.

Anyway, my Loop and Returning 2 values questions both stand still. Any takers?
See the post before your last. Perhaps it'll also shed some light on what value your "sub functions" may add.
Well, right now countVowels is calling your input function, which, although there's nothing wrong with it technically speaking, it isn't really great from a design perspective. If your main looked as I said your main would look like "ideally" you'd be able to add that with no problem (provided, of course, you used word.length() as opposed to the same without parentheses.)

You should design code to be reused. If countVowels took a string (or const reference to a string) as a parameter, it would be decoupled from the input function and so be useful in a wider variety of circumstances. Say you wanted to get your 'word' from a file? Right now you'd have to change the input function so it got the string from a file. But, what if you wanted to mix words from a file and an active user? It's better, generally speaking, to decouple input from processing.


I've tried your ideal main way, but the (word) in int vCount = countVowels(word); always becomes undefined and refuses to work.
Last edited on
It would be easier to see what you're doing wrong if you posted code. Here's yours with a few minor modifications:

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

using namespace std;

string input();
int countVowels(string);

// Main Function
int main(){

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

	cout << "There are " << vCount << " vowels in " << word << " which has " << word.length() << " letters.\n" ; 
	return 0;
}

// Input Function
string input(){

	string word;

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

	return word;
}

// Count Vowel Function
int countVowels(string word){
	int    vCount = 0;
	int    i = 0;

	while (i < word.length()){
		char Let = word [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 )
			++vCount;
		++i;
	}

	return vCount;
}
Hmm.. okay.

I suppose I need clarification on one additional thing then.

What I understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){

	string word = input() ;

	cout <<  word << " is your word "  << endl;; 
	return 0;
}

// Input Function
string input(){

	string word;

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

	return word;
}


I understand that the Input Function returns 'word' to the main function as the new "input ()". That makes sense.
What I don't understand is the purpose or process that occurs when you put a word inside the parenthesis. In your example, line 13 = int vCount = countVowels(word);, the countVowels function is only returning the vCount. What good does putting (word) in the parenthesis accomplish?

(Bear with me here; I really am trying to learn this. ^_^)
That is the syntax you use when passing arguments to functions. For example, in your code on line 3 you are "calling" input with no arguments. If you did something like:

string word = input(something);

Then you are trying to call input with one argument (something, in this case).
Pages: 1234