creating and calling function

while working on a module i came across a problem and could not figure out the solution to the problem. here is the question . program will get the input number and position from the user output the digit which is is at that position. for example if number = 256314 and position = 0 then output should be "2" if number = 1256985 and position = 3 then output should be "6" here is my code

#include <iostream>
#include <cmath>
using namespace std;
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{

{
int positiondigit = number/pow(10,(totaldigits - position));
positiondigit = positiondigit % 10;
return positiondigit;
}
}
int main()
{
int number = 0,position = 0,digit = 0;
cout << " Enter the number : ";
cin>>number;
cout<<" Enter the position : ";
cin>>position;
/*cout<<" Enter the digit : ";
cin>>digit;*/
int totaldigits = log(number);
int DigitAtPosition = returnDigitAtPosition(position,totaldigits,number);
cout<<DigitAtPosition;





}
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
#include <cmath>
using namespace std;
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{

{ 
int positiondigit = number/pow(10,(totaldigits - position));
positiondigit = positiondigit % 10;
return positiondigit;
}
} 
int main()
{
int number = 0,position = 0,digit = 0;
cout << " Enter the number : ";
cin>>number;
cout<<" Enter the position : ";
cin>>position;
/*cout<<" Enter the digit : ";
cin>>digit;*/
int totaldigits = log(number);
int DigitAtPosition = returnDigitAtPosition(position,totaldigits,number);
cout<<DigitAtPosition;





}
1
2
3
4
5
6
 
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{

{  // this right here is wrong it should be like this I don't know if it solve your problem but this is wrong. No time to look over the whole thing because im sleepy. close your bracket around your function.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

int DigitAtPosition(int &number, int &position)
{
    int positiondigit = int(number/pow(10,position)) % 10;
    return positiondigit;
}

int main()
{
    int number = 0, position = 0;
    
    std::cout << " Enter the number : ";
    std::cin >> number;
    
    std::cout << " Enter the position : ";
    std::cin >> position;
    
    std::cout << " Number is: " << DigitAtPosition( number, position);
    
    return 0;
}
Topic archived. No new replies allowed.