I am trying to limit the user to a max value of +/- 32768 when entering in a value into the program . To accomplish this I have to use a “For Loop” , my issue is I am not using the loop correctly and can not see why it’s not working in the program. Thank you for the help.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int times (int a, unsigned int b)
{
int result;
result=0;
int shift;
for(shift = 1; b != 0 && shift <= 32768;) // Iterate the loop till b==0
{
if (shift,b&01) // Logical ANDing of the value of b with 01
result=result+a; // Update the result with the new value of a.
a<<=1; // Left shifting the value contained in 'a' by 1.
b>>=1; // Right shifting the value contained in 'b' by 1.
}
return result;
}
const int TEXT_SIZE = 100;
int main ()
{
printf("Hello, I'm a program that multplies integers. \n");
do {
int a;
int b;
char text[TEXT_SIZE];
printf("Please enter the first integer or 'Q' to quit: ");
fgets(text,TEXT_SIZE,stdin);
if ( toupper(text[0])== 'Q')
break;
a = atoi(text);
printf ("Please enter the second intger or 'Q' to quit: ");
fgets(text,TEXT_SIZE,stdin);
if ( toupper(text[0])== 'Q')
break;
b = atoi(text);
printf("The product for %d and %d is %d.\n\n", a,b,times(a,b));
}
while (1);
return(EXIT_SUCCESS);
getch();
}