BasedGod -OMG-

Member

Total Posts: 476
Online Status: Offline
Mon Oct 4 16:28:27 EDT 2010

Your welcome

in this program we have a program that includes an if statement, but no matter which integer the user inputs, the output will always show true, so what am I doing wrong?


// This program tests whether or not an initialized value
// is equal to a value input by the user


#include <iostream>
using namespace std;

int main()
{
    int num1,        // num1 is not initialized
    num2 = 5;        // num2 has been initialized to 5

    cout << "Please enter an integer" << endl;
    cin >> num1;

    cout << "num1 = " << num1 << " and num2 = " << num2 << endl;

    if (num1 = num2)
           cout << "Hey, that's a coincidence!" << endl;
    if (num1 != num2)
            cout << "The values are not the same" << endl;
    return 0;
}



oh my god

Lessa

Member

Total Posts: 220
Online Status: Offline
Tue Oct 5 10:55:35 EDT 2010

should it be
if (num1 = num2)
           cout << "Hey, that's a coincidence!" << endl;
   else
            cout << "The values are not the same" << endl;
return0;
( if-statement requires else-statement to end? )


.-,
Edited 1 time(s). Last edited by Lessa @ Tue Oct 5 11:06:56 EDT 2010

Arkona

Member

Total Posts: 800
Online Status: Offline
Tue Oct 5 16:42:04 EDT 2010

Quote from KittenInMyCerealz
should it be
( if-statement requires else-statement to end? )


no, that's not true. often you just don't want to do anything if the conditions aren't met.




Quote Wall Of Fame:
[21:02] <Lufia_Maxim> bitgubg'

[22:07] <%King Kod> I also start go sleeping now again

{Kill or Be Kill}

[19:34] <@Sociopath> I have at times called a cat simply "cat" to gain its attention

[13:23] <CYBER> but since my lvl is low so Iwill not sell brain fish becoz I need a lots of brain

WE ARE THE REBORNATION OF DEATH DESCIPLES

Arkona

Member

Total Posts: 800
Online Status: Offline
Tue Oct 5 16:42:05 EDT 2010


Post deleted by user


Cyberkilla

Admin

Total Posts: 5,844
Online Status: Offline
Tue Oct 5 17:00:16 EDT 2010



Invisible War ][

BasedGod -OMG-

Member

Total Posts: 476
Online Status: Offline
Tue Oct 5 18:02:37 EDT 2010

   actually the problem is highlighted in red

 if (num1= = num2)
           cout << "Hey, that's a coincidence!" << endl;
    if (num1 != num2)
            cout << "The values are not the same" << endl;
    return 0;





oh my god