-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
5
  • 3
    Typo, using = instead of ==. Commented Sep 16, 2014 at 1:46
  • Turn on/up your compiler warnings. Commented Sep 16, 2014 at 1:46
  • Do if (1 == donuts) so you'll get an error if you make the same mistake again. Commented Sep 16, 2014 at 1:51
  • 1
    @HotLicks with modern compilers yoda conditions should not be required anymore. Commented Sep 16, 2014 at 1: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. Commented Sep 16, 2014 at 1:59

1 Answer 1

2
if (donuts = 1)

should be:

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

You need to use logical equal == or assignment =

Sign up to request clarification or add additional context in comments.

1 Comment

i am ashamed at how much i over look that....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.