Bad output -- possible input buffer problems

This is a discussion on Bad output -- possible input buffer problems within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm producing a piece of code that converts numbers to different bases. I want the program to loop ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Bad output -- possible input buffer problems

    Hello everyone,

    I'm producing a piece of code that converts numbers to different bases. I want the program to loop until someone types in zero which exits the program. I have it setup and working, however I think there's a problem with the output. Since I'm using scanf() multiple times it seems to be tagging a newline char to the other calls, therefore producing bad output, at least I think. Anyways here's the code.

    Code:
    #include <stdio.h>
    
    // functions
    void getNumberAndBase(void);
    void convertNumber(void);
    void displayConversion(void);
    void clear_line(void);
    
    // globals
    int gConvertedNumber[64], gBase, gDigit = 0;
    unsigned long long gNumber;
    
    int main(void) {
    	
    	int stop = 1;
    	char ch;
    	
    	while (stop == 1) {
    		//1. get the number and coversion base
    		getNumberAndBase();
    	
    		if (gNumber == 0)
    			stop = 0;
    		else {
    			//2. convert the number by gathering it's digits
    			convertNumber();
    			//3. display the number
    			displayConversion();
    		}
    	}
    	
    	return 0;
    }
    
    void getNumberAndBase(void) {
    
    	printf("Input number to convert: ");
    	scanf("%llu", &gNumber);
    	
    	clear_line();
    	
    	if (gNumber != 0) {
    		printf("Input conversion base: ");
    		scanf("%i", &gBase);
    		
    		clear_line();
    	
    		while (gBase < 2 || gBase > 16) {
    			printf("Error: no base, please input a proper base\n"); 
    			scanf("%i", &gBase);
    			clear_line();
    		}
    	}
    }
      
    void convertNumber(void) {
    
    	do {
    		gConvertedNumber[gDigit] = gNumber % gBase;
    		gDigit++;
    		gNumber /= gBase;
    	} while (gNumber != 0);
    
    }
    
    void displayConversion(void) {
    	
    	static const char numTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    												'A', 'B', 'C', 'D', 'E', 'F' };
    		
    	for (gDigit--; gDigit >= 0; gDigit--)
    		printf("%c", numTable[gConvertedNumber[gDigit]]);
    	
    	printf("\n");
    	
    }
    
    void clear_line(void) {
    	
    	char ch;
    	while ((ch = getchar()) != '\n' && ch != EOF);
    }
    And output, (input text bolded)
    Code:
    OUTPUT:
    Input number to convert: 10
    Input conversion base: 2
    1010
    Input number to convert: 10
    Input conversion base: 2
    101 <--- Incorrect
    Input number to convert: 0
    If there's anyone that can clarify this for me or point me to a resource that explains this, that would be helpful. Thank you.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Try resetting gDigit to 0 after a conversion. Perhaps in your display function.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are not resetting gDigit to zero after your loop, and the loop ends with gDigit == -1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank's for the help matsp and MacGyver, I could have probably spotted this with a debugger, but I currently don't have one setup right now.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jake123 View Post
    Thank's for the help matsp and MacGyver, I could have probably spotted this with a debugger, but I currently don't have one setup right now.
    Having a local debugger is probably a better solution than posting on this board - at least for those simple bugs.

    By the way, you wouldn't have had this problem if you used local variables instead of globals, most likely.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I didn't use a debugger. For projects like this, you don't actually need one. While they can be extremely helpful, I wouldn't get completely dependent upon it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    I didn't use a debugger. For projects like this, you don't actually need one. While they can be extremely helpful, I wouldn't get completely dependent upon it.
    Oh, sure, I didn't either [even if I took a bit longer than you to post my answer] - I just followed the logic of the code, and realized that there was no resetting of the gDigits variable - then I checked the for-loop and saw that it ends at -1.

    And of course, even if you do need to follow what happens in the code, a simple way that often works better than the debugger, is to add "printlines", e.g lines that do printf() to show the intermediate results of some operation, e.g. print the "number of digits found" at the end of convertNumber() - and when it comes up with 3 the second time, you should start worrying. Depending on your ability to guess, the number of steps necessary to get it to go wrong, it may be faster to get a printline to the right place than it is to step through the code until you spot the error. I often use both - together or separately. But I wouldn't want to be without the debugger.

    Oh, and one final point: calling a variable "stop", then setting it to 1, and doing "while (stop == 1)" is contrary to my logical sense. You should either call the variable "more" and use it like you do stop now, or keep the name stop, but use it with an initial value of 0 and then stop when the value is 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Indeed, I know you're well capable of handling this type of problem, and I didn't think you needed one. My only objection would be if someone (ie. the OP, for example) is addicted to a debugger to the point they can't follow their own code in a small project and see what is going wrong.

    And yes, printf() or some equivalent is definitely beneficial in this area.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    Indeed, I know you're well capable of handling this type of problem, and I didn't think you needed one. My only objection would be if someone (ie. the OP, for example) is addicted to a debugger to the point they can't follow their own code in a small project and see what is going wrong.
    I agree completely: There is no replacement for the ability to quickly look at a bit of code and say "It looks wrong" - which is something you can't be born with, you learn it by studying code, understanding it (and a bit of a nose for "smelling bad code" as someone said). If all else fails, step through each line of code with pen and paper, and write down each step.

    And I have debugged startup code with only a single LED to blink on or off to indicate if I got to some particular place or not [having a logic analyzer helps at this point, so you can see HOW MANY blinks you got out of the board before it got stuck - that way you can add several "LEDON" and "LEDOFF" at good places, rather than having to recompile the code each time you made it past the "LEDON" or "LEDOFF" state.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send output to keyboard buffer
    By DReynolds in forum C Programming
    Replies: 2
    Last Post: 06-06-2007, 03:44 PM
  2. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 03:46 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  4. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 02:17 PM
  5. Input problems with DJGPP?
    By Hardboy in forum C++ Programming
    Replies: 10
    Last Post: 03-13-2003, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21