My error message: error C2659: '=' : function as left operand

Oct 15, 2009 at 10:06pm
I'm not quite sure why I'm getting this message. This is my whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<ctime>
using namespace std;


int divide ()
{
	int choice;
	cout<<"Please choose which game you would like to play:\n"
		<<"1 High low\n"
		<<"2 Roulette\n"
		<<"3 Blackjack\n"
		<<"4 Slots\n"
		<<"5 I want to leave";
	cin>>choice;

	return(choice);
}

int highlow ()
{
	int a = 4;
	cout<<a;
	return (a);

}

int roulette ()
{
	cout<<"roulette";
	return(2);
}

int blackjack ()
{
	cout<<"blackjack";
	return(3);
}

int slots()
{
	cout<<"slots";
	return(4);
}

int main()
{
//money player has
	srand((unsigned)time(0));
	int randa = 100;
	int randb = 100;
		int money = rand()%randb+randa;

	cout<<"Welcome to the Eiselen Casino!\n\nYou have $"<<money<<" to gamble with.\n\nYou can play High-Low, Roulette, Blackjack, or Slots. You may play for how ever long you would like as long as you do not bust. \nGood luck!\n\n";


	divide();
	if (divide = 1) {int highlow();}
	else if (divide = 2) {int roulette();}
	else if (divide = 3) {int blackjack();}
	else if (divide = 4) {int slots();}
	system("PAUSE");
	return 0;
}


The last few sets of lines are where the problem is. I don't know why, because divide() returns either 1-4, which is the problem I'm looking at right now - this isn't a finished code at all.

I only have it so if I enter it so that divide would returns '1', it should work, but these if statements aren't functioning for some reason..?
Oct 15, 2009 at 10:11pm
You need to review your knowledge about functions: http://www.cplusplus.com/doc/tutorial/functions/
and on comparison use == operator, = is for assignment
Oct 15, 2009 at 10:18pm
In your if statements you are assigning divide (which is a function) the values 1-4. On line 57 you aren't assigning the return value of divide() to anything. What I think you need to do (because I'm not sure what the aim of your program is) change line 57 to something line int d = divide(); and your if statements need to check the value of that using == not =.

The bodies of your if statements (int highlow(); // etc ) aren't actually doing anything (and don't make sense) either.

You also don't need to enclose the return value in brackets in your functions (although it makes no difference).
Oct 15, 2009 at 10:28pm
Awesome, it lives. Thank you so much :)
Oct 16, 2009 at 12:41am
Function as left operand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int& function()
  {
  int n;
  return n;
  }

int value()
  {
  int n;
  return n;
  }

int main()
  {
  function() = 42;

  cout << value() << endl;

  return 0;
  }

;-]
Topic archived. No new replies allowed.