0

It's be awhile since I touched C++, but I'm writing inside my main I have a function called "solution" and right now that line is giving me the error: "a function definition is not allowed here before '{'"

Afterwards I thought I was supposed to write my funciton definitions after my main(), but that led to another slue of errors.

Also, as my code stands, I get the error of "invalid arguments" when I call my function solution and pass it to my outfile.

I also get the error on the final '{' of "expected '{' at end of input."

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;


int main(int argc, char** argv) {
    ifstream infile("TEST.txt", std::ifstream::in);
    string line;
    vector<string> inputLines;

    if(infile.is_open()){
        while(getline(infile, line)){
            cout << line << '\n';
            inputLines.push_back(line);
        }
    }

    infile.close();






    ofstream outfile("output.txt", std::ofstream::out);
    for(unsigned int i = 1; i < inputLines.size(); i+= 3){
        int credit = inputLines[i];
        int numofItems = inputLines[i+1];
        int numofItemscopy = inputLines[i+1];

        vector<int> items;
        stringstream ssin(inputLines[i+2]);
        int x = 0;
        while(ssin.good() && x < numofItems){
            ssin >> items[x];
            ++x;
        }
        outfile << solution(credit,
                            numofItems,
                            numofItemscopy,
                            items.size());


        outfile << inputLines[i] << '\n';
    }
    outfile.close();
    return 0;
}


string solution(int cred, vector<int> original, vector<int> copy, int size){
        for(int i = 0; i < size; i++ ){
            for (int ii = 0; ii < size; ii++){
                if(original[i] + copy[ii] == cred){
                    return std::string(i) + std::string(ii);
                }
            }
        }
        return "";
    }

EDIT:

I put my solution function after my main, now I am getting the following errors:

On all three lines of :

        int credit = inputLines[i];
        int numofItems = inputLines[i+1];
        int numofItemscopy = inputLines[i+1];

I get the error: "cannot convert ‘std::basic_string’ to ‘int’ initialization"

Also when I call my "solution" function:

outfile << solution(credit,
                numofItems,
                numofItemscopy,
                items.size());

I get the error that "Solution was not declared in this scope."

Kyle H
  • 921
  • 1
  • 8
  • 20

2 Answers2

1

First of all, you need to declare your function before you use it in your main function. You can then define it after your main function if you so desire. The compiler goes from top to bottom, and it only knows about what it has seen up to that point. The compiler has not yet seen the solution function when you call it in main, so it does not know what to do.

string solution(int cred, vector<int> original, vector<int> copy, int size);

Your declaration should look like this. To declare a function you just take its header and instead of giving it a body with {}, you just end the line with a ; like follows.

int num = stoi("32");

As for parsing strings to integers, in C++11 you can do that simply as seen above. See more info on that here: https://stackoverflow.com/a/11354496/2749485

See below for how your code should now look:

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

string solution(int cred, vector<int> original, vector<int> copy, int size);

int main(int argc, char** argv) {
    ifstream infile("TEST.txt", std::ifstream::in);
    string line;
    vector<string> inputLines;

    if(infile.is_open()){
        while(getline(infile, line)){
            cout << line << '\n';
            inputLines.push_back(line);
        }
    }

    infile.close();

    ofstream outfile("output.txt", std::ofstream::out);
    for(unsigned int i = 1; i < inputLines.size(); i+= 3){
        int credit = stoi(inputLines[i]);
        int numofItems = stoi(inputLines[i+1]);
        int numofItemscopy = stoi(inputLines[i+1]);

        vector<int> items;
        stringstream ssin(inputLines[i+2]);
        int x = 0;
        while(ssin.good() && x < numofItems){
            ssin >> items[x];
            ++x;
        }
        outfile << solution(credit,
                            numofItems,
                            numofItemscopy,
                            items.size());


        outfile << inputLines[i] << '\n';
    }
    outfile.close();
    return 0;
}


string solution(int cred, vector<int> original, vector<int> copy, int size){
    for(int i = 0; i < size; i++ ){
        for (int ii = 0; ii < size; ii++){
            if(original[i] + copy[ii] == cred){
                return std::string(i) + std::string(ii);
            }
        }
    }
    return "";
}
Community
  • 1
  • 1
Geoffrey Tucker
  • 670
  • 1
  • 9
  • 18
  • What is the way to do this without using stoi and C++11? I'm currently having difficulties adding C++11 to my current project, and for future reference I would like to know how to do it. – Kyle H Mar 12 '14 at 05:28
  • Try including `#include ` and doing `int i = atoi(credit.c_str());` – Geoffrey Tucker Mar 12 '14 at 06:28
0

Function definitions should go before your main() if you want to have them in the same source file

  • Technically they go before `main()` regardless - `#include` just substitutes the `#include` line with the contents of the header, so declarations always show up before definitions. – cf- Mar 12 '14 at 04:46
  • `int credit = inputLines[i];`, the error message explains the exact problem. `inputLines[i]` is a string, and `credit` is an int, so this doesn't work. What are you trying to do? – M.M Mar 12 '14 at 04:47
  • The input is a txt file that contains just lines of ints. I'm trying to store that int at that line of inputLines to int credit. – Kyle H Mar 12 '14 at 04:48
  • Wrong. Function **declarations** should go before `main`. The linker will sort out the definitions. – MSalters Mar 12 '14 at 08:33