Reputation: 921
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."
Upvotes: 0
Views: 68
Reputation: 670
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 "";
}
Upvotes: 1
Reputation: 1
Function definitions should go before your main() if you want to have them in the same source file
Upvotes: 0