Thread: Multiplication problem

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Lightbulb Multiplication problem

    My code is part of a calculator program. it computes
    4
    *
    4
    E
    and then gives the answer 16 which is correct but if i do more than 2 numbers, the answer is completely wrong giving crap. why does it work for 2 integers but anything more it doesnt? here is my code:


    #include <stdio.h>
    #define SIZE 100

    int main()
    {

    int a[SIZE];
    char b[SIZE];
    int i = 1;
    int k = 1;
    char c = 'a';
    int total;

    while (c != 'E' )
    {
    scanf("%d", &a[i]);
    fflush(stdin);
    scanf("%c", &b[i]);
    fflush(stdin);

    c = b[i];
    i++;
    }
    a[SIZE] = i - 1;
    b[SIZE] = i - 1;
    for(i=1;i <= b[SIZE]; i++)
    { if(b[i] == '*')
    { a[i]=a[i]*a[i+1];
    a[SIZE] -= 1;
    b[SIZE] -= 1;
    for(k=i+1;k<= a[SIZE]; i++)
    a[i]=a[i+1];
    for(k=i+1;k<= b[SIZE] ;i++)
    b[i]=b[i+1];
    }
    printf("the answer is: %d\n", a[i]);
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ug.

    First off, you should modify you loop. Since you already are tracking how many characters have been entered, using 'i', there is no need to check beyond that in your secondary for loops.

    Next, you just have to loop through your lists, and remove whatever you use. Like this:
    Code:
    for( x = 0; b[x]; x++ )
    {
       if( b[x] == '*' )
       {
          a[x] *= a[x+1];
          for( y=x+1; a[y]; y++ )
             a[y]=a[y+1];
       }
    }
    Ideally, you'd want to track the total number of 'operators' and the total number of 'numbers', and decrement the count each time one is used.

    Then you repeat the main loop until there are no more operators.

    Stacks and Queues anyone?

    Quzah.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    fflush(stdin) doesnt make sense
    from K&R
    "on an output stream fflush causes any buffered but unwritten data to be written , on an input stream the effect is undefined"

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > fflush(stdin) doesnt make sense

    I agree. fushing an input stream has no defined result. In MSVC++, it is supposed to work, because the MSVC++ will flush any stream type (or is supposed to anyway), but it is not ANSI. I've seen books use fflush(stdin), people state that their college teachers use it also.

    It's wrong, but people do it all the time and I usually preach at them about it. "NO! DO THAT AND YOU'LL BURN IN HELL!!"

    But noone listens

    Quzah.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    > people state that their college teachers use it also.
    Thats sad , teachers teaching a particular implementation and claiming they are teaching C , I have even heard of someone losing marks for using int main(void) instead of void main()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM