Modifying the program

May 31, 2016 at 5:53pm
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 May 31, 2016 at 5:53pm
May 31, 2016 at 6:08pm
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.
May 31, 2016 at 6:24pm
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?
May 31, 2016 at 7:04pm
Other way around. You need to do num2 like num1.
May 31, 2016 at 7:13pm
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;
}
May 31, 2016 at 7:30pm
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.

May 31, 2016 at 10:18pm
closed account (D4NbpfjN)
so i need to add cin num2 after cin num1?
Jun 1, 2016 at 1:39am
Yup.
Topic archived. No new replies allowed.