How to add 1 to a variable when a if statement is sufficed

Write your question here.
currently I have this code written it all seems to be in order other than I am unable to add 1 to a variable when two variable equal each other.

the code takes a word of 5 or more letters, it then takes a character, and gives you back the number of times that character is used within that code. so I have everything up to the part where if the letter within the word matches the letter you are looking for it is suppose to add 1 to an external exponent... problem is, I have no idea how to do 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
  #include<iostream>
#include<string>

using namespace std;

int main()
{
	//ask the user to enter word
	string word = "";
	cout << "Enter a word at least 5 characters in length; \n";
	cin >> word;
	//validate word
	int length = word.size();
	if(length >= 5)
	{
		//ask user to input character
		char ch = 'a';
		cout << "Enter the letter you would like to know the # of within the previously supplied word: \n";
		cin >> ch;
		//check code for # of character
		cout << ch << "\n";
		int freq = 0;

		int i = 0;
		while(i < length)
		{
			char x = word.at(i);
			cout << x << "\n";
			if(x == ch)
			{
			[THIS IS WHERE I NEED TO ADD TO THE EXPONENT freq]	
			}
			i++;
		}


	}
	if(length < 5)
	{
			cout << "The word you entered is invalid";
			return 0;
	}
	cout << "There are " << freq << " " << ch << "s in the word " << word << ".";
}


I just started coding today so please be nice :)
Last edited on
Move int freq = 0; outside and before the if(length >= 5).
and just use normal freq++;.
Last edited on
Declare freq before your first if statement. Inside your while statement, increment freq (freq++) when x == ch.
Look up how your scope defines which variables exist.

Once it's set up properly, you can easily just do
1
2
3
4
5
if(x == ch)
{
    freq++;
}
i++;
Also note that it would be more straightforward if you used a for loop instead of a while loop, but this is minor issue.
Last edited on
thanks,
so here is the new 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
int main()
{
	//ask the user to enter word
	string word = "";
	do
	{
	cout << "Enter a word at least 5 characters in length; \n";
	cin >> word;
	}while(word.size() < 5);
	//validate word

		//ask user to input character
		char ch = 'a';
		cout << "Enter the letter you would like to know the # of within the previously supplied word: \n";
		cin >> ch;
		//check code for # of character
		int freq = 0;

		int i = 0;
		while(i < word.size())
		{
			char x = word.at(i);
			if(x == ch)
			{
				freq++;
			}
			i++;
		}

		cout << "The word " << word << ", contains " << freq << " " << ch << "'s.";
		return 0;
}


I want the program to automatically restate the first question "enter a word at least 5 characters in length" if the word given is less than 5 characters.

also, how do I make it so that when I run my program in cmd, the cmd window does not close as soon as the code ends.
Last edited on
Your program's fine.
What you want program to do after finished? Wait for any key pressed, isn't it?

Normally without IDE running, after processed if you don't place pause syntax the program will always end itself.

see system("pause");
Last edited on
it tells me that function system cannot be resolved... do I need to include something specific?
Last edited on
It's very easy to find if you try to do, just google once,

btw, this lib.
#include <cstdlib>
Last edited on
Topic archived. No new replies allowed.