user2939276
user2939276

Reputation: 61

Counting characters in a string

My code runs and works well the first time around, but I am having looping problems:

  1. My code isn't counting characters that are in words

  2. The second time around when you press "yes," it ends up printing everything out. I must have a loop in the wrong spot, but I can't find it for the life of me.

#include <string> 
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    char character;
    string sentence;
    char answer;
    int cCount;
    while(1) {

        cout << "Enter a character to count the number of times it is in a sentence: ";
        cin >> character;
        cout << "Enter a sentence and to search for a specified character: ";
        cin >> sentence;
        if(character == '\n' || sentence.empty())
        {
            cout << "Please enter a valid answer:\n";
            break;

        }
        else {
            cCount = count(sentence.begin(), sentence.end(), character);
            cout << "Your sentence had" << " " << cCount << " " << character << " " << "character(s)" << '\n'; 
         }

    cout << "Do you wish to enter another sentence (y/n)?: \n";
    cin >> answer;
    if (answer == 'n'){
        break;
        }
    }
return 0;
}

Upvotes: 0

Views: 442

Answers (6)

Phillip Ngan
Phillip Ngan

Reputation: 16086

Try:

cCount = count(sentence.c_str(), sentence.c_str()+sentence.length(), character);

Upvotes: -1

user
user

Reputation: 130

This is working, try this.

#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    char character;
    string sentence;
    char answer;
    int cCount;
    while(1) {

        cout << "Enter a character to count the number of times it is in a sentence: ";
        cin >> character;
        cout << "Enter a sentence and to search for a specified character: ";
        fflush(stdin);
        getline(cin, sentence, '\n');
        if(character == '\n' || sentence.empty())
        {
            cout << "Please enter a valid answer:\n";
            break;

        }
        else {
            cCount = count(sentence.begin(), sentence.end(), character);
            cout << "Your sentence had" << " " << cCount << " " << character << " " << "character(s)" << '\n';
         }

    cout << "Do you wish to enter another sentence (y/n)?: \n";
    cin >> answer;
    if (answer == 'n'){
        break;
        }
    }
return 0;
}

after you enter first input and enter that enter is considered as input in sentence So, you need to flush that and after that you can scan that sentence.

Upvotes: 0

David
David

Reputation: 4873

By just reading your code, it looks fine, except where you get the sentence. Using cin, it will only read until it sees a newline or a space, so if you're entering a sentence, it will read every word as a different input.

Try getline(cin, sentence) and see if that fixes the problem.

Edit: Forgot to add in: use cin.ignore() after the getline. cin reads up to, and including the line break (or space) while getline only reads up to the line break, so the line break is still in the buffer.

Upvotes: 2

CLS
CLS

Reputation: 131

use cin it will end with a newline or a space eg: when you input hello world it will get hello

and you can try getline

it wiil end with a newline

Upvotes: 0

Saad Abdullah
Saad Abdullah

Reputation: 2432

use

cin.ignore();  //dont forget to use cin.ignore() as it will clear all previous cin
getline(cin, sentence, '\n'); //take the sentence upto \n i.e entered is pressed

Upvotes: 1

john
john

Reputation: 87959

You don't have the loops wrong. What's wrong is that you are assuming that

cin >> sentence;

does something different from what it really does.

If you want to read a line of text, then do this

getline(cin, sentnence);

Your code reads a single word only.

Upvotes: 0

Related Questions