problem with visual studion vs using batch file to compile and run

Discussion in 'C Programming' started by myjunkcontainer@hotmail.com, Oct 8, 2007.

  1. Guest

    To whom ever might be able to help me:

    When I run the below code it returns the value I expect i.e. "arrays
    are equal" if true and "arrays are not equal" if false as long as I
    run it in Visual Studio. However, if i create a batch file, compile
    with cl.exe and run, then I only get arrays are not equal - whether
    they are or not. I do not understand why there is a difference. Visual
    studio uses cl.exe. I have tried this using the function as an int or
    as a bool and including stdbool.h. ----Please help.

    /* array equality test.
    Written by: clint
    Date: 10-7-7
    */
    #include <stdio.h>
    //#include "stdbool.h"
    // this file must be included in order for program to work.
    //#include <stdbool.h>
    // or this file can be added to the include directory



    // Function Declarations

    int comparearrays (int arrayA[], int arrayB[]);


    int main (void)
    {
    // Local Declarations

    //int comparerA;
    //int comparerB;
    int falsifier;
    int arrayA[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int arrayB[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Statements

    printf("Compare ArrayA with ArrayB\n");

    //testing code before building function --CS
    //for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    comparerB++)
    //{
    // if (arrayA[comparerA] == arrayB[comparerB])
    // {
    // falsifier = 1;
    // }
    // else
    // {
    // falsifier = 0;
    // break;
    // }
    //}
    //comparearrays(arrayA, arrayB);

    falsifier = comparearrays(arrayA, arrayB);
    if (falsifier == 0)
    {
    printf("arrays are not equal\n");
    }

    else if (falsifier == 1)
    {
    printf("arrays are equal\n");
    }

    return 0;
    } // main

    /* ==================== comparearrays ====================
    this function copares two arrays and returns a boolean value
    where the arrays are equal or not */

    int comparearrays (int arrayA[], int arrayB[])
    {
    int falsifier;
    int comparerA;
    int comparerB;

    for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    comparerB++)
    {
    if (arrayA[comparerA] == arrayB[comparerB])
    {
    falsifier = 1;
    }
    else
    {
    falsifier = 0;
    break;
    }
    }

    return falsifier == 1;
    }
    /* Results:
    returns boolean value and states if arrays are equal or not equal
    */
     
    , Oct 8, 2007
    #1
    1. Advertising

  2. Guest

    > int comparearrays (int arrayA[], int arrayB[])
    > {


    > for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    > comparerB++)

    --

    array overflow

    comparerA < 11 ought to comparerA < 10 ?
     
    , Oct 8, 2007
    #2
    1. Advertising

  3. Clinto Guest

    On Oct 7, 10:55 pm, wrote:
    > > int comparearrays (int arrayA[], int arrayB[])
    > > {
    > > for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    > > comparerB++)

    >
    > --
    >
    > array overflow
    >
    > comparerA < 11 ought to comparerA < 10 ?


    That was it. Thanks a million.....it was so simple i overlooked it.
     
    Clinto, Oct 8, 2007
    #3
  4. Army1987 Guest

    On Sun, 07 Oct 2007 20:19:36 -0700, myjunkcontainer wrote:

    > To whom ever might be able to help me:
    >
    > When I run the below code it returns the value I expect i.e. "arrays
    > are equal" if true and "arrays are not equal" if false as long as I
    > run it in Visual Studio. However, if i create a batch file, compile
    > with cl.exe and run, then I only get arrays are not equal - whether
    > they are or not. I do not understand why there is a difference. Visual
    > studio uses cl.exe. I have tried this using the function as an int or
    > as a bool and including stdbool.h. ----Please help.
    >
    > /* array equality test.
    > Written by: clint
    > Date: 10-7-7
    > */
    > #include <stdio.h>
    > //#include "stdbool.h"
    > // this file must be included in order for program to work.

    You can simplily use int, or enum { false, true }, for boolean
    values if you don't have stdbool.h.
    > //#include <stdbool.h>
    > // or this file can be added to the include directory

    While writing stdbool.h is trivial, it is not in general a good
    idea to add your own headers to the implementation. If it doesn't
    provide them, there is a reason.
    >
    >
    > // Function Declarations
    >
    > int comparearrays (int arrayA[], int arrayB[]);
    >
    >
    > int main (void)
    > {
    > // Local Declarations

    Lose useless comments.
    > //int comparerA;
    > //int comparerB;
    > int falsifier;
    > int arrayA[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    > int arrayB[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    >
    > // Statements
    >
    > printf("Compare ArrayA with ArrayB\n");

    Usually it is better to comment out big blocks with
    #if 0
    ....
    #endif
    >
    > //testing code before building function --CS
    > //for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    > comparerB++)

    Avoid using line comments on the Usenet. Apart the fact that they
    were added in C99 and MSVC doesn't support it (odds are you're
    compiling the code as C++), they break if the line is too long
    and get wrapped. If I copied and pasted the code, I would find
    that `comparerB++)` above isn't commented out.
    > //{
    > // if (arrayA[comparerA] == arrayB[comparerB])
    > // {
    > // falsifier = 1;
    > // }
    > // else
    > // {
    > // falsifier = 0;
    > // break;
    > // }
    > //}
    > //comparearrays(arrayA, arrayB);
    >
    > falsifier = comparearrays(arrayA, arrayB);
    > if (falsifier == 0)
    > {
    > printf("arrays are not equal\n");
    > }
    >
    > else if (falsifier == 1)

    What else could it equal?
    What was wrong with
    if (falsifier) {
    printf("arrays are equal\n");
    } else {
    printf("arrays are not equal\n");
    }
    ?
    > {
    > printf("arrays are equal\n");
    > }
    >
    > return 0;
    > } // main
    >
    > /* ==================== comparearrays ====================
    > this function copares two arrays and returns a boolean value
    > where the arrays are equal or not */


    Please put */ in a place where one can see it.
    > int comparearrays (int arrayA[], int arrayB[])
    > {
    > int falsifier;
    > int comparerA;
    > int comparerB;
    >
    > for (comparerA = 0, comparerB = 0; comparerA < 11; comparerA++,
    > comparerB++)

    if comparerA and comparerB are bound to be always equal, why don't
    use only one variable? BTW, don't hardcode sizes such as 11 in the
    function. (Or, if you do, declare it as
    int comparearrays(int arrayA[11], int arrayB[11]), it makes no
    difference, but it helps you to avoid errors. The arrays in main
    have 10 elements, not 11.) Usually it is better to pass the size
    in a parameter.
    > {
    > if (arrayA[comparerA] == arrayB[comparerB])
    > {
    > falsifier = 1;
    > }
    > else
    > {
    > falsifier = 0;
    > break;
    > }
    > }
    >
    > return falsifier == 1;
    > }
    > /* Results:
    > returns boolean value and states if arrays are equal or not equal
    > */


    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
     
    Army1987, Oct 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raterus
    Replies:
    1
    Views:
    943
    Patrice
    Apr 23, 2004
  2. yurps
    Replies:
    0
    Views:
    369
    yurps
    Jun 10, 2005
  3. Michael Barrido
    Replies:
    2
    Views:
    376
    Carlos J. Quintero [VB MVP]
    Jun 23, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    874
    Lionel B
    Mar 1, 2007
  5. Richard Lawson
    Replies:
    2
    Views:
    148
    Jeff Cochran
    Jan 30, 2006
Loading...

Share This Page