Thread: Hexadecimal calculator

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    Unhappy Hexadecimal calculator

    Hi guys, down below you will see my embarrassing attempt at coding lol

    Code:
    #include<stdio.h>intadd(p,q);intsub(p,q);
    
    intmain(){intn1,n2;chars[4];printf("Typeinanexpression:\n");scanf("%x%s%x",&n1,&s,&n2);
    if(s=="add")printf("%x",add(n1,n2));
    
    }
    intadd(p,q){floatc;c=p+q;returnc;}intsub(p,q){floatc;c=q-p;returnc;
    }
    


    It needs to work like this

    example: 41 add 49

    answer = 8a

    there is more to this but for right now I hope you guys can help me with this. Thank you guys



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    Perhaps you could post code from a decent editor into a decent browser.
    Your code is a format train wreck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    How old is your book? This code will not be compiled by any decent recent compiler

    Code:
    #include<stdio.h>
    
    int add(p,q);
    int sub(p,q);
    
    int main() {
        int n1,n2;
        char s[4];
        printf("Type in an expression:\n");
        scanf("%x%s%x",&n1,&s,&n2);
        if(s=="add")
            printf("%x",add(n1,n2));
    
    }
    int add(p,q) {
        float c;
        c=p+q;
        return c;
    }
    int sub(p,q) {
        float c;
        c=q-p;
        return c;
    }
    Code:
    Compiling src/test.cpp -> obj/test.o
    Makefile:52: recipe for target 'obj/test.o' failed
    src/test.cpp:3:9: error: ‘p’ was not declared in this scope
     int add(p,q);
             ^
    src/test.cpp:3:11: error: ‘q’ was not declared in this scope
     int add(p,q);
               ^
    src/test.cpp:3:12: error: expression list treated as compound expression in initializer [-fpermissive]
     int add(p,q);
                ^
    src/test.cpp:4:9: error: ‘p’ was not declared in this scope
     int sub(p,q);
             ^
    src/test.cpp:4:11: error: ‘q’ was not declared in this scope
     int sub(p,q);
               ^
    src/test.cpp:4:12: error: expression list treated as compound expression in initializer [-fpermissive]
     int sub(p,q);
                ^
    src/test.cpp: In function ‘int main()’:
    src/test.cpp:10:30: error: format ‘%x’ expects argument of type ‘unsigned int*’, but argument 2 has type ‘int*’ [-Werror=format=]
         scanf("%x%s%x",&n1,&s,&n2);
                                  ^
    src/test.cpp:10:30: error: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘char (*)[4]’ [-Werror=format=]
    src/test.cpp:10:30: error: format ‘%x’ expects argument of type ‘unsigned int*’, but argument 4 has type ‘int*’ [-Werror=format=]
    src/test.cpp:11:11: error: comparison with string literal results in unspecified behaviour [-Werror=address]
         if(s=="add")
               ^
    src/test.cpp:12:30: error: ‘add’ cannot be used as a function
             printf("%x",add(n1,n2));
                                  ^
    src/test.cpp: At global scope:
    src/test.cpp:15:9: error: redefinition of ‘int add’
     int add(p,q) {
             ^
    src/test.cpp:3:5: note: ‘int add’ previously defined here
     int add(p,q);
         ^
    src/test.cpp:15:9: error: ‘p’ was not declared in this scope
     int add(p,q) {
             ^
    src/test.cpp:15:11: error: ‘q’ was not declared in this scope
     int add(p,q) {
               ^
    src/test.cpp:20:9: error: redefinition of ‘int sub’
     int sub(p,q) {
             ^
    src/test.cpp:4:5: note: ‘int sub’ previously defined here
     int sub(p,q);
         ^
    src/test.cpp:20:9: error: ‘p’ was not declared in this scope
     int sub(p,q) {
             ^
    src/test.cpp:20:11: error: ‘q’ was not declared in this scope
     int sub(p,q) {
               ^
    cc1plus: all warnings being treated as errors
    make: *** [obj/test.o] Error 1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hexadecimal value
    By MasterAchilles in forum C Programming
    Replies: 5
    Last Post: 11-19-2008, 07:12 AM
  2. how to add two hexadecimal
    By bubblegum in forum C Programming
    Replies: 5
    Last Post: 03-12-2008, 10:10 AM
  3. hexadecimal
    By Domdom in forum C++ Programming
    Replies: 14
    Last Post: 02-13-2006, 10:55 AM
  4. hexadecimal
    By pierremk in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2002, 08:05 PM
  5. code for hexadecimal calculator
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 09:53 PM

Tags for this Thread