2

I'm trying to compare two characters (my search character, and the characters in an input string) to see how many of my search characters are present in the given string. But, all I am getting for the count is 0, no matter what I input.

Below is my code for reference:

#include <iostream>
#include <string> 
using namespace std;

int main() {
    string str;
    int length = 0;
    char ch;
    char search;
    int count = 0;

    length = str.length();

    cin >> search;
    cin >> str;

    for (int i = 0; i < length; i++) {

        //current character
        ch = str[i];
        //cout << ch << " ";
        if (ch == search) {
            count += 1;
        }
    }
    cout << count << " " << search << "'s" << endl;

    return 0;
}

I've tried both ch.compare(search) and (ch == search), as seen in my code. Both just result in me getting 0 for count. I've also flipped ch and search, and tried to set both search and ch to ' ', but again none of it did anything. Example of one of the tests I've tried is below.

How many e's are in the string "Geek".

I don't know what I am doing wrong, as all I'm getting as the output is 0 for count.

For example:

Input: "Geek"
Wanted Output: 2 e's
What I'm getting: 0 e's

I'm using CLion for my IDE, with my Language Standard set at C++23.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    *How many e's are in the string "Geek"* -- `std::count(str.begin(), str.end(), 'e');` – PaulMcKenzie Apr 22 '23 at 23:24
  • 2
    C++ isn't a spreadsheet. `length = str.length();` sets `length` to 0, because that's the length of `str` at that point in the code. Later changes to `str` don't change `length`. You have to set `length` again after `str` changes. – Pete Becker Apr 22 '23 at 23:39

1 Answers1

4

You are retrieving the value of str.length() before you have read the user's input into str. str is still empty at that time, and so your length variable is always 0, thus your loop never runs.

You need to read the str.length() value after you have read in the input into str:

#include <iostream>
#include <string> 
using namespace std;

int main() {
    string str;
    int length = 0;
    char ch;
    char search;
    int count = 0;

    // length = str.length(); // <-- too early here!

    cin >> search;
    cin >> str;

    length = str.length(); // <-- move it here instead!

    for (int i = 0; i < length; i++) {

        //current character
        ch = str[i];
        //cout << ch << " ";
        if (ch == search) {
            count += 1;
        }
    }
    cout << count << " " << search << "'s" << endl;

    return 0;
}

Online Demo

That being said, you can eliminate the need for reading the length at all by using iterators instead of indexes:

#include <iostream>
#include <string> 
using namespace std;

int main() {
    string str;
    char ch;
    char search;
    int count = 0;

    cin >> search;
    cin >> str;

    for (auto iter = str.begin(); iter != str.end(); ++iter) {

        //current character
        ch = *iter;
        //cout << ch << " ";
        if (ch == search) {
            count += 1;
        }
    }
    cout << count << " " << search << "'s" << endl;

    return 0;
}

Online Demo

Or, by using a range-for loop (which uses iterators internally):

#include <iostream>
#include <string> 
using namespace std;

int main() {
    string str;
    char search;
    int count = 0;

    cin >> search;
    cin >> str;

    for (char ch : str) {
        //cout << ch << " ";
        if (ch == search) {
            count += 1;
        }
    }
    cout << count << " " << search << "'s" << endl;

    return 0;
}

Online Demo

Or, by using the standard std::count() algorithm instead of a manual loop at all:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string str;
    char search;
    int found;

    cin >> search;
    cin >> str;

    found = count(str.begin(), str.end(), search);

    cout << found << " " << search << "'s" << endl;

    return 0;
}

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • You know what, it looks like you know your stuff, so you earned my upvote. However, I would suggest changing endl into '\n' as endl flushes the stream. – Hudson Apr 22 '23 at 23:55
  • @Hudson [Does new line character also flush the buffer?](https://stackoverflow.com/questions/42430701/does-new-line-character-also-flush-the-buffer) – Remy Lebeau Apr 23 '23 at 00:06
  • Oh well, I was told that endl is bad and \n is better. – Hudson Apr 23 '23 at 00:10
  • Thanks, moving it worked. I hadn't realized I had my length() in the wrong position. Also, thanks for showing me two different ways I could also solve my problem. – MoonProgrammer Apr 24 '23 at 15:46