Originally Posted by
kermit
@bbray,
Since you declared
d as a char type variable, you ought to read
this.
Thanks! I went back to the drawing board(mostly) and implemented the change you pointed out. It works!!
Here's my new program.
Code:
#include <stdio.h>
#include <ctype.h>
int main ()
{
int c, i, count, tenCheck, e;
char number[10];
char d;
d = '1';
while (d == '1')
{
/* Initialize array */
for (i = 0; i < 10; ++i)
{
number[i] = 0;
}
/* Get number */
i = 0;
count = 0;
printf("Enter a number between 1 and 10\n");
while ((c = getchar()) && c != EOF && c != '\n') // Clear input buffer in case of previous iteration
{
number[i] = c;
++count;
++i;
}
/* Check if 10 was chosen */
tenCheck = 0;
if ((count == 2) && (number[0] == '1') && (number[1] == '0'))
{
printf("10 divided by 25 is %.1f\n", 10.0/25.0);
++tenCheck;
}
if (tenCheck == 0)
{
/* Check for digit */
e = 0; // Initializes case if '0' is chosen
for (i = 0; i < count; ++i)
{
if ((count == 1) && (isdigit(number[0])))
{
switch (number[i])
{
case '4':
printf("4 + 10 is %d\n", 4+10);
break;
case '0':
printf("You entered \'0\'.\n");
printf("Try again.\n");
e = 1;
break;
default:
printf("You chose %c.\n", number[i]);
break;
}
}
else
printf("Not a digit\n");
}
}
/* Check if zero was entered, if so re-run program */
if (e == 1)
{
d = '1';
}
else
{
/* Prompt if user wants to run again or not */
c = 0;
printf("\nEnter 1 to continue, enter any other key to end.\n");
while ((c = getchar()) && c != EOF && c != '\n')
{
d = c;
}
}
}
}