PistolKid's C++

Started by deluxulous, 05-10-2011

0 Members and 1 Guest are viewing this topic.

deluxulous

so yeah. i'm beginning to learn C++. It's pretty fun so far, and it's gone very well. I suppose I'll use this thread to document my progress and post issues. I just ran into a problem.
(I'm learning forward declaratory statements by the way)

#include <iostream>

int DoMath(int, int, int, int) \\ Forward declaratory statement. Sets up a function before the program sees it in the main() function, and also before you actually define it

int main()
{
   using namespace std;
cout << "3 + 4 * 5 / 6 = " << DoMath(3 + 4 * 5 / 6) << endl; // Compiler reports "error C2660: 'DoMath' : function does not take 1 arguments"
   return 0;
}

int DoMath(int a, int b, int c, int d)
{
   return a + b * c / d;
}

Paintcheck

You need to declare your variables. When you call DoMath(3 + 4 * 5 / 6), the computer evaluates that as DoMath(6.333333333333) which is just 1 double in the argument. The computer is expecting 4 integers. 

You need to declare a = 3, b= 4, c = 5, d = 6. Then call it as DoMath(a, b, c, d) OR leave it as it is and call DoMath(3, 4, 5, 6). Either of those should fix your problem.

deluxulous

Yup, that was the problem. Wow, can't believe I missed that.




deluxulous

3 days of learning C++ and I have this:


did this for a quiz. the questions are:
1) Write a program that reads two numbers from the user, adds them together, and then outputs the answer. The program should use two functions: A function named ReadNumber() should be used to get an integer from the user, and a function named "WriteAnswer" should be used to output the answer. You do not need to write a function to do the adding.

2) Modify the program you wrote in #1 so that the function to read a number from the user and the function to output the answer are in a separate file called "io.cpp". Use a forward declaration to access them from your main() function, which should live in "main.cpp".

3) Modify the program you wrote in #2 so that it uses a header file to access the functions instead of forward declarations. Make sure your header file uses header guards.


yayyyy