• Log in or sign up for an account here: https://www.sythe.org/login

C++ Help - School task :P

Dome106

Forum Addict

Banned
Joined
Dec 9, 2009
Sythe Gold
57
Messages
383
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 =)
 
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.
 
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).
 
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
I never knew there was a built in API for that. I've been using #define MAX(a,b) a > b ? a : b all along.

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.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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.
 
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;
}
 
Runescape Gamblingrunescape gambling
Back
Top