Advertisement

Help With Functions

Started by June 06, 2002 08:57 AM
10 comments, last by Kevinator 22 years, 6 months ago
I''m very new to C++, and I''m having some problems that I can''t solve by looking in my text book. I can''t figure out what''s wrong with my simple program. Here''s the source code:
  int doSomething(int num1, int num2); //function prototypes


int main(int argc, char* argv[])
{
  int num1, num2;

  cout << "Enter number 1: ";
  cin >> num1;
  cout << "Enter number 2: ";
  cin >> num2;
  cout << endl;

  int ans = doSomething(num1, num2);         //assigns what

                                             //the function returns

  cout << "The answer is: " << ans << ".";   //to ans

  getch();

  return 0;
}

int doSomething(int num1, int num2)
{
  return num1(sqrt(num2))+(num1+num2);
}  
When I try to compile this, my compiler gives me the error "[C++ Error] Learning1.cpp(34): E2314 Call of nonfunction." Any help here? I''ve never come across that error before and I don''t know how to fix it.. Thanks! -Kevinator
The compiler thinks:

''return num1(sqrt(num2))+(num1+num2);''

is a function call. Remove the ''num1'' bit and it should ''do something''
<a href="http://www.purplenose.com>purplenose.com
Advertisement
I tried it out on mine and all it says is that getch() and sqrt() are undecalred. What are you using to compile?
Your problem is this line:

return num1(sqrt(num2))+(num1+num2); 


You are trying to use num1 as if it were a function, which it clearly isn''t. You probably mean

return num1*(sqrt(num2))+(num1+num2); 
In:
return num1(sqrt(num2))+(num1+num2); 

It thinks you''re calling a function called num1();... Look at how you have it set up. You need some sort of operator between num1 and the opening paren ''(''. That''ll fix it. ex:
return num1*(sqrt(num2))+(num1+num2); 

and you don really need the parens and could just use:
return num1*sqrt(num2)+num1+num2; 


Gamer-Insight.com
lol

Guess I typed too slowly.

Gamer-Insight.com
Advertisement
Hmm, I added a ''*'' after the first num1 and that did the trick. BTW I''m using Borland C++Builder..
Wow, I go to type my reply and 5 other people reply in the mean-time. Thanks for all the help.
BTW, to the AP: include < math.h >, this is where sqrt () is defined, and include < stdio.h > for getch ().

[edited by - Melraidin on June 6, 2002 10:34:44 AM]
Actually it is conio.h you should include to use getch().

This topic is closed to new replies.

Advertisement