Help With Calling/Passing String Function Into Main

Hello everyone,

Beginner C++ student here, first ever programming class. I am trying to put together the program below. It's supposed to ask the user for an input string, then a specific char to look for within that string, and output how many times it appears.

For example:

input: 1+2+3

look for: '+'

It appears 2 times.

I am learning string functions in the format shown in the code, where I call a function. However, I am still learning how to call the function in the main. So, I am wondering if someone kindly advise as to how I can tell the main to use the function howMany and output the 'counter' value from the loop.

Thank you very much!

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

using namespace std;

bool die(const string & msg);

unsigned howMany(char c, const string & s, unsigned counter);

int main() {

	char c;
	string s;
	unsigned counter = 0;

	cout << "Enter a string: " << endl;
	getline(cin, s);
	cout << "Enter char: " << endl;
	cin >> c;

	cout << "The char chosen appears " << howMany(counter) << endl; //<---- **HELP WITH COUT HERE**

}

unsigned howMany(char c, const string & s, unsigned counter) {

	//unsigned counter;

	for (unsigned i = 0; i < s.length(); i++){
		if (s[i] == c){
			counter++;

		}

		return counter;
	}


}

bool die(const string & msg){
	cout << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}
Last edited on
There is an extra unnecessary parameter in the function header.
It should look more like this:
 
unsigned howMany(char c, const string & s);

Change both the prototype (line 10) and the header of the function definition (line 27).

In main(), call the function as simply howMany(c, s)


First you need to look at how many parameters are in your function 'howMany'.

The answer is 3.

To call a function inside main(), you need to type the functions name with its parameters:

1
2
3
4
5
6
7
8
9
int main()
{
...
...
...

howMany(c, s, counter)

} 


It's important to note that you need to, of course, put real values in the function parameter.

So, instead of c, s, counter, you need to put values that you want to pass into the function.

In your code, at line 23, you're passing one parameter which isn't sufficient for the function to work.

cout << "The char chosen appears " << howMany(counter) << endl;

that should be changed to:

cout << "The char chosen appears " << howMany(c,s,counter) << endl;
Last edited on
First you need to look at how many parameters are in your function 'howMany'.

Surely the first thing is to look at what parameters the function needs in order to do its job.
It needs:
1. the string
2. the character to search for.

The resulting number is passed back to the calling function as the return value.

The variable 'counter' is part of the internal working of the function, the rest of the program doesn't care about what goes on inside the function.
I agree. I don't see why he put counter in the function parameter.
Thank you very much for all the input everyone. It's making more sense now. Learned some more and final working 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

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

bool die(const string & msg);

unsigned howMany(char c, string & s);

int main() {

	char c;
	string s;

	cout << "Enter a string: ";
	getline(cin, s);
	cout << "Enter char: ";
	cin >> c;

	cout << "The char chosen appears " << howMany(c, s) << endl;


}

unsigned howMany(char c, string & s) {

	transform(s.begin(), s.end(), s.begin(), ::tolower);

	size_t n = count(s.begin(), s.end(), c);

	return n;

}

bool die(const string & msg){
	cout << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}
Topic archived. No new replies allowed.