Simple Program help.

This is a discussion on Simple Program help. within the C++ Programming forums, part of the General Programming Boards category; Objective: Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Simple Program help.

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

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main()
    {
    
          int a,b,c,sum, product,average,small,large;
          cout <<"Enter Three Numbers " <<endl;
          cin >>a;
          cin >>b;
          cin >>c;
          sum = a + b + c;
          cout <<"The sum is equal to " <<sum <<endl;
          average = sum / 3;
          cout <<"The average is qual to "<<average <<endl;
          product = a*b*c;
          cout <<"The product is equal to "<<product <<endl;
          if (a > b && c)
             cout <<"The largest number is "<<a  <<endl;
          if (a < b && c)
             cout <<"The smallest number is "<<a  <<endl;
          if (b > a && c)
             cout <<"The largest number is "<<b  <<endl;
          if (b < a && c)
             cout <<" is The smallest number is "<<b  <<endl;
          if (c > b && a)
             cout <<"The largest number is "<<c  <<endl;
          if (c < b && a)
             cout <<" is The smallest number is "<<c  <<endl;
          if (a == b && a == c && b == c)
             cout <<"All numbers are equal to each other" <<endl;
           cin >>a;
          return 0;
    }
    The problem is that some times the largest or smallest number is displayed twice. Whats the problem and how do I fix it? Im guessing its a simple logic error?
    Last edited by RealityFusion; 07-15-2004 at 06:17 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> if (a > b && c)

    that's not how the logical AND operator works. suppose you meant:

    if (a > b && a > c)
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok, I get it. Thanks!
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    The program would flow better if the comparison logic was separated into individual functions. As it is, you just need to specify the second case in the conditional statement. Stating "&& c" for example will always be true unless c is equal to 0.
    Code:
    if (a > b && a > c)
             cout <<"The largest number is "<<a  <<endl;
          if (a < b && a < c)
             cout <<"The smallest number is "<<a  <<endl;
          if (b > a && b > c)
             cout <<"The largest number is "<<b  <<endl;
          if (b < a && b < c)
             cout <<" is The smallest number is "<<b  <<endl;
          if (c > b && c > a)
             cout <<"The largest number is "<<c  <<endl;
          if (c < b && c < a)
             cout <<" is The smallest number is "<<c  <<endl;
          if (a == b && a == c)
             cout <<"All numbers are equal to each other" <<endl;
    David

    ** Late on the response.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21