In: Computer Science
#include
#include
#include
int main(void) {
int feof(FILE *stdin);
int i, num;
int binary[10];
char input[10];
printf("Starting the CPSC 1011 Decimal to Binary
Converter!\n");
while(1) {
i=0;
printf("\nPlease enter a positive whole number (or EOF
to quit): ");
scanf("%s", input); // user inputs value as a string
for separate values
if(strcmp(input,"")==0) {
printf("\n\tThank you for using the
CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");
return(0);
}
num=atoi(input);
if (num<=0) {
printf("\n\tSorry, that was not a positive whole
number.\n"); // default when num is not positive
}
else {
int temp = num;
while (num>0) {
binary[i] = num % 2; // stores
remainder
num = num / 2; //finds base
i++;
}
printf("\n\t%d (base-10) is equivalent to ",
temp);
for(int j=i-1; j>=0; j--) {
printf("%d", binary[j]); // prints
out binary
}
printf(" (base-2)!\n");
}
}
return(0);
}
ok so i can't use strcmp because it creates a sigpipe error
and i was just wondering how i could implement EOF (crtl+d) to
trigger that end output instead of the strcmp
Deserve thumbs up :)
#include <stdio.h>
int main(void) {
int feof(FILE *stdin);
int i, num;
int binary[10];
char input[10];
int flag = 0;
printf("Starting the CPSC 1011 Decimal to Binary
Converter!\n");
while(1) {
i=0;
printf("\nPlease enter a positive whole number (or EOF to quit):
");
flag = scanf("%s", input); // user inputs value as a string for
separate values
if(flag == EOF) {
printf("\n\tThank you for using the CPSC 1011 Decimal to Binary
Generator.\nGoodbye!\n\n");
return(0);
}
num=atoi(input);
if (num<=0) {
printf("\n\tSorry, that was not a positive whole number.\n"); //
default when num is not positive
}
else {
int temp = num;
while (num>0) {
binary[i] = num % 2; // stores remainder
num = num / 2; //finds base
i++;
}
printf("\n\t%d (base-10) is equivalent to ", temp);
for(int j=i-1; j>=0; j--) {
printf("%d", binary[j]); // prints out binary
}
printf(" (base-2)!\n");
}
}
return(0);
}