Question
I think my code has an infinite loop and I need help fixing so it operates properly in C programming. This is my code: #include
I think my code has an infinite loop and I need help fixing so it operates properly in C programming. This is my code:
#include
do { int flag=0; int length; length=strlen(number); //checks for the string if it contains '.' followed by non zero value while(length--) { if(number[length]=='.'&&number[length+1]!='0') { flag=1; break; } } if(flag) printf("Sorry, that was not a postive whole number."); else { temp = atoi(number);//convert string to integer value if(temp0) { binary[i]=temp%2; temp=temp/2; i++; } printf(" %d (base-10) is equivalent to ",n); for(int j=i-1; j>=0; j--) { printf("%d",binary[j]);
} printf(" (base-2)!"); } } printf(" Please enter a postive whole number (or EOF to quit): "); scanf("%s",number); temp = atoi(number); }while(strcmp(number,"EOF"));
printf(" Thank you for using the CPSC 1011 Decimal to Binary Generator. Goodbye! "); return(0); }
Here is a picture of how the code and output should look and work:
./a.out Starting the CPSC 1011 Decimal to Binary Converter! Please enter a positive whole number (or EOF to quit): 42 42 (base-10) is equivalent to 101010 (base-2)! Please enter a positive whole number (or EOF to quit): -2 Sorry, that was not a positive whole number. Please enter a positive whole number (or EOF to quit): 1.1 Sorry, that was not a positive whole number. Please enter a positive whole number (or EOF to quit): 11.0 11 (base-10) is equivalent to 1011 (base-2)! Please enter a positive whole number (or EOF to quit):Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started