Program that finds the middle number

This is a discussion on Program that finds the middle number within the C++ Programming forums, part of the General Programming Boards category; What I'm trying to do is write a program to find the middle number of 3 sets of variables entered. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    Program that finds the middle number

    What I'm trying to do is write a program to find the middle number of 3 sets of variables entered. I'm starting off doing it in basic fashion , not advanced using fuctions etc. This is the code I have so far, but when I excute the program returns no answers.

    Code:
    #include <iostream>
    using namespace std;
    
    //1,2,3
    
    int main()
    {
    
     int a,b,c;
    
    cout << " Enter number " << endl;
     cin >>a;
    
     cout << " Enter 2nd number " << endl;
     cin >>b;
    
     cout << " Enter 3rd number " << endl;
     cin >>c;
    
     if (a > b && a < c)
     
      cout <<b<< " middle number" << endl;
     
    
    else if (c > a && c < b)
     
      cout <<a<< " middle number" << endl;
     
    
    else if (b > c && b < a)
     
      cout <<c<< " middle number" << endl;
     
    
    
    system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    I did this, and got better results, but still not the proper execution. Basically the 3rd part of the program will not execute any answer at all.

    Code:
    #include <iostream>
    using namespace std;
    
    //1,2,3
    
    int main()
    {
    
     int a,b,c;
    
    cout << " Enter number " << endl;
     cin >>a;
    
     cout << " Enter 2nd number " << endl;
     cin >>b;
    
     cout << " Enter 3rd number " << endl;
     cin >>c;
    
     if (a > b )
    	 cout <<a<< " greater number" << endl;
     
    if	 (a < c)
    cout <<b<< " middle number" << endl;
     
     
      
    
     if (c > a )
    	 cout <<c<< " greater number" << endl;
    if	 ( c < b)
     
      cout <<a<< " middle number" << endl;
     
    
     if (b > c )
    	 cout <<b<< " greater number" << endl;
    if	 ( b < a)
     
      cout <<c<< " middle number" << endl;
     
    
    
    system("pause");
    	return 0;
    
    	
    
    }
    Last edited by Zerohero11; 10-12-2005 at 09:15 PM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    i tried this and it worked.

    Code:
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    
    int main()
    {
        int a, b, c;
        cout<< "Enter a number:";
        cin>> a;
        cout<< "\nEnter another number:";
        cin>> b;
        cout<< "\nEnter the last number:";
        cin>> c;
        if (a < b)
        {
              if (a > c)
              {
                    cout<< "\n" << a << " is the middle number.\n";
              }
        }
        if (a < c)
        {
              if (a < b)
              {
                    cout<< "\n" << a << " is the middle number.\n";
              }
        }
        if (b < c)
        {
              if (b > a)
              {
                    cout<< "\n" << b << " is the middle number.\n";
              }
        }
        if (b < a)
        {
              if (b > c)
              {
                    cout<< "\n" << b << " is the middle number.\n";
              }
        }
        if (c < b)
        {
              if (c > a)
              {
                    cout<< "\n" << c << " is the middle number.\n";
              }
        }
        if (c < a)
        {
              if (c > b)
              {
                    cout<< "\n" << c << " is the middle number.\n";
              }
        }
        system("PAUSE");
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Thanks for the help. Can you explain what those librarys you added consist of?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    well i cant remember which header file had system("PAUSE") so i just put both of htem in there lol, but without those im pretty sure system("PAUSE") wont work..

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by aznboi
    well i cant remember which header file had system("PAUSE") so i just put both of htem in there lol, but without those im pretty sure system("PAUSE") wont work..
    I see,

    As far as the program , when you ran it did it work properly for you, and did the calculations correctly?

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    o wow. I just tried to figure out which one of those headers enabled u to use system("PAUSE") and it turns out htat you don't even need them! wow all those times i added that in there without needing too....

    but anyway yeah i tried it, it worked. but if u dont get any of the code you should ask... but if u understand it all then great

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by aznboi
    o wow. I just tried to figure out which one of those headers enabled u to use system("PAUSE") and it turns out htat you don't even need them! wow all those times i added that in there without needing too....

    but anyway yeah i tried it, it worked. but if u dont get any of the code you should ask... but if u understand it all then great
    I jsut want to make sure in all scenero's that the program exected the calculations correctly. You seem like an experienced programer thats why I asked.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    me? experienced? no way lol. I read a C++ tutorial and after learning the basics, stopped reading and just made programs using stuff like cout and cin lol. i still dont even kno how to make a good struct/class thingy ma jigg

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    The system function is declared in the stdlib.h header however, both that header and the stdio.h header are deprecated. If you had to include those headers you should use the newer versions which are cstdlib and cstdio (both without the .h extension). Also, calls to system should be avoided where possible, you could have achieved the same effect with a call to cin.get() instead.

    A slightly more compact version of that checking would be:

    Code:
    if( (b < a && a < c) || (c < a && a < b) )
        cout << a << " is the middle number." << endl;
    
    else if( (a < b && b < c) || (c < b && b < a) )
        cout << b << " is the middle number." << endl;
    
    else
        cout << c << " is the middle number." << endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by hk_mp5kpdw
    The system function is declared in the stdlib.h header however, both that header and the stdio.h header are deprecated. If you had to include those headers you should use the newer versions which are cstdlib and cstdio (both without the .h extension). Also, calls to system should be avoided where possible, you could have achieved the same effect with a call to cin.get() instead.

    A slightly more compact version of that checking would be:

    Code:
    if( (b < a && a < c) || (c < a && a < b) )
        cout << a << " is the middle number." << endl;
    
    else if( (a < b && b < c) || (c < b && b < a) )
        cout << b << " is the middle number." << endl;
    
    else
        cout << c << " is the middle number." << endl;
    Thanks alot for you help!

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    I also noticed a very simple error in your original code. after the if statements you forgot to include the opening and closing brackets.

    Code:
    { and }

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Orrill
    I also noticed a very simple error in your original code. after the if statements you forgot to include the opening and closing brackets.

    Code:
    { and }
    That's unnessasary because the following statement is only one line long.

    Code:
     
    if ( 1 == 1 )
       cout << "This is a proper if statement" << endl;
       cout << "...but this line isn't part of the if statement";
       
    if ( 1 == 1 ) {
       cout << "This is a proper if statement" << endl;
       cout << "Now this is part of the if statement";
    }
    Last edited by SlyMaelstrom; 10-13-2005 at 05:04 PM.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Thumbs up

    Quote Originally Posted by SlyMaelstrom
    That's unnessasary because the following statement is only one line long.
    Didn't realise that, thanks for correcting me. Learning all the time eh!

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Now by "line" understand I mean one statement line, not one line in the IDE interface.

    Code:
    if ( 1 == 1 )
       cout << "This " 
            << "is "
            << "also "
            << "all "
            << "part "
            << "of "
            << "the "
            << "if "
            << "statement." 
            << endl; // The semicolon ends the statement.
    Last edited by SlyMaelstrom; 10-13-2005 at 05:13 PM.
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Fibonacci number program problem(assembly)
    By ok_good in forum Tech Board
    Replies: 7
    Last Post: 04-07-2006, 07:27 PM

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