-2

According to the program below, if donuts = 1 it will assign 0 to donuts, but if I run this it shows that 0 is assigned to donuts. Please tell me what I am over looking. Because how I see this code donuts should equal 12, because donuts should have fallen into the else:

#include <iostream> // Header file to enable console I/O
#include <string>   // Header file to enable string
#include <iomanip>  // enables maniplution of io strem
using namespace std;

// Begin main Function Definition
int main()
{
int donuts = 10;
if (donuts = 1)
{
    donuts = 0;
}
else
{
    donuts += 2;
}
cout<< donuts;
system("PAUSE");
return 0;

}
// End of main function
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
hurnhu
  • 888
  • 2
  • 11
  • 30
  • 3
    Typo, using `=` instead of `==`. – Shafik Yaghmour Sep 16 '14 at 01:46
  • Turn on/up your compiler warnings. – chris Sep 16 '14 at 01:46
  • Do `if (1 == donuts)` so you'll get an error if you make the same mistake again. – Hot Licks Sep 16 '14 at 01:51
  • 1
    @HotLicks with modern compilers [yoda conditions](http://stackoverflow.com/a/22106732/1708801) should not be required anymore. – Shafik Yaghmour Sep 16 '14 at 01:55
  • @HotLicks well sometimes it is a matter of using the correct warning flags which everyone really should be e.g. `-Wall -Wextra -Wconversion -Werror -pedantic` etc... Getting used to using these flags early on will save you a lot of pain in the long run. Also using more than one compiler can be really useful as well. – Shafik Yaghmour Sep 16 '14 at 01:59

1 Answers1

2
if (donuts = 1)

should be:

if (donuts == 1)
        //^^^

You need to use logical equal == or assignment =

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
taocp
  • 23,276
  • 10
  • 49
  • 62