将<cstdlib>函数调用与<string>函数混合使用会产生许多错误

时间:2019-01-04 07:48:43

标签: c++

我正在尝试创建一个程序,该程序计算用户给定单词中的元音和辅音的数量。我使用了诸如strlen()之类的函数来获取用于迭代的用户输入数组的长度。我还使用了bits / stdc ++。h,因此我可以在用户输入中出现元音时调用count()函数。

为了检查元音的出现,我尝试了几个从count开始的函数,分别是find_first_of,find()和count()。我的第一个错误是说它无法识别对strlen()的调用。我检查以确保包含正确的软件包以使用strlen(),但这似乎不是问题。我正在具有High Sierra的MacBook Pro上运行此程序。

  #include <iostream>//std::cout
  #include <string>//std::string
  #include <cstdlib>
  #include <set>// std::size_t
  #include "/Users/richardlopez/Desktop/stdc++.h"

  using namespace std;

  int main(){
      string input = "hello";
      cout << " The input is " << input << endl;
      std::size_t vowelsFound = 0;

      /*cout << " Enter a word, and we will count the vowels and    consonants: ";
      cin >> input;
      cout << " You entered " << input << endl;*/

      char cons[] =    {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};

      char vows[] = {'a','e','i','o','u','y'};

      std::size_t n = strlen(input);
      std::size_t v = strlen(vows);

      for(int i = 0; i < v; i++){
          if(count(input,input + n,vows[i]) != 0){
              vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows[] encountered in input
              cout << " The vowel(s) found is " << vowelsFound << endl;
    }
          else{
              cout << " The vowel " << vows[i] << " is not found in input " << endl;
    }
}

  cout << " The amount of vowels found is " << vowelsFound << endl;
  cout << " The expected amount of vowels found is 2 " << endl;
}

我对短语“ hello”进行了硬编码以进行输入,因此在说完所有内容后,元音数应为2。

3 个答案:

答案 0 :(得分:1)

您的代码中存在许多问题:

  1. 我不知道"/Users/richardlopez/Desktop/stdc++.h"包含的内容是什么,但包含它不太可能是一件好事。您可能希望#include <cstring>得到strlen
  2. 您不能在strlen上使用std::string,它仅适用于以null结尾的字符数组。您应该只使用input.size()
  3. 您也不应在strlen上使用vows,因为它虽然是字符数组,所以将其编译为非null终止,因此strlen的返回值是不确定的。您可以使用sizeof(vows)或仅将vows设为std::stringstd::vector<char>
  4. count(input,input + n,vows[i])不正确。 input + n无法编译。您可能是count(input.begin(),input.end(),vows[i])

更正上述问题并使用现代c ++会得到简单的代码:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
  std::string input = "hello";
  std::cout << " The input is " << input << "\n";
  std::size_t vowelsFound = 0;

  std::string cons = "bcdfghjklmnpqrstvwxyz";
  std::string vows = "aeiou";

  for ( auto v : vows )
  {
      size_t c = std::count(input.begin(), input.end(), v);
      if ( c != 0 )
      {
          vowelsFound += c;
          std::cout << "found " << c << " " << v << "\n";
      }
      else
      {
          std::cout << " The vowel " << v << " is not found in input\n";              
      }
  }

  std::cout << " The amount of vowels found is " << vowelsFound << "\n";
  std::cout << " The expected amount of vowels found is 2\n";
}

如果只需要元音总数,就可以使用:

std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
  {
    return vows.find(c) != std::string::npos;
  }) << "\n";

答案 1 :(得分:0)

在“ /Users/richardlopez/Desktop/stdc++.h”之上的几个问题。如果您需要特定的功能,请查看参考,并为该功能提供适当的官方C ++标头。

第一个strlen用于以0结尾的char*。它不适用于input。为此,只需使用:

auto n = input.size();

然后vows也一样:

std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();

然后count也不同:

std::count(std::begin(input) ,std::end(input), vows[i])

如果您有C ++ 17,请执行以下操作:

if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
    vowelsFound += count;
    std::cout << count << std::endl;
}

答案 2 :(得分:0)

您可以使用分区算法。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() {

    std::string input = "hello";
    std::cout << " The input is " << input << std::endl;

    std::vector<char> vows = {'a','e','i','o','u','y'};
    auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
    std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
    std::cout << " The expected amount of vowels found is 2 " << std::endl;


    // If you also want to count consonants (for example if the input can also have digits or punctuation marks)
    std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
    auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
    std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;

}

输出:

输入是你好
找到的元音数量是2
预期的元音数量为2
发现的辅音数量是3


std :: size_t是sizeof的返回类型。因此,使用它来遍历容器是很自然的。另外,由于它是某些库使用的标准类型,因此使代码更易于阅读。