Modifying the program

closed account (D4NbpfjN)
Write your question here.

It is asking for me to 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. So where do i start on this question?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // 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
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;
}

Last edited on
The question states: "the user inputs both values to be tested".
You've hard coded one value (num2).

Other than that, your program is fine.

line 15: You really don't need a second if statement here. An else will work just fine.
closed account (D4NbpfjN)
So since i have coded num2, I just need to do the first num1 just like num2 for it to be tested for equality?
Other way around. You need to do num2 like num1.
closed account (D4NbpfjN)
thank you. I tried that and I am getting some errors. Below is the modified code. What did i do wrong to be getting errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
	int
		num1, // num1 is not initialized
		num2, // num2 is not initialized
	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;
		else (num1 == num2)
			cout << "The values are not the same" << endl;
	}
	return 0;
}
Line 7: You need to terminate the declaration with a ; not a ,

Line 9: You're not doing a cin of num2.

Line 14: else does not take a condition. Remove it.

Line 11,16: {} are not necessary.

closed account (D4NbpfjN)
so i need to add cin num2 after cin num1?
Yup.
Topic archived. No new replies allowed.