Greetings (I need advise/input on a practice exercise)

Hello, I'm new to this forum and to c++ and I need help.

I'm reading C++ how to program 6ed by P.J and H.M Deitel (love the book, very easy to understand)

I am doing one of the exercises given at the end of the chapters (on chapter 2 at the moment). the exercise tells me to "write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers."

This is what I have so far:

// Three integers.cpp : Write a program that inputs three integers from the keyboard
// and prints the sum, average, product, smallest and largest of these numbers.


#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
int number1;
int number2;
int number3;
int sum;
int mean;
int product;


cout << "input three different intergers: "; // prompt user to enter number
cin >> number1 >> number2 >> number3; // input 3 number from user

sum = number1 + number2 + number3; // add numbers and store in sum

cout << "sum is " << sum << endl; // prints result for sum

mean = (number1 + number2 + number3) / 3; // add numbers and divide by the mean of 3 then store to mean

cout << "average is " << mean << endl; // prints result for mean

product = (number1 * number2 * number3); // multiply numbers

cout << "product is " << product << endl; // prints result for product

if (number1 < number2 < number3)
cout << "smallest is " << number1 << number2 << number3 << endl;

if (number1 > number2 > number3)
cout << "largest is " << number1 << number2 << number3 << endl;

return 0;
}


(Sorry I don't know how to post the codes properly, if there is a way to do so on this forum)

Now the problem that I have is that when I run the program, It works properly up to Product. When it gets to the Smallest problem it prints it weird and the Largest problem does not print at all.

Any input and advise/help would be appreciated.
To post code:

[code]your code here[/code]


As for your problem.. here is what your code is doing:

 
if (number1 < number2 < number3)


The < operator checks the values to the left of it and the right of it. if the left one is less than the right one, it returns 1 (true), otherwise it returns 0 (false).

So let's plug in numbers to make this easier to follow. Let's say:
number1 = 15
number2 = 6
number3 = 10

The computer will do this:
1
2
3
if( 15 < 6 < 10 )
if( (15<6) < 10 )  // is 15 < 6?  no, so replace with 0:
if( 0 < 10 )  // is 0 less than 10?  yes so this if statement succeeded 


As you might be able to see, the compiler isn't doing at all what you might expect. It's comparing the output of one < operator with a number. It isn't comparing all 3 numbers to each other.

Furthermore, you do this line:

 
cout << "smallest is " << number1 << number2 << number3 << endl;


Here, you're not printing the smallest number, you're just printing all 3 numbers one after the other.


To find the smallest, you'll need to use another variable, let's call it 'smallest'. Check 'smallest' against each number. If a number is smaller than smallest, then replace smallest with that number. Once you've checked it against all 3 numbers, you'll know you have the smallest of the 3.
Welcome [code] "Put your code inside code tags" [/code]

1
2
if (number1 < number2 < number3)
//comparisons like 'X<=Y<=Z' do not have their mathematical meaning 

you need to write if( a<b and b<c )
About code tags: http://cplusplus.com/articles/firedraco1/

The problem with the greatest in smallest is that your both your if statements and your output is incorrect. First, you cannot "chain" > and < like that; they return a boolean value, and "chaining" those operators results in something like true < number3 which means converting the bool to a 1 or 0 and comparing it to number3. This is obviously not what you want. The second if statement has the same problem.

Inside the if statements, you simply print all the numbers. You haven't actually figured out which one to print. You need to compare each number to each other number then print out which one you have found to be largest or smallest.

Think about how to do that for a bit, then post again if you are still having troubles.
I see, thanks.

Okay, I think I have figure out the if statement but I'm still stuck on how to print the output

1
2
3
int smallest;
int largest;
// I added these two variable 



1
2
3
4
5
6
7
8
9
if (number1 < number2) (number1 < number3);
		smallest = (number1 < number2 < number3);
		cout << "smallest is " << smallest << endl;

	
	if (number1 > number2) (number1 > number3);
		largest = (number1 > number2 > number3);
		cout <<  "largest is " << largest << endl;
// Still stuck on how to print 

Last edited on
That would already print out the smallest and largest, just put the <EOF> for both if statements.

Sum:
 
cout << "The sum is: " << number1 + number2 + number 3 << endl;


Product would be the same except replace the addition symbols (+) with multiplication symbols (*).

Average:
 
cout << "The average is: " << (number1*number2*number3)/3 << endl;


As for :

1
2
3
int sum;
int mean;
int product;


With this code, they're unnecessary, but can be implemented easily.

Hope this helps.

EDIT:
I've never heard of the stdafx library, so I'm assuming it came with the book.
Last edited on
stdafx.h is the pre-compiled header stuff that MSVC++ has on by default.

And your smallest and largest calculations are still wrong. A brute force would look like this:
1
2
3
4
5
6
if(number1<number2 && number1<number3)
    cout<<"smallest: "<<number1<<endl;
else if(number2<number1 && number2<number3)
    cout<<"smallest: "<<number2<<endl;
else if(number3<number1 && number3<number2)
    cout<<"smallest: "<<number3<<endl;
Topic archived. No new replies allowed.