c++ Error

Asked 2 years ago, Updated 2 years ago, 67 views

I wrote this code, but I keep getting errors.

It's the code execution result at the bottom, but this error keeps popping up. Please, which part should I correct?

c++ error string

2022-09-22 20:15

2 Answers

#include <iostream>
#include <string>

#include "pch.h"

using namespace std;


string find_words(string str, int a) {

    int i;
    int idx = 1;

    for (i = 0; i < a; i++) {

        if (str.at(i) == ' ') {

            cout << endl;
            idx++;
        }

        else {

            cout << str[i];
        }
    }

    cout << endl << idx << endl;

    return 0;
}


int main(int argc, const char *argv[]) {

    string str;
    int num, f;

    while (1) {

        getline(cin, str);

        f = str.find("Q");

        if (f != -1) {

            break;
        }

        else {

            num = str.size();

            cout << find_words(str, num);
        }

        return 0;
    }
}

I don't think you need to pay much attention. If you're not going to release it, wouldn't it be okay to just get the input and output right?


2022-09-22 20:15

Why does the return type of the function find_words return 0 when it is a string? If you don't have anything to return, change it to void


2022-09-22 20:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.