Output in a different format

Hello.
I'm pretty new to programming and I have some trouble figuring out why I have the wrong output on my division code and the output on my pythagoras theorem function.

On the division output problem, if I divide a odd number so the output should be a decimal result (eksample 8/3 =2,666), but I only get even numbers.

On the pythagoras theorem I have made a header file with the function for pythagoras theorem. When i link the header file to the main source file and run it. It comes up with a wired nummber like 001C12CB (I have heard about the format but don't remember what it is called)

I think it is because I use the wrong types, but i'm not sure.

By the way. This is the first time writing in this forum, so hello from Denmark :)


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
#include <math.h>
#include "pytagoros.h"
using namespace std;
/*declaring variabels*/
int a;
int b;
int numberProduct, numberSum, numberMinus;
float numberDivision;


int main()
{

	/*indput from user*/
	cout << "first nummber" << endl;
	cin >> a;
	cout << "Secound number" << endl;
	cin >> b;
	/*executeing mathmatical equations*/
	numberProduct = a * b;
	numberSum = a + b;
	numberMinus = a - b;
	numberDivision = a / b;
	/*Output found data*/
	cout << "the product is: " << numberProduct << endl;
	cout << "the sum is: " << numberSum << endl;
	cout << "the minus is: " << numberMinus << endl;
	cout << "the division is: " << numberDivision << endl;
	cout << "The hypotenus of the two values is " << pytagoros << endl; // execute of wrong result, by wrong format.
	cin.get();
}


pytagoros.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <math.h>
using namespace std;

int pytagoros()
{
	float a, b, c;

	cout << "Please type the value of a and b";
	cin >> a;
	cin >> b;

	c = a * a + b * b;
	c = sqrt(c);
	return c;
}


Edit: typo*
Last edited on
Hello johannes67890,

In "main"<math.h> is a C header file. The proper header, which you do not need for (+ - * /), is <cmath> for a C++ program.

In the other ".cpp" file that you call "pytagoros.h" you do need <cmath> for the " sqrt()" function.

Try to avoid using global variables like you have. They are more likely to cause a problem than to be helpful. They should be defined in "main" where they are used.

What you are calling "pytagoros.h" is a ".cpp" file not a header file and this should no be an include, but a separate ".cpp" file that is compiled separately and included at link time.

You should give "a" and "b" better names like you did with the other variables.

"double" is the preferred floating point type as it has better precision, i.e., more decimal places to store a number. Since some numbers like 0.3 are stored as "0.2999999999999999889" at least in my VS2017. When the output is 2 or 4 decimal places does it round up correctly.

Defining "a" and "b" as "int"s is fine for (+ - *), but for divide it is integer division so there is no decimal part to work with. then when you store the result in a "double" it only stores the whole number, e.g., (10.0000...). You could either define "a" and "b" as a "double" or type case one of the variables
(numberDivision = static_cast<double>(a) / b;). This will promote "b" to a "double" before the calculation.

That is what I can see for now. After I load the program and compile it I will let you know if I find anything else.

Andy
Hello johannes67890,

There is one little thing I missed the first time
cout << "The hypotenus of the two values is " << pytagoros << endl; // execute of wrong result, by wrong format.
Here "pytagoros" is being used as a variable and that variable was never defined. Which makes me wonder why I received no error or warning when I first compiled.

What you want is:
cout << "The hypotenus of the two values is " << pytagoros() << endl; // execute of wrong result, by wrong format. Here the function call is done before the "cout" statement is completed.

I took the liberty of changing things around. See what you think of this:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>  / <--- For std::fixed and std::setprecision.
#include <limits>

using namespace std;

/*declaring function prototypes*/
double pytagoros();

int main()
{
    int num1;
    int num2;
    int numberProduct, numberSum, numberMinus;
    double numberDivision;

    /*indput from user*/
    cout << "first nummber: ";
    cin >> num1;

    cout << "Secound number: ";
    cin >> num2;

    /*executeing mathmatical equations*/
    numberProduct = num1 * num2;
    numberSum = num1 + num2;
    numberMinus = num1 - num2;
    numberDivision = static_cast<double>(num1) / num2;  // <--- "num2" promotes to a "double" before the calculation.

    std::cout << std::fixed << std::setprecision(3);

    /*Output found data*/
    cout <<
        "\nthe product is: " << numberProduct <<
        "\nthe sum is: " << numberSum <<
        "\nthe minus is: " << numberMinus <<
        "\nthe division is: " << numberDivision <<
        "\nThe hypotenus of the two values is " << pytagoros() << endl; // execute of wrong result, by wrong format.

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}


pytagoros.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>

using namespace std;

double pytagoros()
{
    double a, b, c;

    cout << "Please type the value of a: ";
    cin >> a;
    cout << "Please type the value of b: ";
    cin >> b;

    c = a * a + b * b;

    c = sqrt(c);

    return c;
}

In the 2 files notice how I added some blank lines to make it more readable. This is your first goal and the first benefit is to you.

Andy

Edit:
Last edited on
Hello!
Thank you Andy for you feedback, I really appreciate it.
I figured it out and it is working perfectly!

Edit:
I figured it out before I saw your second post by this code:

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// made by Johannes
// Program: matematiske udøvning af ligninger
#include <iostream>
#include "Pytagoros.h"
using namespace std;


int main()
{
	/*declaring variabels*/
	int FirstValue;
	int SecoundValue;
	int numberProduct, numberSum, numberMinus;
	double numberDivision;

	/*indput from user*/
	cout << "føste tal" << endl;
	cin >> FirstValue;
	cout << "andet tal" << endl;
	cin >> SecoundValue;
	/*executing mathmatical fuctions*/
	numberProduct = FirstValue * SecoundValue;
	numberSum = FirstValue + SecoundValue;
	numberMinus = FirstValue - SecoundValue;
	numberDivision = static_cast<double>(FirstValue) / SecoundValue;
	/*Output from found data*/
	cout << "the product is: " << numberProduct << endl;
	cout << "the sum is: " << numberSum << endl;
	cout << "the minus is: " << numberMinus << endl;
	cout << "the division is: " << numberDivision << endl;
	pytagoros();
	cin.get();
}


pytagoros.h (Really wanted to try to link a header file with a .cpp file. that is why I keept trying with a header file, even if it isn't that optimum)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <math.h>
using namespace std;

int pytagoros()
{
	double FirstValue, SecoundValue, c;

	cout << "Please type the value of a: ";
	cin >> FirstValue;
	cout << "Please type the value of b: ";
	cin >> SecoundValue;

	c = FirstValue * FirstValue + SecoundValue * SecoundValue;
	c = sqrt(c);
	cout << "The hypotenus is: " << c << endl;
	return 0;
}


Just now saw your take of the code, and I found it really useful to see how you also can write the code :)
I also took your first advice with editing the names of the variables and declaring then within main.
Last edited on
Hello johannes67890,

You are welcome.


pytagoros.h (Really wanted to try to link a header file with a .cpp file. that is why I keept trying with a header file, even if it isn't that optimum)

You are on the right track, but is is not a good form to include what should be a ".cpp" file with an include.

Header files should contain things like "prototypes", "class" definitions and/or "struct" definitions. And the line using namespace std; should never be in a header file and best not to use it in a ".cpp" file. http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Your attempt is good. You just went about it the wrong way.

Using your program as an example this is something I use often:

proto.hpp or proto.h
1
2
3
4
5
6
#ifndef PROTO_HPP
#define PROTO_HPP

double pytagoros();

#endif // !PROTO_HPP 


And in "main" it would be:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <limits>

#include "proto.hpp" 

This makes more sense than what you are doing.

Andy
Topic archived. No new replies allowed.