Thread: incrementing in if statements?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    incrementing in if statements?

    Hey can someone tell me a way to make the [b] in the if statement increment to 50?
    here is the code so far.

    Code:
      
      printf ("\nPlease enter the 4 digit registration of your car:");
      scanf ("%d", &rego_check);
      //for (b=0; b>50; b++)
      if (rego_check == rego_array [b])
      {
      printf ("\nThe registration you entered is already in the carpark please enter a new registration");
      carentry();
      }
      rego_array [i] = rego_check;
    Last edited by tomisme; 06-02-2008 at 05:35 PM. Reason: accidently pressed enter

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    This will increment to 49:
    Code:
    for (b=0; b<50; b++)
    (Which is what you want if your array is size 50)

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    8
    yeah i tried that above the if statement didnt seem to work. i cant think of any other place to put it that could work.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well you want to put opening and closing braces around the content you want to be included in the loop. Also, what is the variable 'i' on the last line. In the code you posted it would be undefined.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    8
    "i" is a global variable its a counter it increments by 1 everytime a new entry is made.

    Code:
      printf ("\nPlease enter the 4 digit registration of your car:");
      scanf ("%d", &rego_check);
      for (b=0; b>50; b++)
      {
      if (rego_check == rego_array [b])
      {
      printf ("\nThe registration you entered is already in the carpark please enter a new registration");
      carentry();
      }}
      rego_array [i] = rego_check;
    Still doesnt work.

    Im trying to ensure a user does not enter the same number twice.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Have you initialized your array first? The for loop in the code you just posted still is not fixed (b>50). Apart from that it seems as if your code should work. What does carentry(); do?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    8
    Got it going, thanks for the help.

    (carentry() was that function, the call is a reset for when the user enters somthing invalid)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. having problems with my if statements
    By bajanstar in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 01:39 PM
  5. Class Function Members and If Statements
    By Team Shadow V2 in forum C++ Programming
    Replies: 10
    Last Post: 02-03-2005, 03:00 PM