Thread: need help with hex adder.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    need help with hex adder.

    Hi, just having trouble with my hex adder.

    It will have to do several things first of which is to add two hex numbers together.
    It is read in with scanf as you will see below then I am going to use several if statements to achive the different functions of my adder.

    However i have run into a problem or two.

    First of all when i read my hex numbers in with scanf.. should i declare these as integers or a char array?
    Secondly why am i getting this error :
    C:\DOCUME~1\nlpa58\Desktop\ass2: In function `int main()':
    C:\DOCUME~1\nlpa58\Desktop\ass2:12: warning: format argument is not a pointer (arg 2)
    C:\DOCUME~1\nlpa58\Desktop\ass2:15:12: warning: multi-character character constant
    C:\DOCUME~1\nlpa58\Desktop\ass2:15: error: incompatible types in assignment of `int' to `char[80]'
    Failure

    here is my code at the moment im only trying to get the add function to work so that i know im reading in my hex numbers right etc..

    Code:
    #include <stdio.h>
    
    int num1;
    int num2;
    char opp[80];
    int result;
    
    int main() {
    	
    	//prompt
    	printf ("Type in an expression: ");
    	scanf ("%x %s %x", &num1, opp , &num2);
    
    	//if loops for the bitwise operators
    	if (opp = 'add'){
    		result = num1 + num2;
    		}
    
    		
    		//test
    		printf ("%x", result);
    		
    }
    any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    if (opp = 'add'){
    A number of things wrong with this statement in particular.
    1. You cannot compare strings like this in C, you need to use strcmp.
    2. You are trying to assign 'add' to opp. The equality operator is ==.
    3. 'add' is not a string anyway, but a multi-character character constant (as stated in the warning).

    It should be something like:
    Code:
    if (0 == strcmp(opp, "add")) {
    I can't speak to the scanf because to be honest I never really use it.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    god!

    thanks very much!
    Can you just tell me why you used the 0 == strcmp..
    what is the 0 for, sorry i would just like to make sure i understand it completely
    thanks again!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by webznz View Post
    thanks very much!
    Can you just tell me why you used the 0 == strcmp..
    what is the 0 for, sorry i would just like to make sure i understand it completely
    thanks again!
    First thing - read the reference http://www.cppreference.com/stdstring/strcmp.html
    Only after that ask question is you do not undestand something in the description of the function
    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

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    thanks VAR, i have the beginning with C book by Ron House.. If has lots of strcmp stuff but nothing refering to the (0 == .. thanks for the link will read now.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    Right.. i think i get it.. the if statment will be true if strcmp of the opp variable is "add" then strcmp == 0 and will execute the if statment that it applies too! ...
    thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM