Thread: problems with loops

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    problems with loops

    I was trying to write a simple program to convert a number to its hexidecimal equivalent. Easy enough, I got that much. The problem comes in when I want it to loop, depending on the user's answer to the last question, either y (for yes, and to continue the loop) or anything else to end the program.

    Here's my coding so far:

    #include <stdio.h>
    main ()
    {
    char answer, number;
    answer= 'y';
    do{
    printf("\nEnter in the number you wish converted to hexidecimal: ");
    number = getchar();
    Printf("\nThe hexidecimal representation of %c is 0x%00x.", number, number);
    printf("\nDo you wish to enter in a new number? Press y for yes, any other key to exit.", answer);
    answer = getchar();
    }
    while (answer='y');

    return 0;
    }

    Any ideas as to what I'm doing wrong here? It's somewhere in the loop, I know that, because it works perfectly fine without the loop.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    while answer == 'y'

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    still not working

    still not working. It doesn't allow me to input the answer, instead throws me back to the DOS prompt.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The reason why your program is not working is that the getchar gets all characters from input, including the enter key. When you type a number followed by the enter key the second getchar will read the enter (= '\n') and stop the program. It's better to use the gets function to read a complete line:

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <ctype.h>
    
    main () 
    { 
    	char line[256];
    	int number;
    
    	do{ 
    		printf("Enter a number or 'x' to exit\n");
    		/* read the complete line */
    		gets(line);
    
    		/* check if first character is a digit */
    		if(isdigit(line[0]))
    		{
    			/* convert string-number to integer */
    			number = atoi(line);
    			printf("The hexidecimal representation of %d is: %x\n", number, number);
    		}
    		else if(line[0] != 'x')
    		{
    			printf("Invalid number\n");
    		}
    	} while(line[0] != 'x');
    
    
    	return 0; 
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The reason why your program is not working is that the getchar gets all characters from input, including the enter key. When you type a number followed by the enter key the second getchar will read the enter (= '\n') and stop the program. It's better to use the gets function to read a complete line:
    Actually, getchar only reads one character from the input stream. The first call to getchar reads properly but the second reads the newline or further characters that were left in the stream by the first call. This still has an unwanted effect similar to what you stated. And it's not better to use gets, period. gets is very unsafe and should be avoided like the plague, use fgets instead.

    I found a few problems with this code, not the least of which was that the number converted can only be single digit since getchar only reads one digit of a larger number. I changed this as well as fixed the newline problem. The output was also incorrect since you were printing characters and not integers, with everything fixed and the program working properly I ended up with this:
    Code:
    #include <stdio.h> 
    
    int main ( void ) 
    { 
      int answer, number; 
      answer = 'y'; 
      do{ 
        printf("\nEnter in the number you wish converted to hexadecimal: "); 
        scanf ( "%d%*c", &number );
        printf("\nThe hexadecimal representation of %d is 0x%00X.", number, number); 
        printf("\nDo you wish to enter in a new number? (y/n): "); 
        answer = getchar();
      } 
      while (answer == 'y'); 
      return 0; 
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Thank you ever so much Prelude

    I knew I could do it with a do-while loop, I just wasn't sure where I was going wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 10:04 PM
  2. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  4. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM