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.
#include<iostream>
#include<string>
usingnamespace 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 << ".";
}
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.
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.