If/Else Statements

Modify the program so that the user inputs both values to be tested for equality. Make sure you have a prompt for each input. Test the program with pairs of values that are the same and that are different.
Modify the program so that when the numbers are the same it prints the following lines:

The values are the same. Hey that's a coincidence!

Modify the revised program earlier by replacing the two if statements with a single if/else statement. Run the program again to test the results.


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
26
27
28
29
30
31
32
33
// This program tests whether or not an initialized value

// is equal to a value input by the user

// PLACE YOUR NAME HERE

#include <iostream>

using namespace std;

int main( )

{

int num1; // num1 is not initialized

int 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;

}
closed account (48T7M4Gy)
Unfortunately the mind-reading team is away at the moment so if you would like some help or advice could you please turn the above statement into a question?
Not 100% sure what you are looking for exactly. But to put them into a single if/else statement you'd want something like :

1
2
3
4
if (num1 == num2)
		cout << "Hey, that's a coincidence!" << endl;	
	else
		cout << " The values are not the same" << endl;


Also, note how the first statement I changed your if statement from the assignment operator ( = ) to the equality operator (==).
The assignment operator would assign the value of num2 to num1. The equality operator checks for equality.
Last edited on
Topic archived. No new replies allowed.