Adblock breaks this site

C++ Help - School task :P

Discussion in 'Programming General' started by Dome106, Sep 28, 2010.

  1. Dome106

    Dome106 Forum Addict
    Banned

    Joined:
    Dec 9, 2009
    Posts:
    383
    Referrals:
    0
    Sythe Gold:
    57
    C++ Help - School task :P

    Well, I've got a school task and I have some problems with it.
    I need to make a program in which you put in 3 numbers and it prints out which one is the biggest. I have that done already, no problem. The problem is that when I enter 2 of the same numbers (example 5 5 1) it says that 1 is the biggest. I don't know what to do :p.

    Here's what I have:
    Code:
    int main(int argc, char *argv[])
    {
        int a, b, c;
        cin >> a >> b >> c;
        
        
        if (a > b && a > c)
        {
              cout << "A is the biggest" << endl;
        }     
        else if (b > a && b > c)
        {
             cout << "B is the biggest" << endl;
        }
        
        else
        {
            cout << "C is the biggest" << endl;
        }   
            
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    Please help me guys =)
     
  2. xRSx Newb

    xRSx Newb Member

    Joined:
    Sep 27, 2010
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0
    C++ Help - School task :P

    Hey not sure if you still need help with this but your problem is that if two numbers are the same, your if and else if statements will both evaluate to false, meaning that the code will always execute the else statement and no matter what the inputs are, C will always be chosen as the biggest number.
     
  3. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    C++ Help - School task :P

    If an else is not a good way to do your sorting (finding out biggest number). Look into bubble sort (inefficient but easy to understand) or if you want something efficient then look into merge sort.

    Then you can find the biggest number in an array of numbers (any size).
     
  4. Sythe

    Sythe Join our discord

    test

    Administrator Village Drunk

    Joined:
    Apr 21, 2005
    Posts:
    8,072
    Referrals:
    468
    Sythe Gold:
    5,287
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi
    C++ Help - School task :P

  5. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    C++ Help - School task :P

  6. Sythe

    Sythe Join our discord

    test

    Administrator Village Drunk

    Joined:
    Apr 21, 2005
    Posts:
    8,072
    Referrals:
    468
    Sythe Gold:
    5,287
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi
    C++ Help - School task :P

    Virtually every language has predefined math functions.

    And it is more appropriate to use an inline for this, if you wanted to write your own. Macros can change the meaning of what you are trying to do if they're not constructed and used correctly. Inline functions give you a little more protection against this, without any function call overhead.
     
  7. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    C++ Help - School task :P

    Typically macro problems can be addressed by preprocessing to a file and taking a direct look at what is actually being done. You're right, though, now that I think about it, inline functions would be better.
     
  8. Dome106

    Dome106 Forum Addict
    Banned

    Joined:
    Dec 9, 2009
    Posts:
    383
    Referrals:
    0
    Sythe Gold:
    57
    C++ Help - School task :P

    Thanks Sythe, helped me a lot =).
     
  9. Ashken

    Ashken Newcomer

    Joined:
    Dec 18, 2009
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0
    C++ Help - School task :P

    Code:
    
    int maximum( int,int int);
    
    int main()
    {
       int a,b,c;
       cin>> a>>b>>c;
       cout<<"maximum value is "<<maximum(a,b,c);
    return o;
    }
    int maximum(int x,int y,int z)
    {
       int maxVal = x;
    
       if (y > maxVal)
         maxVal = y;
    
      if (z > maxVal)
        maxVal = z;
    
     return maxVal;
    }
    
    
     
< [source]Runescape Browser[source] | A Simple Hello world Tutorial for Java! Part 1 of many. >


 
 
Adblock breaks this site