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 #include #include int main() { int i=0,temp,binary[i]; char number[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); printf("Please enter a postive whole number (or EOF to quit): "); scanf("%s",number); 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(temp<=0) { printf("Sorry, that was not a postive whole number."); printf("\n"); } else { int i=0; //int j; int n = temp; while(temp>0) { 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("\nPlease enter a postive whole number (or EOF to quit): "); scanf("%s",number); temp = atoi(number); }while(strcmp(number,"EOF")); printf("\nThank you for using the CPSC 1011 Decimal to Binary Generator. Goodbye!\n"); return(0); } Here is a picture of how the code and output should look and work:
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<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
int i=0,temp,binary[i];
char number[10];
printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");
printf("Please enter a postive whole number (or EOF to quit): ");
scanf("%s",number);
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(temp<=0)
{
printf("Sorry, that was not a postive whole number.");
printf("\n");
}
else
{
int i=0;
//int j;
int n = temp;
while(temp>0)
{
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("\nPlease enter a postive whole number (or EOF to quit): ");
scanf("%s",number);
temp = atoi(number);
}while(strcmp(number,"EOF"));
printf("\nThank you for using the CPSC 1011 Decimal to Binary Generator. Goodbye!\n");
return(0);
}
Here is a picture of how the code and output should look and work:
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images