#include <iostream>
#include <cmath>
usingnamespace std;
int znam(int a){
int br;
do{
a/=10;
br++;
}while(a);
return br;
}
int pre(int a){
int uk=0;
for(int x=0;x<a;x++){
uk+=((x+1)*9*pow(10,x));
}
return uk;
}
int main()
{
int n;
cin>>n;
int zn=znam(n);
int br=(n-pow(10,(znam(n)-1))+1);
cout<<br<<endl;
return 0;
}
When it is written like this, program backs me some unknown and unlogical result. But when I remove: int zn=znam(n);
it shows me right result. I don't know why is this happening. Please help!
int znam(int a){
int br;
do{
a/=10;
br++;
}while(a);
return br;
}
In this function, your first use of br is br++, which means add 1 to the current value of br, but you haven't initialized br, so its current value is undefined. Consider initializing the variable br in this function before using it.
br is not initialized, it can be anything at line 7. It's only luck that it appears to be working on the first call to the function because the memory allocated for br happens to be zero (and by the next call, it sounds like it is given memory that has a random value in it already).