tags:

views:

306

answers:

5
#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....

+5  A: 

You forgot the parentheses which are required for a function definition. Change that to:

int main()
{
    ...
}
R Samuel Klatchko
+13  A: 

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
}
GMan
+1 for "I don't hate you." :)
Bragboy
Dammn it, GMan, you really ought to know that `int x;` is a variable _definition_, not a _declaration_! (http://stackoverflow.com/questions/1410563/1410632#1410632) Fix that and I'll up-vote your answer.
sbi
Oh, and while `main()` is indeed the single one non-void function where we can omit the return statement, I'd rather you write it. It's just too confusing for a newbie.
sbi
@sbi: Ha, I do know that, my fingers do not. Also, I actually prefer to explicitly say `void`, but I also get poked fun at it a lot and thought I'd do it in the style most people want here. :) Lastly, I'm maxed for the day, so psha!
GMan
I liked "don't declare variables until you need them" better. Definitions are declarations, so either statement is "true", but "declaration" applies more widely. After all, it's about names being visible or not visible, so we are making a statement about declarations in the first place. For instance, now your statement does not apply to "int main(void) { extern int f; ... }` anymore because `f` is a non-defining declaration.
Johannes Schaub - litb
I knew there was a reason I always said it was a declaration. @sbi: **:(**
GMan
I still disagree, though. No cookie from me. `:)`
sbi
+4  A: 

You're missing parentheses after main.

Also, semicolons:

infile.close();
outfile.close();
WhirlWind
when i putted parentheses and then i get a massive errors
SkyBoxer
@user: That's because once the compiler can treat it as a function properly, you get the errors *in* the function.
GMan
+3  A: 

Change that to:

int main(int argc, char *argv[])
{
    ...
}
PiedPiper
+3  A: 

int main()

Welcome to Computer Sc. and Stackoverflow (SO).

Advice: there are already a lot of C++ questions here, so just search before asking a new question :)

N 1.1
Thank you for the warm welcome :)
SkyBoxer