简体   繁体   English

计算字符串中的字符数

[英]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;
}

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. 使用cin,它只会在看到换行符或空格之前读取,所以如果你输入一个句子,它会将每个单词作为不同的输入读取。

Try getline(cin, sentence) and see if that fixes the problem. 尝试getline(cin,sentence),看看是否能解决问题。

Edit: Forgot to add in: use cin.ignore() after the getline. 编辑:忘记添加:在getline之后使用cin.ignore()。 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. cin读取并包括换行符(或空格),而getline只读取换行符,因此换行符仍在缓冲区中。

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

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. 您的代码只读一个单词。

use cin it will end with a newline or a space eg: when you input hello world it will get hello 使用cin它会以换行符或空格结束,例如:当你输入hello world它会hello

and you can try getline 你可以试试getline

it wiil end with a newline 它会以换行结束

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. 在您输入第一个输入并输入后,输入被视为句子中的输入因此,您需要刷新它,之后您可以扫描该句子。

尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM