帮助,我是C+的新手+;我需要一些建议 #包括 #包括 使用名称空间std; int main() { cout>x; { char*string=x; 整数字母_计数[26]; //初始化 对于(int i=0;i 64&&string[i]96&&string[i]='A'&&&*it='A'&&*你想问的问题是什么?你肯定应该包括来自编译器的实际错误消息;它们可能会具体解释问题(和解决方案!)。你使用的是哪种编译器(如.VS 2008、g++等)在main的和处返回0?如果你认为这是一个好习惯,那么可能……也许。否则,main在完成时自动返回0,即使没有明确的return 0语句。main在这方面是特别的。我想你是对的,它在main中是不必要的,我只是想让他知道他应该从他的函数返回。我如何在范围中定义x?string不是一个关键字,但它在这个上下文中是一种类型,因为使用名称空间std;。 string x; cout << "Input a Sentence: "; string line; std::getline(std::cin , line); int letter_count[26]; // Initialization for(int i= 0; i<26; i++) { letter_count[i] = 0; } // Counting the number of letters for(int i = 0; line[i] != '\0'; i++) { if(line[i] >= 'a' && line[i] <= 'z'){ letter_count[line[i]-'a']++; }else if (line[i] >= 'A' && line[i] <= 'Z'){ letter_count[line[i]-'A']++; }else if (line[i] == '.') break; } // Show the result #include <iostream> // include the header containing cout and cin #include <string> // include the header containing the string class // using namespace std; // don't do this. Prefix standard library names with the std namespace instead int main() { std::cout << "Input a Sentence: "; std::string x; // declare a variable named x, so we can use it afterwards - and use the C++ string class, not char* //std::cin >> x; // only reads a word into x. If we want a sentence, use getline instead: std::getline(cin, x); int letter_count[26] = {}; // automatically fills the array with zeros // Counting the number of letters for(std::string::iterator it = x.begin(); it != x.end(); ++it) { // might as well use iterators. That's what they're for. And then your code behaves better if the string actually contains a \0 character. if(*it >= 'A' && *it <= 'Z'){ // never leave out the {}'s. It'll blow up on you later, when you modify the code letter_count[*it - 'A']++; } else if (*it >= 'a' && *it <= 'z'){ letter_count[*it-'a']++; } else if (*it == '.'){ break; } } // Show the result for(int i=0; i < 26; i++) if (letter_count[i] != 0) std::cout << letter_count[i] << " "<< static_cast<char>(i+97) << std::endl; // prefer C++-style casts } }

帮助,我是C+的新手+;我需要一些建议 #包括 #包括 使用名称空间std; int main() { cout>x; { char*string=x; 整数字母_计数[26]; //初始化 对于(int i=0;i 64&&string[i]96&&string[i]='A'&&&*it='A'&&*你想问的问题是什么?你肯定应该包括来自编译器的实际错误消息;它们可能会具体解释问题(和解决方案!)。你使用的是哪种编译器(如.VS 2008、g++等)在main的和处返回0?如果你认为这是一个好习惯,那么可能……也许。否则,main在完成时自动返回0,即使没有明确的return 0语句。main在这方面是特别的。我想你是对的,它在main中是不必要的,我只是想让他知道他应该从他的函数返回。我如何在范围中定义x?string不是一个关键字,但它在这个上下文中是一种类型,因为使用名称空间std;。 string x; cout << "Input a Sentence: "; string line; std::getline(std::cin , line); int letter_count[26]; // Initialization for(int i= 0; i<26; i++) { letter_count[i] = 0; } // Counting the number of letters for(int i = 0; line[i] != '\0'; i++) { if(line[i] >= 'a' && line[i] <= 'z'){ letter_count[line[i]-'a']++; }else if (line[i] >= 'A' && line[i] <= 'Z'){ letter_count[line[i]-'A']++; }else if (line[i] == '.') break; } // Show the result #include <iostream> // include the header containing cout and cin #include <string> // include the header containing the string class // using namespace std; // don't do this. Prefix standard library names with the std namespace instead int main() { std::cout << "Input a Sentence: "; std::string x; // declare a variable named x, so we can use it afterwards - and use the C++ string class, not char* //std::cin >> x; // only reads a word into x. If we want a sentence, use getline instead: std::getline(cin, x); int letter_count[26] = {}; // automatically fills the array with zeros // Counting the number of letters for(std::string::iterator it = x.begin(); it != x.end(); ++it) { // might as well use iterators. That's what they're for. And then your code behaves better if the string actually contains a \0 character. if(*it >= 'A' && *it <= 'Z'){ // never leave out the {}'s. It'll blow up on you later, when you modify the code letter_count[*it - 'A']++; } else if (*it >= 'a' && *it <= 'z'){ letter_count[*it-'a']++; } else if (*it == '.'){ break; } } // Show the result for(int i=0; i < 26; i++) if (letter_count[i] != 0) std::cout << letter_count[i] << " "<< static_cast<char>(i+97) << std::endl; // prefer C++-style casts } },c++,C++,对于初学者,在使用它之前必须声明x 你也可以改变 #include <iostream> #include <string> using namespace std; int main() { cout << "Input a Sentence: "; cin >> x; { char* string = x; int letter_count[26]; // Initialization for(int i=0;

对于初学者,在使用它之前必须声明
x

你也可以改变

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

int main()
{
  cout << "Input a Sentence: ";
  cin >> x;
  {
  char* string = x;
  int letter_count[26];

  // Initialization
  for(int i=0; i<26; letter_count[i++]=0);

  // Counting the number of letters
  for(int i = 0; string[i] != '\0'; i++) {
    if(string[i] > 64 && string[i] < 91)
      letter_count[string[i]-65]++;
    else if (string[i] > 96 && string[i] < 123)
      letter_count[string[i]-97]++;

    else if (string[i] == '.')
      break;
  }

  // Show the result

  for(int i=0; i < 26; i++)
    if (letter_count[i] != 0)
      std::cout << letter_count[i] << " "<< char(i+97) << std::endl;
  }
}

不要将单词string用作变量名,您将包含定义具有相同名称的类的string.h头


是的,如果您编写一个带有特定问题的问题,那么它会更好,因为您没有声明
x
变量。它应该是
std::string

int letter_count[26] = {0};
读取输入后,您声明了一个名为
string
(带
char*string=x;
)的变量。如果省略该行,只需在现在使用
string
的地方使用
x
,您的程序就可以正常编译

它也已经几乎达到了我想你想要的效果。


  • 您的代码将无法编译。您在
    cin中使用了
    x
    。代码中的具体问题是您在使用前没有声明变量
    x
    。(您的声明很奇怪。若要声明变量,请将类型后跟名称,如
    char*x
    ,也可以选择后跟赋值来初始化它,
    char*x=“hello world”
    。声明变量后,编译器将允许您使用它

        cout << "Input a Sentence: ";
        string line;
        std::getline(std::cin , line);
    
    int letter_count[26];
    
    // Initialization
    for(int i= 0; i<26; i++)
    {
        letter_count[i] = 0;
    }
    
    // Counting the number of letters
    for(int i = 0; line[i] != '\0'; i++) {
        if(line[i] >= 'a' && line[i] <= 'z'){
                letter_count[line[i]-'a']++;
        }else if (line[i] >= 'A' && line[i] <= 'Z'){
                letter_count[line[i]-'A']++;
        }else if (line[i] == '.')
                break;
    }
    
    // Show the result
    
    #include//include包含cout和cin的头文件
    #include//include包含字符串类的标头
    //使用命名空间std;//不要这样做。改为使用std命名空间作为标准库名称的前缀
    int main()
    {
    std::cout>x;//只将单词读入x。如果我们想要一个句子,请使用getline:
    标准:getline(cin,x);
    int letter_count[26]={};//自动用零填充数组
    //计算字母数
    对于(std::string::iterator it=x.begin();it!=x.end();++it){//最好使用迭代器。这就是迭代器的用途。如果字符串实际包含\0字符,那么代码的性能会更好。
    
    如果(*it>='A'&&&*it='A'&&*你想问的问题是什么?你肯定应该包括来自编译器的实际错误消息;它们可能会具体解释问题(和解决方案!)。你使用的是哪种编译器(如.VS 2008、g++等)
    main
    的和处返回0
    ?如果你认为这是一个好习惯,那么可能……也许。否则,
    main
    在完成时自动返回
    0
    ,即使没有明确的
    return 0
    语句。
    main
    在这方面是特别的。我想你是对的,它在
    main中是不必要的
    ,我只是想让他知道他应该从他的函数返回。我如何在范围中定义x?
    string
    不是一个关键字,但它在这个上下文中是一种类型,因为
    使用名称空间std;
    string x;
    
        cout << "Input a Sentence: ";
        string line;
        std::getline(std::cin , line);
    
    int letter_count[26];
    
    // Initialization
    for(int i= 0; i<26; i++)
    {
        letter_count[i] = 0;
    }
    
    // Counting the number of letters
    for(int i = 0; line[i] != '\0'; i++) {
        if(line[i] >= 'a' && line[i] <= 'z'){
                letter_count[line[i]-'a']++;
        }else if (line[i] >= 'A' && line[i] <= 'Z'){
                letter_count[line[i]-'A']++;
        }else if (line[i] == '.')
                break;
    }
    
    // Show the result
    
    #include <iostream> // include the header containing cout and cin
    #include <string> // include the header containing the string class
    // using namespace std; // don't do this. Prefix standard library names with the std namespace instead
    
    int main()
    {
      std::cout << "Input a Sentence: ";
      std::string x; // declare a variable named x, so we can use it afterwards - and use the C++ string class, not char*
      //std::cin >> x; // only reads a word into x. If we want a sentence, use getline instead:
      std::getline(cin, x);
      int letter_count[26] = {}; // automatically fills the array with zeros
    
      // Counting the number of letters
      for(std::string::iterator it = x.begin(); it != x.end(); ++it) { // might as well use iterators. That's what they're for. And then your code behaves better if the string actually contains a \0 character.
        if(*it >= 'A' && *it <= 'Z'){ // never leave out the {}'s. It'll blow up on you later, when you modify the code
          letter_count[*it - 'A']++;
        }
        else if (*it >= 'a' && *it <= 'z'){
          letter_count[*it-'a']++;
        }
        else if (*it == '.'){
          break;
        }
      }
    
      // Show the result
    
      for(int i=0; i < 26; i++)
        if (letter_count[i] != 0)
          std::cout << letter_count[i] << " "<< static_cast<char>(i+97) << std::endl; // prefer C++-style casts
      }
    }