Hello i just started c++, and i have a small problem with compiling. The book im reading just told me to compile the following but line 4 has an error/warning.(im using devc++)

#include <iostream>
int Add (int x, int y)
{
    cout<< "In Add(), received " << x<<  "an d" <<y<< "\n";
    return (x+y);
}

int main()
{
    cout<<"Im in main()!\n";
    int a, b, c;
    cout<<"Enter two numbers:";
    cin<< a;
    cin<< b;
    cout<<\nCalling Add()\n";
    c=Add(a,b);
    cout<<"\nBackin main().\n";
    cout<<"\nExiting....n\n";
    reuturn 0;
}
Re: Simple problem 80 80

You're using new style C++, so you need using namespace std; immediately after the #include line.

Another way would have been to write std::cout<< "In Add(), received " << x<< "an d" <<y<< "\n"; but that can get rather verbose.

> reuturn 0;
Is this copy/pasted from your IDE, or did you just type this in from memory? The latter is just a low value post as it is no longer possible to be sure of anything you're saying.

In future, mention actual error messages (copy/paste from the IDE), as this will almost certainly help in more complex situations.

commented: Good ~SpS +3
Re: Simple problem 80 80

ellow i,m also a newbie n programming using the same language you're using (C++). By the way try this...

you define your function int Add(int x,int y), but you didn't create its prototype function. maybe this is the problem. try to declare the prototype first before defining it by writing int Add(int x,int y);

I suggest to place this cout<< "Total:" << c;
before cout << exiting... and after cout << back main

what book are you using?

Re: Simple problem 80 80

revenge2: In addition to Salem's corrections please note that the << operator is for ostreams like cout and >> is for istreams like cin. Unfortunately you are calling << on both cout and cin.

mypopope: The compiler/linker need to see either a function declaration (prototype) or a function definition before the funciton is called. Defining the entire function before main() as done by revenge2 is acceptable, as is declaring Add() before main() with a prototype and defining Add() after main() as you recommend.

Re: Simple problem 80 80

well i am also new to c++ sever things that i can see that will not lead to a sucsesful compilation are :

1. you must include "using namespace std;"

2. u wrote: "cout<<\nCalling Add()\n";"
but it should be "cout<<"\nCalling Add()\n";"

3.also when using cin you have to put
cin >>something;

4. you misspeeled return

and just an advice if i were u i would combine the two fuctions from my perspective it would make things much easier for me.

Re: Simple problem 80 80

and just an advice if i were u i would combine the two fuctions from my perspective it would make things much easier for me.

Don't do that. It is much better programming practice to separate functions where possible. Imagine if you had to call

int Add (int x, int y)

ten times with different variables for parameters? Or a hundred times? It is much easier to have

c=Add(a,b);

a hundred times than the details of the function.

Just my 2 cents worth...
darkagn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.