我需要帮助修复C++空白检测器

I need help fixing a C++ white space detector

本文关键字:C++ 空白 检测器 帮助      更新时间:2023-10-16

我需要帮助修复以下代码,它是一个 c++ 空格检测器,它使用不同的单词来产生不同的输出:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string Line;
string firstWord[99];
string secondWord[99];
int lineFunction(string line) {
string line[199];
for (int i = 0; i < line.length(); i++) {
while (isspace(line[i]) == false) {
firstWord += line[i];
}
secondWord += line[i];
}
} 
int main() {
cin >> Line;
lineFunction(Line);
if (firstWord == "A") {
if (secondWord == "B") {
cout << "AB";
}
}
}

当我输入A B(带空格(时的预期结果应该是打印到屏幕上的字母 AB(没有空格,以测试它是如何工作的(,作为 if 语句的结果,但我正在尝试使输出可配置。当我尝试运行它时,我收到的错误是:

main.cpp: In function ‘int lineFunction(std::string)’:
main.cpp:12:20: error: declaration of ‘std::string line [199]’ shadows a parameter
string line[199];
^
main.cpp:13:30: error: request for member ‘length’ in ‘line’, which is of non-class type ‘std::string [199] {aka std::basic_string [199]}’
for (int i = 0; i < line.length(); i++) {
^~~~~~
main.cpp:14:31: error: no matching function for call to ‘isspace(std::string&)’
while (isspace(line[i]) == false) {
^
In file included from /usr/include/c++/6/cctype:42:0,
from main.cpp:2:
/usr/include/ctype.h:118:1: note: candidate: int isspace(int)
__exctype (isspace);
^
/usr/include/ctype.h:118:1: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘int’
In file included from /usr/include/c++/6/bits/basic_ios.h:37:0,
from /usr/include/c++/6/ios:44,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:3:
/usr/include/c++/6/bits/locale_facets.h:2565:5: note: candidate: template bool std::isspace(_CharT, const std::locale&)
isspace(_CharT __c, const locale& __loc)
^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2565:5: note:   template argument deduction/substitution failed:
main.cpp:14:31: note:   candidate expects 2 arguments, 1 provided
while (isspace(line[i]) == false) {
^
main.cpp:15:23: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
firstWord += line[i];
~~~~~~~~~~^~~~~~~~~~
main.cpp:17:20: error: no match for ‘operator+=’ (operand types are ‘std::string [99] {aka std::basic_string [99]}’ and ‘std::string {aka std::basic_string}’)
secondWord += line[i];
~~~~~~~~~~~^~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:24:22: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (firstWord == "A") {
^~~
main.cpp:25:27: error: comparison between distinct pointer types ‘std::string* {aka std::basic_string*}’ and ‘const char*’ lacks a cast [-fpermissive]
if (secondWord == "B") {

以下是修复错误的一些可能的解决方案。

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
// don't use 
// using namespace std;
// don't use global variables
// string Line;
// string firstWord[99]; you need strings, not arrays
// string secondWord[99]; 
// use references to return multiple values and no return value
// alternatively you could return a std::pair
void lineFunction(std::string line, std::string &firstWord, std::string &secondWord) {
// string line[199]; you don't need this variable
// line.length() return unsigned int so you should compare with unsigned int
// this whole loop makes no sense
// for (unsigned int i = 0; i < line.length(); i++) {
// this would result in a infinite loop
// while (isspace(line[i]) == false) {
// if (!std::isspace(line[i])) {
// firstWord += line[i];
// }
// secondWord += line[i];
// }
unsigned int i;
for (i = 0; i < line.length() && !std::isspace(line[i]); ++i) {
firstWord += line[i];
}
for (++i; i < line.length(); ++i) {
secondWord += line[i];
}
} 
int main() {
std::string line;
// std::cin only reads until first whitespace
// std::cin >> line;
std::getline(std::cin, line);
std::string firstWord;
std::string secondWord;
lineFunction(line, firstWord, secondWord);
if (firstWord == "A" && secondWord == "B") {
std::cout << "AB";
}
}