Thread: Problem regarding the coding error !

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Unhappy Problem regarding the coding error !

    hi , i'm using the Borland C++ builder for programming and i've been trying to resolve this issue as whenever i try to run it, it gives me an error.

    This code is supposed to do is if you press on “Accept” button, the entered number will be sent to the array named A1 and it should validate the number and the Accept button will be disabled after you accept five numbers.
    If you press on “Exc” button, the lower number in the array A1 will be found and it will be displayed in the memo.

    And it has one one editbox named Edit1 that holds the input number and one memo named Memo1 and two buttons named Accept and Exc. And i already have declared array of 5 integers initialized by zeros.



    Code:
    int num;
    int A1[5]= {0, 0, 0, 0, 0 };
    int i;
    int j;
    int count;
    
    
    void __fastcall TForm1::AcceptClick(TObject *Sender)
    {
               num=Edit1->Text.ToInt();   
           A1[i] = num;  
           count++; 
    
    
    if (count==5)  
            Accept->Enabled=false;   
    
    
    
    
    }
    //---------------------------------------------------------------------------
    
    
    void __fastcall TForm1::ExcClick(TObject *Sender)
    
    
    {
    min=A1[0] 
                for(int j=1; j<5; j++)  
                {
                       if(A1[j] < min)   
                min= A1[j]; 
                }
        Memo1->Lines->Add(AnsiString(num));  
    
    
    }
    Your help would be highly appreciated !

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have both i and count?

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    because all variables and objects need to declared and initialized.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    So what error is it giving? Or am I missing the question completely?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by naughtyaashiq View Post
    because all variables and objects need to declared and initialized.
    That ... what? That doesn't really answer the question of why do you have both i and count? Sometimes you use i, sometimes you use count, therefore neither of them have the values that they (both) should have. You need to pick one of them, and just use that one consistently.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    I'm sorry but what value and how? i mean if i just choose the just the "i".

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by naughtyaashiq View Post
    I'm sorry but what value and how? i mean if i just choose the just the "i".
    Well, look at where they're used:
    Code:
      A1[i] = num;  
           count++;
    You add one to count, but never to i, so you never move through the array.

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    pls advise how do i do it ?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by naughtyaashiq View Post
    pls advise how do i do it ?
    Pick one of "i" and "count" and use it consistently. Do not use both i and count. Pick one.

  10. #10
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by tabstop View Post
    Pick one of "i" and "count" and use it consistently. Do not use both i and count. Pick one.
    i is never init... god knows where that data is going... unless he is in debug then I am slightly learning towards the 0 element each time. He defines j as an int twice as well we aren't getting the whole story here. We can't even be entirely sure he doesn't assign count to i somewhere.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ~Kyo~ View Post
    i is never init... god knows where that data is going... unless he is in debug then I am slightly learning towards the 0 element each time. He defines j as an int twice as well we aren't getting the whole story here. We can't even be entirely sure he doesn't assign count to i somewhere.
    If i is truly global as shown, then it is auto initialized to 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with my coding problem
    By akerr95 in forum C Programming
    Replies: 7
    Last Post: 11-25-2012, 11:57 AM
  2. error in filter design coding
    By loga in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2008, 04:38 AM
  3. Help on coding, possible memory allocation error.
    By NiHL in forum C Programming
    Replies: 1
    Last Post: 11-08-2004, 09:14 AM
  4. Coding Error
    By polonyman in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2004, 02:17 AM
  5. Replies: 3
    Last Post: 03-27-2004, 12:15 PM