3rd c++ assignment. Getting weird numbers.

Hi there,

This is my 3rd assignment ever for C++ and I am having problems with it, I keep on getting some weird outputs for numbers. Here is the assignment, then I will show my code for it.

"
For this assignment you are going to write three functions:

AddTwoNumbers - takes two integers (a and b) as arguments and returns the integer sum of the two integers.

AddThreeNumbers - taks three integers as arguments (a, b and c). It calls AddTwoNumbers, passing in the first two arguments (a and b) and storing the result in an integer variable named "part1". It then calls AddTwoNumbers again, passing in part1 and c, and storing the result in an integer variable named "part2". It then returns the "part2"variable.

AddFourNumbers - takes four integers as arguments (a, b, c and d). It calls AddTwoNumbers, passing in the first two arguments (a and b) storing the result in an integer named "part1". It then adds the third and forth arguments (c and d) by passing them to AddTwoNumbers and stores the result in an integer variable named "part2". It then calls AddTwoNumbers, passing in part1 and part2, storing the result in an integer variable named "part3". It then returns "part3".

You will declare all of the functions in a header file, and include the header file in your main cpp file.

You will declare the functions in the following order:



_tmain

AddFourNumbers

AddThreeNumbers

AddTwoNumbers



In _tmain you will call each function (AddFourNumbers, AddThreeNumbers and AddTwoNumbers) three times each, printing the result in a format similar to the following:

AddTwoNumbers( 10, 20 ) = 30
AddThreeNumbers( 10, 20, 30 ) = 60
... etc



There should be nine output lines when you are done.

You must turn in both your header (.h) file and you cpp (.cpp) file.

Due before our next class.
"

Now here is my code:

Header File saved as myfirstheader.h in the correct directory:

int AddTwoNumbers(int a, int b)
{
int part1=(a+b);
return part1;
}
int AddThreeNumbers(int a, int b, int c)
{
int part1=AddTwoNumbers(a,b);
int part2=AddTwoNumbers(part1,c);
return part2;
}
int AddFourNumbers(int a, int b, int c, int d)
{
int part1=AddTwoNumbers(a,b);
int part2=AddTwoNumbers(c,d);
int part3=AddTwoNumbers(part1,part2);
return part3;
}
//

Now here is my actual project code:

#include "stdafx.h"
#include "myfirstheader.h"

int _tmain(int argc, _TCHAR* argv[])
{
AddFourNumbers(10,20,30,40);
AddThreeNumbers(10,20,30);
AddTwoNumbers(10,20);

printf("AddTwoNumbers( 10, 20 ) = %i\n", AddTwoNumbers);
printf("AddThreeNumbers( 10, 20, 30 ) = %i\n", AddThreeNumbers);
printf("AddFourNumbers( 10, 20, 30, 40 ) = %i\n", AddFourNumbers);

scanf("!");
return 0;
}

//
When I run the program I get this:

AddTwoNumbers( 10, 20 ) = 4264286
AddThreeNumbers( 10, 20, 30) = 4263976
AddFourNumbers( 10, 20, 30, 40 ) = 4264026

//

Why the weird numbers?


Thanks for trying to help!
Last edited on
Nevermind, I figured it out. The problem was:

AddFourNumbers(10,20,30,40);
AddThreeNumbers(10,20,30);
AddTwoNumbers(10,20);


I needed to declare the returned number as an integer and then call that integer in the printf command.
Topic archived. No new replies allowed.