Reputation: 329
Here is my code, which is transforming hexadecimal number (given in string) to decimal:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int val(char);
int main() {
char s[100];
int sum = 0, mult = 1,i,len;
printf("Hexadecimal number:\n");
fflush(stdout);
fgets(s,100,stdin);
len = strlen(s);
for (i = len-2; i >= 0; i--) {
sum += val(s[i])*mult;
mult *= 16;
}
printf("%d\n",sum);
fflush(stdout);
return (EXIT_SUCCESS);
}
int val(char c) {
switch(c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': return 10;
case 'b': return 11;
case 'c': return 12;
case 'd': return 13;
case 'e': return 14;
case 'f': return 15;
default: return -1;
}
}
1. I add a breakpoint to int sum = 0, mult = 1,i,len;
2. I start the debugging
3. I use Step Into - the Variables are shown right
4. I've step into printf("Hexadecimal number:\n");
and fflush(stdout);
and it not shows up in the console.
5. The arrow is next to fgets(s,100,stdin);
I step into
6. I can't use steppers anymore, I assume this is the moment when I should give the string in stdin.
7. I try to give the string, nothing happens, I can give it again, nothing happens.
Other example:
1. I add a breakpoint to len = strlen(s);
(after fgets
)
2. I start debugging
3. Same result when stepping into fgets
(previous example)
The program is running normally if not debugging. I tried Cygwin and MinGW too, same result. Is this a bug or do I miss somehing? Should I report it?
Upvotes: 0
Views: 229
Reputation: 329
There is a workaround:
In the Project Properties
, under Run
select External Terminal
for the Console Type
. (This is needed for MinGW.)
With Cygwin the problem was that Netbeans used the 64bit install somehow with the selected 32bit bin directory. After I selected the 64bit Cygwin bin directory in the Options|C/C++|Build Tools
, the debugger was working with the Internal Terminal
options too.
Upvotes: 1
Reputation: 213955
do I miss somehing
Yes: when you step
into fgets
, your program is stopped (and thus can't accept any input).
What you want to do is next
over fgets
. That will block the program on input. Once you type in your number, the program will unblock, and stop again due to (temporary) breakpoint on the next line.
P.S. Your implementation of val
can be replaced with just a few lines:
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'f') return c - 'a' + 10;
return -1;
Upvotes: 0