Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include int main() { int i=0,temp,binary[i]; char number[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter! "); 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(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:

image text in transcribed

./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): Thank you for using the CPSC 1011 Decimal to Binary Generator. Goodbye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions