Hello, i I'm a beginner in the arts of c++ and stumbled upon a little logical question while doing a practice program I designed myself about equations.
Don't know how to post the code as IDE-like code so if someone can enlighten me,
please do. (This have been corrected.)
At the first if equation if (a == b, a == c). I first wrote if (a == b, b == c)
The problem was that if the first entry was 100, and the 2 last was, say 50
it reported all 3 numbers to match.
I found the problem but don't know why this is correct and the other not.
The program is as follows.
#include <iostream>
usingnamespace std;
int main ()
{
int a, b, c; // Declares 3 integer variables.
/* The program starting text */
cout << "Enter 3 numbers and the program will check if they match." << endl;
cout << "Enter a number: "; // Promt for the first input.
cin >> a;
cout << "Now enter another: "; // Second promt.
cin >> b;
cout << "Now for the final number: "; // Final promt.
cin >> c;
if (a == b, a == c) // This one checks if all 3 input matches.
cout << "All 3 numbers match.";
elseif (a==b)
cout << "The first and second numbers match.";
elseif (a==c)
cout << "The first and last mumbers match.";
elseif (b==c)
cout << "The second and last numbers match.";
else
cout << "None of the numbers you entered match each other.";
return 0;
}
Replace the comma in the if statement with &&. && is the AND logic operator, and checks first whether the left is true, and then whether the right is true. If they are both true, than it returns true.
That will work perfectly. Also, next time you post code, be sure to use the ['code][/code] tags, just so that it is easier to read (being sure to get rid of that apostrophe in the first half of the tag).