For some reason my if statement in both my unionFunction and intersectFunction is setting the array equal to 1 rather then comparing it. I've tried to figure out whats wrong but nothing seems wrong to me. I appreciate any help.

Here is my program so far:

#include "stdafx.h"

#include <iostream>
 using namespace std;

#include <string>

#include <cstdlib>
 using std::rand;
 using std::srand;

#include <ctime>
 using std::time;

#include <iomanip>
 using std::setw;

// Global Arrays 
int arrayA[100];
int arrayB[100];
int arrayC[100];

// Global Integers 
int addDeleteNum = 0;
int addDeleteChange = 0;

// Global Char
char addDeleteStr;

// Global Call
void
unionFunction();
void intersectFunction();
void addDelete();
void driver();
void displayDriver();

// *******MAIN*********
int _tmain(int argc, _TCHAR* argv[])
{
	// Set Random Seed
	srand(0);

	driver();
	unionFunction();
	displayDriver();
	addDelete();

	system("Pause");
}

// Or Statement Comparision 
// If A or B Are 1 Set C To That Number
void unionFunction() 
{
	for (int i = 0; i <100; i++) {

		if ((arrayA[i] = 1) || (arrayB[i] = 1)) {
			arrayC[i] = 1;
		}
		else {
			arrayC[i] = 0;
		}
	}
}

// And Statement Comparision 
// If A and B Are Same Set C To That Number
void intersectFunction()
{
	for (int i = 0; i <100; i++) {
		if ((arrayA[i] = 1) && (arrayB[i] = 1)) {
			arrayC[i] = 1;
		}
		else {
			arrayC[i] = 0;
		}
	}
}

void addDelete()
{
	cout << "Please Input the Array Letter (A or B): ";
		cin.get(addDeleteStr);

	cout << "Please Input the number you would like to add or delete: ";
	cin >> addDeleteNum;


		// Changes the values
		switch(addDeleteStr) {
		case 'a': 
		case 'A': 
			cout << "The number is currently: " << arrayA[addDeleteNum];
			cout << "\nWhat would you like to change it to? ";
			cin >> addDeleteChange;

			arrayA[addDeleteNum] = addDeleteChange;
			break;

		case 'b':
		case 'B': 
			cout << "The number is currently " << arrayB[addDeleteNum];
			cout << "What would you like to change it to? ";
			cin >> addDeleteChange;

			arrayB[addDeleteNum] = addDeleteChange;
			break;

		default: cout << "Invalid input program exit";
			break;
	}
}
	

// Fills Arrays with Random 0 and 1
void driver() 
{
	int randNum = 0;

	for (int i = 0; i < 100; i++) {
		randNum = rand() % 2;
		arrayA[i] = randNum;
		
	}

	for (int i = 0; i < 100; i++) {
		randNum = rand() % 2;
		arrayB[i] = randNum;
	}
	
}

void displayDriver()
{
	for (int i = 0; i < 100; i++) {

		cout << setw( 5 ) << right << arrayA[i]
		<< setw( 8 ) << left << arrayB[i]
		<< setw(5) << left << arrayC[i]
		<< ( 100 % 2 == 0 ? '\n' : '\t' );
	}
}

Recommended Answers

All 7 Replies

You need to use == instead of = in your if statements.

'=' sets the thing on the left equal to the thing on the right, == means 'is equal to.'

The reason is that blah = 1 sets the lvalue blah to 1. You want blah == 1 , which evaluates to true if blah is 1. It's generally a good idea in C++ to get in the habit of putting constants like 1 on the left-hand side of equality comparisons. That reduces the probability of making this kind of error.

Oh wow what a really dumb mistake on my part, thank you both for your help.

don't type else part of if statements!just write two if statements!this would save you some time when you want to later view the program!

don't type else part of if statements!just write two if statements!this would save you some time when you want to later view the program!

What the fuck are you talking about?

commented: Fully Agree +10

don't type else part of if statements!just write two if statements!this would save you some time when you want to later view the program!

What The Hell? Please don't post crappy advice.

commented: <circlejerk/> +9

sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.