Incorrect use of “For Loop”

This is a discussion on Incorrect use of “For Loop” within the C Programming forums, part of the General Programming Boards category; I am trying to limit the user to a max value of +/- 32768 when entering in a value into ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    Incorrect use of “For Loop”

    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(); 
            
            }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Why are you making this so complicated?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    Ya, I know but it's a lesson using shifting and bitwise operations. I am new to the C language so it's making my life that much more rough. But I love the language , and really what to lean as much as I can ! ok, thanks if you do see my error !

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Well, first a for loop is contructed with three parts like so:

    for(x = 0; x < 100; ++ x)

    in your loop it appears that the value of "shift" never changes and the break condition is really predicated on "b" being equal to zero. Therefore a while loop would be more appropriate:

    while(b != 0)

    To increment shift you should have

    for(shift = 1; b != 0 && shift <= 32768; ++ shift)
    Last edited by rmetcalf; 10-08-2008 at 08:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table outputting incorrect number
    By Paul Skinner in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:19 AM
  2. It is not incorrect to use void main
    By momo20016 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-22-2002, 09:17 AM
  3. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  4. 'The parameter is incorrect' - LoadImage
    By DarkAvenger in forum Windows Programming
    Replies: 1
    Last Post: 04-12-2002, 02:53 PM
  5. incorrect output
    By runtojesus in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2001, 03:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21