### Program-Error after using <cmath>

hello,

im quite new to c++ and i have a big problem at the moment wich i really want to solve.. i am looking for the solution since 2 hours now and im getting quite frustrated .. here is the full code:

explenation for english people: zahl1 = number 1, geben sie eine zahl ein = tipe a number in, die wievielte ziffer soll auslgelesen werden? = which part of the number should be printed on the screen, die nte ziffer der zahl lautet = the n part of the number is;

 ``12345678910111213141516171819202122`` ``````#include #include #include using namespace std; double getZahl(const double zahl1,const double n,double x) { x = double(zahl1/pow(10.0,double(log10(double(zahl1))-n))) % 10; return (x); } int main(){ double zahl1,n, x; cout << "Geben Sie eine Zahl ein: " << endl; cin >> zahl1; cout << "Die wievielte Ziffer soll ausgelesen werden? " << endl; cin >> n; cout << "Die " << n << "-te Stelle der Zahl lautet: " << getZahl(zahl1, n, x) << endl; return 0; }``````

allways when i try to compile it it says the following:
 3 C:\Dev-Cpp\include\c++\3.4.2\cmath:52, from 8.cpp In file included from C:/Dev-Cpp/include/c++/3.4.2/cmath:52, from 8.cpp

.. the program is working if i do not include the cmath and just use normal operators like "*/:% etc." .. i really have no idea what i am doing wrong ..

I hope someone can help me solve my problem because i want to continue with the next program and i just need to find a solution to go on .. because im afraid this error will appear more often in the future and i dont want to make the same mistakes over and over again ..

looking forward to an answer & thank you anyways
valentin
Last edited on
What does the error message say? It looks like you have only posted the first half of the error message.
 Z_main.cpp:7:69: error: invalid operands of types 'double' and 'int' to binary ' operator%'

Simply cast the first operand to an int.

`x = int(zahl1/pow(10.0,double(log10(double(zahl1))-n))) % 10;`
Last edited on
true .. it also says the following:

`3 C:\Dev-Cpp\include\c++\3.4.2\cmath:52, from 8.cpp In file included from C:/Dev-Cpp/include/c++/3.4.2/cmath:52, from 8.cpp `

`3 C:\Users\Admin\Desktop\uebungheute\8\8.cpp from 8.cpp `

`90 C:\Dev-Cpp\include\math.h expected constructor, destructor, or type conversion before "extern" `

`90 C:\Dev-Cpp\include\math.h expected `,' or `;' before "extern" `

`3 C:\Users\Admin\Desktop\uebungheute\8\8.cpp In file included from 8.cpp `

i hope this also helps
That's odd, the MinGW compiler only gives me that one error. What compiler are you using to compile your code?
i am using "dev c++ version 4.9.9.2"

i tried to cast the first operand to an int but still the same errors ..! i also tried to include a downloaded math.h libary but the compiler dev++ couldnt find the path to the file .. but still it should actually work with the cmath file wich came with dev++ ..?

EDIT: it also tells me that funktions like "pow" are not declared in cmath .. but i checked now also again the cmath file in the include folder of dev++ and it last got changed when i used the program .. and i used cmath before sucessfully and without any problems
Last edited on
closed account (3qX21hU5)
I would highly recommend upgrading your compiler and IDE away from dev C++ which hasnt been updated in ages. Some ones that I recommend are CodeBlocks, and Visual Studio 2010 Express (This one can be a pain in the ass sometimes but has a few more features).
If you want to do modulus with floating point numbers you can use std::fmod.

It looks like there is something wrong with cmath. What happens if you include <cstdlib> instead of <stdlib.h>? If that doesn't work, I recommend that you update the compiler. Dev-C++ is very old.
Last edited on
ok i will change now my compiler .. i just stated studying informatics and thats why its so important for me to solve all the problems .. maybe its because of dev c++ .. i will reply again if this works out

thanks alot for your help till now!
Actually, there is a version of Dev-C++ that is being maintained.
http://sourceforge.net/projects/orwelldevcpp/
Last edited on
ok i registered now a version of

microsoft visual c++ 2010 express - i hope this compiler is now more professional

i casted now all doubles wich are not really necesarry to int but now it tells me the following:

 The variable 'x' is being used without being initialized.

i thought i initialized it in main .. sorry for all that questions and thank you to all wich allready helped me .. !

here the new code again:

 ``1234567891011121314151617181920212223`` ``````#include #include #include using namespace std; int getZahl(const int zahl1,const int n,int x) { x = int(zahl1/pow(10.0,double(log10(double(zahl1))-n))) % 10; return (x); } int main(){ int zahl1,n, x; cout << "Geben Sie eine Zahl ein: " << endl; cin >> zahl1; cout << "Die wievielte Ziffer soll ausgelesen werden? " << endl; cin >> n; cout << "Die " << n << "-te Stelle der Zahl lautet: " << getZahl(zahl1, n, x) << endl; return 0; }``````

is this not enough to initialized the variable x directly after int main() when i wrote `int zahl1,n,x;`?

valentin

UPDATE: " I added `x = 0; ` and now it is compiling without errors .. but still dont know why i had to give a number to x ... i mean it doesnt matter normally if x = 13245185418 or sth or does it ? it gets overwritten anyway and i also casted it as int so there shouldnt be problems normally .. ? please correct me .. or give me some information .. would be good "
Last edited on
You are not assigning a value to x in main anywhere. The x variable in getZahl is not the same variable as x in main. It doesn't look like you need x in main at all.
i didnt know that .. thank you very much .. now i removed x from the complete main function and declared (i hope thats the right word for it) it in the function getZahl1 .. i tried it with simple math and i get the correct return value and no errors anymore .. so i learned that i do not have to declare variables in the main function if they are only used in some other function like getZahl1 .. i hope thats right now

here the code again modified after the new knowledge:

 ``1234567891011121314151617181920212223`` ``````#include #include #include using namespace std; int getZahl(const int zahl1,const int n) { int x; x = int(zahl1/pow(10.0,double(log10(double(zahl1))-n))) % 10; return (x); } int main(){ int zahl1,n; cout << "Geben Sie eine Zahl ein: " << endl; cin >> zahl1; cout << "Die wievielte Ziffer soll ausgelesen werden? " << endl; cin >> n; cout << "Die " << n << "-te Stelle der Zahl lautet: " << getZahl(zahl1, n) << endl; return 0; }``````
Last edited on
Yes that's right. If you want to get rid of x altogether you can return the value of the expression in one line.
 ``123`` ``````int getZahl(const int zahl1,const int n) { return int(zahl1/pow(10.0,double(log10(double(zahl1))-n))) % 10; }``````

ok i fixed all my mistakes and now i am a bit more satisfied with my work, because i understand what i did and why i did it. i really have to thank you for all the time you invested in helping me!

here the final version wich does what i wanted it to do for all others wich have the same problem (1st you give in a number like "432941" and then you type in wich of the numbers you want to have .. like "3" would output the 9 .. because it counts from right to left) ..

 ``123456789101112131415161718192021`` ``````#include #include #include using namespace std; int getZahl(const int zahl1,const int n) { return (int (pow(10.0,((log10(double (zahl1)))+1)-n) )% 10); } int main(){ int zahl1,n; cout << "Geben Sie eine Zahl ein: " << endl; cin >> zahl1; cout << "Die wievielte Ziffer soll ausgelesen werden? " << endl; cin >> n; cout << "Die " << n << "-te Stelle der Zahl lautet: " << getZahl(zahl1, n) << endl; return 0; }``````
Topic archived. No new replies allowed.