So I've got three files, Test.h, Test.cpp, mainTest.cpp. I'm trying to get more familiar with header files and in doing so I have included in my header a declaration of an addition function, a function to increment a letter by one, a function to find the difference between two numbers, and a simple void function that says it is a void function.
I am getting the following output: 9 + 0.000000 = -147121720
The letter g is now g.
The difference between 5 and 11 is -6
This function is void.
Press any key to continue . . .
I would like to get this as output: 9 + 4 = 13
The letter g is now h.
The difference between 5 and 11 is -6
This function is void.
Press any key to continue . . .
So the problem seems to lie in dAddition and cLetter. Any point in the right direction is greatly appreciated.
Test.h
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef TEST_H
#define TEST_H
int dAddition(int n, int f);
char cLetter(char l);
unsignedint nDifference(unsignedint xx, unsignedint yy);
void vNoArgs();
#endif //TEST_H
#include <stdio.h>
#include "Test.h"
int dAddition(int n, int f)
{
int d = n + f;
return d;
};
char cLetter(char l)
{
char k = l++;
return k;
};
unsignedint nDifference(unsignedint xx, unsignedint yy)
{
unsignedint zz = xx - yy;
return zz;
};
void vNoArgs()
{
printf("This function is void.\n\n");
};
mainTest.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <stdio.h>
#include "Test.h"
int main()
{
int x = 9;
int y = 4;
printf("%d + %f = %d\n\n", x, y, dAddition(x, y));
char l = 'g';
printf("The letter g is now %c.\n\n", l, cLetter(l));
int xx = 5;
int yy = 11;
printf("The difference between %d and %d is %d\n\n", xx, yy, nDifference(xx, yy));
vNoArgs();
}
Line 8 - %f means float, but you are passing int. That is your first problem.
Line 11 - you only have one format specifier - %c - but you are passing two parameters. The second parameter (cLetter(l)) will be ignored and only l will be output.
This is the primary problem with printf--it is not type safe. Had you used C++ I/O streams (ie, cout), this would not have happened. Just FYI, in case you aren't aware of their existence.