-4

This is the function I made:

void loading(bool wsound) {

if (wsound = true)
{

    PlaySound(TEXT("sounds/intro.wav"), NULL, SND_ASYNC);
    cout << "..........";
    Sleep(1000);
    cout << "..........";
    Sleep(1000);
    cout << "..........";
    Sleep(1000);
    cout << "..........";
    Sleep(1000);
    cout << "..........";
    Sleep(1000);
    cout << ".........." << endl;
    Sleep(1000);

}
else if (wsound = false)
{
    cout << "..........";
    Sleep(1000);
    cout << "..........";
    Sleep(1000);
    cout << ".........." << endl;

}
else {

    cout << "An error occured" << endl;


    cin.get();
        system("exit");

    }
}

So what this basically does is that it takes a bool if the value is true then it loads it with sounds if false then it loads it without sounds.

My problem is that in the main I placed a bool with the value of true then it worked though after calling it again with the value type of false it still loads it with sounds.

The code is like this:

//a bunch of code here 
loading(true);
//a bunch of code here
loading(false);
//and more codes.........
Bulat
  • 720
  • 7
  • 15
  • 1
    You *do* know the difference between assignment (using `=`) and comparison for equality (with `==`)? If you take some time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) (which all programmers should know!) then you would have found out the problem quite quickly. – Some programmer dude Apr 17 '18 at 06:17
  • `if (wsound = true)` is always true. – Stan Apr 17 '18 at 06:18
  • yes the first one assigns and the second one compares, just a tiny mistake ill try to use == – Mr.Shaking Apr 17 '18 at 06:19
  • @Mr.Shaking What value of wsound passes the control to the else statement bypassing if ( wsound == true ) and else if (wsound == false )? – Vlad from Moscow Apr 17 '18 at 06:24

1 Answers1

3

This is not a check, this is an assignment: wsound = true

You should use something like wsound == true, however in an if statement it is enough to only use if (wsound), which is equal to if (wsound == true). For the false check you could use: if(!wsound).

EDIT: To understand what happens: if(wsound = true) is a correct statement, because the assignment operator=(), used in this if statement, should in the most cases return a non-cost reference to the variable, which is already explained here.

M. Denninger
  • 151
  • 9