i apolozise beforehand for the length of this post, i hope there will be someone who'll bear with me :(
here is my code, the one with scanf(). this one has no problems and works just fine. :)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int entry();// gets any base input and converts it to decimal
int main(void){
int ip_num,ip_base,op_num,op_base,i=0,cont_flag,acc[20];
char base_digits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
do{
ip_num=entry();
printf("choose the base to convert to:");
scanf("%d",& op_base);
puts("\nin main()\n\nexecuting for() loop . . .\n\n");
for(i=0;i<20 && ip_num!=0;i++) // converts to required o/p base
{
acc[i]=(ip_num % op_base);
ip_num=ip_num/op_base;
printf("index: %d \n",i);
if(ip_num==0)
break;
if(i==19) {
puts("array acc[] full");
break;
}
}//end of for()
printf("\nconverted number in base %d is:",op_base);
for(i;i>=0;i--)// prints the number
{
printf("%c",base_digits[acc[i]]);
}
printf("\n\n convert another number? press\n 1(yes)\n 0(no)\n:");
scanf("%d",& cont_flag);
}while(cont_flag==1);
}
int entry()
{
char sh[100],*stop;
int ip_num,ip_base;
printf("Enter a number: ");
scanf("%s",&sh);
printf("enter radix: ");
scanf("%d",&ip_base);
ip_num = strtol(sh,&stop,ip_base);
return(ip_num);
}
everything works fine. entry() has an strtol() function in its heart that converts any input to decimal, and then the main() takes that decimal and converts it to the desired base , as indicated by op_base.
THE ONLY PROBLEM :
its when i change entry() in the following manner:
int entry()
{
char sh[100],*stop;
int ip_num,ip_base;
printf("Enter a number: ");
gets(sh);
printf("enter radix: ");
scanf("%d",&ip_base);
ip_num = strtol(sh,&stop,ip_base);
return(ip_num);
}
the gets() takes the staring as it supposed it would in the 1st iteration, but from the 2nd iteration onwards it just skips by the whole printf("Enter a number: ");
and goes direct to printf("enter radix: ");
without taking any input for the number.
n.b by iteration, i mean a do-while loop in the form
do{
// run the entire code
printf("\n\n convert another number? press\n 1(yes)\n 0(no)\n:");
scanf("%d",& cont_flag);
}while(cont_flag==1);
any help to understanding this problem would be great! :)
thanks
somjit.