Question : Conversions

While loop not working. Please suggest

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include<stdio.h>
#define p printf
#define s scanf

int num;
void showbits(int n);
void octal(int n);
void hexadecimal(int n);
int main()
{
    int ch;
    p("Enter number");
    s("%d",&num);
    p("Enter your choice:\nPress 1 for binary conversion\nPress 2 for octal conversion\nPress 3 for hexadecimal conversion");
    s("%d",ch);
    while(ch==1||ch==2||ch==3)
    {
        switch(ch)
        {
            case 1:
            {
                showbits(num);
                break;
            }
            case 2:
            {
                octal(num);
                break;
            }
            case 3:
            {
                hexadecimal(num);
                break;
            }
            default:
            {
                p("\nInvalid Option");
                break;
            }
        }
        p("\nWhat do you want to do now?");
        s("%d",&ch);
    }
    return 0;
}

void showbits(int n)
{
    int i;
    unsigned char j, k, andmask;

    p("The binary conversion is ");
    for(i=7;i>=0;i--)
    {
        j=i;
        andmask=1<<j;
        k=n&andmask;
        k==0?p("0"):p("1");
    }
}

void octal(int n)
{
    p("The octal conversion is %o",n);
}

void hexadecimal(int n)
{
    p("The hexadecimal conversion is %x",n);
}
Do you want help in C or help in C++? This program appears to be entirely written in C.
main.c:15:12: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    s("%d",ch);
       ~~  ^~
You should always enable warnings when compiling.
Line 15 should pass the address of ch:
 
s("%d",&ch);


Topic archived. No new replies allowed.