#include <iostream>
usingnamespace std;
int main()
{
float a,b,result;
cout<<"\n Enter a number: ";
cin>>a;
if (a=5)
{cout<<"we dont accept fives\n";
return 0;
}
cout<<"\n Enter the second number: ";
cin>>b;
result=a+b;
cout<<"\n Result: "<<result<<"\n";
cin.get();
return 0;
}
my if statement seems to be activating no matter what number i press...even though it should only be activating if a=5 right?
i just tried that and it still activates my if statement no matter what number i press...oh...maybe i have to add the else command to the rest of the code?
i just tested your code (with Bazzy's modification) and it works for me.
5 will be rejected, every other number works fine, just how it is supposed to be.
and you don't need the else-statement. see, since you have "return 0;" in your if {... the programm will end, when it recieves a 5.
#include <iostream>
usingnamespace std;
int main()
{
float a, b, result;
cout << "\n Enter a number: ";
cin >> a;
if (a == 5) {
cout << "we dont accept fives\n";
return 0;
}
cout << "\n Enter the second number: ";
cin >> b;
result = a + b;
cout << "\n Result: " << result << "\n";
cin.get();
return 0;
}
then does spacing in some code matter? because thats the only difference i see between the code you posted above ( which works for me btw) and the one i pasted where i just added another "="sign to the process
EDIT: nvm i had a ; added to the end of my if (a == 5)