C++ question

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
It's visual C++ 5 code (from a book), and I'm using a VC++ 6 compiler. That could be causing the errors, I don't know. Here's my code:

#include <iostream.h>

double addtwo(int x, double y);

int main(void)
{
int two = 2;
double number = 0;


cout << &quot;Choose a number.&quot; << endl;
cin >> number;

cout << endl
<< &quot;Your number plus 2 = &quot;
<< addtwo(int two, double number); <-----All three errors

cout << endl;

return 0;
}

double addtwo(double x, int y)
{
y += x;
return y;
}

The errors I get are these: 1) Syntax Error: missing ')' before type 'int'
2) 'addtwo' function does not take 0 parameters
3) Syntax Error: ')'

Little help?


 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
First thing, when calling addtwo() don't include the variable types, just pass in the variables.

<< addtwo(int two, double number) //incorrect
<< addtwo(two, number); //correct

Also, addtwo() is declared to return a double, but you are returning y, which has been declared as an int.
 

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
Thanks. After I did that, it gave me weird errors that I didn't understand, but then I realized I had the double variable and a integer variable in the wrong spots in the function header. It works now. :)
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
additionally argument order also matters, if you declare routine foo(int x, double y), you should be passing integer type value followed by a double or in your case << addtwo(number, two);

 

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
Hehe. I'm suprised I actually got responces this quick in this forum. Usually my posts go unanswered or I have to bump it 3 times.

Thanks guys.
 

bigshooter

Platinum Member
Oct 12, 1999
2,157
0
71
In your function prototype at the beginning, aren't you just supposed to lsit variable type? instead of declaring it too? before the main function.