Looping and Unused Variable 'ch'

Hello Guys:

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)
{

// Increment a counter
counter++; // counter = counter + 1
}

// 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;

return 0;

}
closed account (E0p9LyTq)
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
#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 (unsigned int 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";
}
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
#include <iostream>
using namespace 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

}
   }
else if(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
Last edited on
Topic archived. No new replies allowed.