Thread: More Questions??

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    More Questions??

    I'm writing a program that will ask the user to input a decimal number, convert it to hex, do some simple math and compare the answer with the original input (converted to hex). 2 questions:

    1. Do I have to use a string to use hex?
    2. How do I compare 2 hex numbers or 2 strings. I setup the program such that hex numbers would be integers so of course it doesn't work. Any help would be great.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. No
    2a. The same way you compare any integer.
    2b. Use strcmp for comparing strings.

    hex is just a different way of displaying numbers. You don't have to use hex to do any math on an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    Thanks, 2a is way easier than what i was trying to do.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Keep getting RUNTIME ERROR CHECK FAILURE: stack around variable raid6 is corupt. Here is the peice of code:

    Code:
    /* First we must determine if we are using a RAID-6 device*/
    		
    		printf("Is this a RAID-6 device Y/N: ");
    		scanf ("%s", &raid6);
    		getchar();
    
    	/*if yes or no is not entered we get stuck in this loop*/
    
    		while (raid6 != 'Y' && raid6 != 'N' && raid6 != 'y' && raid6 != 'n')
    		{
    			printf ("Invalid input, please enter Y/N: ");
    			scanf ("%s", &raid6);
    			getchar();
    		}
    I always have problems with the scanf buffer yet I continue to use it. Is there a way to clear this in between steps?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If raid6 is a character variable, then your scanf is woefully broken, since %s is not for characters. If raid6 is a character array, then &raid6 is not what you want.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    1. sscanf might as well read too many characters, use "%c"
    2. In every case it will append a NUL at the end of the array. If you enter a letter, the NUL will go past the array, invalidating memory.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    76
    I got it to work, thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM