In this program I'm asking the user to input a word at least 5 characters, then a single character to show how many times it repeats in the world.
I am using xcode for the compiler. When I tried to write char ch = word.at(i) in xcode. The system always says "unused variable ch".
I do not know what I did wrong. Please help.
Thanks in advance
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Ask the user to input a word while the world lenght < 5
string word = "";
do
{
cout << "enter a wrold that has at least 5 characters: " << endl;
cin >> word;
}while (word.size() < 5);
// Ask the user to inpute a character
char searchCH = '0';
cout << "Enter a character and the program will tell you how many times it appears in the word " << word << "." << endl;
cin >> searchCH;
int counter = 0;
// Iterate over the word
for(int i = 0; i < (int)word.size(); i++)
// Get a character
char ch = word.at(i);
// If the character matches the character we are looking for
if(searchCH == ch)
{
// Output the number of times the character was found in the word.
cout << "The number of " << searchCH << "'s in the world " << word << " is " << counter << endl;
#include <iostream>
#include <string>
int main()
{
// Ask the user to input a word while the world lenght < 5
std::string word = "";
do
{
std::cout << "enter a wrod that has at least 5 characters: ";
std::cin >> word;
}
while (word.size() < 5);
// Ask the user to inpute a character
char searchCH = '\0';
std::cout << "Enter a character and the program will tell you how many times it appears in the word " << word << ".\n";
std::cin >> searchCH;
int counter = 0;
// Iterate over the word
for (unsignedint i = 0; i < word.size(); i++)
{
// Get a character
char ch = word.at(i);
// If the character matches the character we are looking for
if (searchCH == ch)
{
// Increment a counter
counter++; // counter = counter + 1
}
}
// Output the number of times the character was found in the word.
std::cout << "The number of " << searchCH << "'s in the world " << word << " is " << counter << "\n";
}
#include <iostream>
usingnamespace std;
int main(){
char word;
char search;
int letter = 0;
cout << "Enter a word";
cin.get(word);
cout << "Enter the letter you want found";
cin.get(search);
while (cin.get(word)){
if ( !(cin.get(word))){ //checks for bad input, like numbers
cin.clear(); /clears bad stuff
while(cin.get(word) != '/n')
continue; //removes bad stuff from input
}
}
elseif(cin.get(word) == search)
letter++;
}
cout << letter << " " << search << "'s were found.";
}
This is how I would have done it. If you don't understand how it works, I'll explain
Btw I never ran this code. I typed it on my ipad