GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Mar-2008, 12:17
supes69 supes69 is offline
New Member
 
Join Date: Mar 2008
Posts: 4
supes69 is on a distinguished road

Program output wrong


Ok guys I'm back. this time im a little farther in the chapter though
this time i'm trying to get 3 intergers from a user then calculate and print the sum, average, product, smallest and largest of the numbers. the program runs fine the only problem is that it prints out the smallest two numbers instead of just the smallest one.
here is the code

CPP / C++ / C Code:
#include <iostream>
using namespace std;
 
int main()
{
    int x, y, z, sum, ave, pro;
 
    cout << "enter three intergers \n";
 
    cin >> x >> y >> z;
 
    sum = x + y + z;
 
    ave = (x + y + z) / 3;
 
    pro = x * y * z;
 
    cout << "sum is " << sum << "\n";
    cout << "Average is " << ave << "\n";
    cout << "Product is " << pro << "\n";
 
 
    if (x > y, x > z) 
        cout << "largest is " << x << "\n";
 
    if (y > x, y > z)
        cout << "largest is " << y << "\n";
 
    if (z > x, z > y)
        cout << "largest is " << z << "\n";
 
    if (x < y, x < z) 
        cout << "smallest is " << x << "\n";
 
    if (y < x, y < z)
        cout << "smallest is " << y << "\n";
 
    if (z < x, z < y)
        cout << "smallest is " << z << "\n";
 
    return 0;            
 
}

ive run this through my head and on the pc using the numbers 3,4,5 but it still gives me this output

enter three intergers
3
4
5
sum is 12
average is 4
product is 60
largest is 5
smallest is 3
smallest is 4

thanks again for any help guys.
Last edited by admin II : 25-Mar-2008 at 04:10. Reason: Please surround your C++ code with [cpp] your code [/cpp]
  #2  
Old 24-Mar-2008, 15:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,527
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program output wrong


Quote:
Originally Posted by supes69
...the program runs fine
Not if it gives the wrong answers, it doesn't.

Consider the statement
CPP / C++ / C Code:
    if (x > y, x > z)

Here's what happens (expressions separated by commas are evaluated one at a time from left to right):

1. The expression x>y is evaluated (but nothing is done with the result).

2. The expression x>z is evaluated. If the result it "true," then the following statement is executed, otherwise it is not.

Similarly for your other logical expressions.

Maybe you mean:

CPP / C++ / C Code:
    if ((x > y) && (x > z))
        cout << "largest is " << x << "\n";

Etc.

Regards,

Dave
  #3  
Old 25-Mar-2008, 05:58
supes69 supes69 is offline
New Member
 
Join Date: Mar 2008
Posts: 4
supes69 is on a distinguished road

Re: Program output wrong


Thank you very much. I have not gotten that far in the book yet. I thought that the statement separated by the comma meant &. OK, back to the book. Thanks again.
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Intermediate output of one program into another... madhu542 C Programming Language 1 27-Nov-2007 03:02
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Cut -> C bulbulred C Programming Language 3 28-May-2006 15:07
Program output in wrong order strangr314 CPP / C++ Forum 3 08-Jul-2005 12:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.