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

  • Thread starter myjunkcontainer
  • Start date
M

myjunkcontainer

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
*/
 
C

Clinto

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.
 
A

Army1987

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
*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top