Нужна помощь в устранении пробелов в C ++ - PullRequest
0 голосов
/ 05 октября 2019

Мне нужна помощь в исправлении следующего кода, это детектор пробелов 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";
        }
    }
}

Ожидаемый результат при вводе AB должен состоять из букв 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") {

1 Ответ

0 голосов
/ 05 октября 2019

Вот несколько возможных решений для исправления ошибок.

#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";
    }
}
...