#include <iostream>
#include <fstream>
using namespace std;
int main
{
int num1, num2;
ifstream infile;
ostream outfile;
infile.open("input.dat");
outfile.open("output.dat");
infile >> num 1 >> num 2;
outfile << "Sum = " << num1 + num2 << endl;
infile.close()
outfile.close()
return 0;
}
This is what I did and when I compile it, I got this error that said
error C2470: 'main' : looks like a function definition, but there is no
parameter list; skipping apparent body
Please don't hate me :( I am new at this computer science....
I don't hate you. ♥
Functions have parameters, such as:
void foo(/* parameters here */);
If your function takes none you don't omit the list, but leave it empty or put void
:
int main()
// or:
int main(void)
Which you use is up to you. I prefer explicitly stating void
.
Note, there are other variants you'll encounter. This is the second most common variant:
int main(int argc, char *argv[])
It gives you a count of the arguments and what they were. Such as:
myExe andAOne andATwo andIDontHateYou
You would be able to access those arguments. There can be more, but that should cover it for now. Don't worry about that stuff till later. <3
Concerning your code:
int main(void) // no parameters
{
int num1, num2;
ifstream infile;
ostream outfile;
infile.open("input.dat");
outfile.open("output.dat");
infile >> num1 >> num2; // no spaces in you variable names
outfile << "Sum = " << num1 + num2 << endl;
infile.close(); // missing semicolon
outfile.close(); // missing semicolon
return 0; // fun fact: optional. In C++, return 0 in main is implicit
}
That should get you started.
The rest of this may not make sense, and that's okay. I'm merely including it for completeness. If it doesn't make sense, ignore it for now:
int main(void)
{
ifstream infile("input.dat"); // use the constructor to open it
ostream outfile("output.dat");
int num1, num2; // don't declare variables until you need them
infile >> num1 >> num2;
outfile << "Sum = " << num1 + num2 << endl;
// closing is done automatically in the destructor of the fstream
}
You forgot the parentheses which are required for a function definition. Change that to:
int main()
{
...
}
You're missing parentheses after main.
Also, semicolons:
infile.close();
outfile.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With