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;
#include <iostream>
#include <stdlib.h>
#include <cmath>
usingnamespace std;
double getZahl(constdouble zahl1,constdouble 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
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
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.
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
#include <iostream>
#include <stdlib.h>
#include <cmath>
usingnamespace std;
int getZahl(constint zahl1,constint 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 "
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:
#include <iostream>
#include <stdlib.h>
#include <cmath>
usingnamespace std;
int getZahl(constint zahl1,constint 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;
}
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) ..