Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 04:30 AM   PM User | #1
eLK
New Coder

 
Join Date: Dec 2007
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
eLK is an unknown quantity at this point
C++ Help

So this is for school, and it's supposed to get 3 numbers from the input and output if no numbers are the same, 2 numbers are the same, or all the numbers are the same. I know I'm supposed to use an else if statement but not sure really how to do it. Here's what I have so far

Quote:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int num1, num2, num3;

// Get User Input

cout << "Please enter the first number (an integer): ";
cin >> num1;

cout << "Please enter the second number (an integer): ";
cin >> num2;

cout << "Please enter the third number (an integer): ";
cin >> num3;

cout << endl;

// Go Program Go!

if (num1 == num2)
cout << "Exactly two of the numbers are the same." << endl;

if (num2 == num3)
cout << "Exactly two of the numbers are the same." << endl;

if (num1 == num3)
cout << "Exactly two of the numbers are the same." << endl;

else
cout << "None of the numbers are the same." << endl;

cout << endl;

return 0;

}
It's displaying both couts, what's causing the else to output?

Last edited by eLK; 10-10-2012 at 02:50 PM..
eLK is offline   Reply With Quote
Old 10-10-2012, 05:43 PM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
You didn't put the actual else ifs in. With your code each if statement is independent from each other. If you use your debug and step through your program line by line, you'd see what it is doing.

Code:
if (num1 == num2)
cout << "Exactly two of the numbers are the same." << endl;

else if (num2 == num3)
cout << "Exactly two of the numbers are the same." << endl;

else if (num1 == num3)
cout << "Exactly two of the numbers are the same." << endl;

else
cout << "None of the numbers are the same." << endl;
Your checks though are not sufficient in the event that all three numbers are the same. In addition you could consolidate all of the checks into one if statement using logical ORs.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:30 PM.


Advertisement
Log in to turn off these ads.