Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I am new to C programming, I need to create loop that will be able to handle

ID: 3624967 • Letter: H

Question

Hi I am new to C programming, I need to create loop that will be able to handle bad user input and print “bad output,” also user should be able to repeatedly enter data calling getchar until it sees a ' '.
User any input other than number would be bad input. And once user enter 0 loop will break. Here is a sample run, and below what I got for the code

Sample run

enter a number: 10

10.00 0x41200000

enter a number:97

97.00 0x42C20000

enter a number:118

118.00 0x42EC0000

enter a number: a

bad input

enter a number: v

bad input

enter a number: 0

0.00 0x00000000

code
#include <stdio.h>

int main(int argc, char *argv[])
{


float input;
float* pinput = &input; //float pointer to input


printf(" enter a number ");
scanf("%f", &input);

while ((input = getchar()) != EOF){
//printf(" %10d ",input);
//printf("0x%08X",input);

if ( input == 0){
printf(" %10.2f ",input);
// printf(" %10d ",HexNumber);
printf("0x%08X",*(unsigned int*)pinput);
break;
}
else{

printf(" %10.2f ",input);
// printf(" %10d ",HexNumber);
printf("0x%08X",*(unsigned int*)pinput);
}
printf(" enter a number ");
scanf("%f", &input);

}
return 0;

}

Explanation / Answer

// WORKING PERFECTLY NOW HAD FUN

// CHEERS.ENJOY   Modified Code Shown in REd Colour....

#include<stdio.h>
int main(int argc, char *argv[])
{
float input;
int ret;
float* pinput = &input; //float pointer to input
printf(" enter a number ");
ret = scanf("%f", &input);
while(1)
{
if(ret)
{     
        if(input == 0)
        {
        printf(" %10.2f ",input);
        printf("0x%08X",*(unsigned int*)pinput);
        break;
        }
        else
        {
        printf(" %10.2f ",input);
        printf("0x%08X",*(unsigned int*)pinput);
        }
}
else
{
printf(" Bad Input ");
fflush(stdin);
}
printf(" enter a number ");
ret = scanf("%f", &input);
}
return 0;
}