aint working..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace 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?
Last edited on
a == 5
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace 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)
Last edited on
Topic archived. No new replies allowed.