-2

The program has to request 3 numbers from an individual, and then return the largest and secondLargest of those numbers. This is what I have:

{
    int num1, num2, num3;
    int largest, secondLargest;

    printf("Enter number 1: ");
        scanf("%d",&num1);
    printf("Enter number 2: ");
        scanf("%d",&num2);
    printf("Enter number 2: ");
        scanf("%d",&num3);
        {
    if(num1>num2);
        largest=num1;
        if(num3>num1);
        secondLargest=num1;
            largest=num3;

    printf("The largest number is: %d\n"),largest;
    printf("The second largest number is: %d"),secondLargest;
        }

    return 0;
}

Very Specifically, the output must follow the following format

Enter number 1: 27

Enter number 2: 36

Enter number 3: 12

The largest number is: 36

The second largest number is: 27


When I run the program, it returns insanely large numbers, which leads me to believe I am missing something very simple, or that I have 0 understanding of what is required for even a basic program like this.

Any help is greatly appreciated!

Maks3w
  • 6,014
  • 6
  • 37
  • 42
dfwlly
  • 17
  • 2
    You are not coding in Visual Studio, but in C++ ; Visual Studio is perhaps your compiler, you could use [GCC](http://gcc.gnu.org/) or [Clang/LLVM](http://clang.llvm.org/). You should edit your question to improve it: which standard of C++ (i.e. C++11) do you want to use? And you should enable all warnings and debug info in your compiler then use the debugger to run your program step by step. Don't expect us to do your homework – Basile Starynkevitch Feb 08 '15 at 19:18
  • The `scanf` has buffer overrun possibilities. See http://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c Also, search the internet for "scanf buffer overrun". – Thomas Matthews Feb 08 '15 at 19:22

4 Answers4

3

Either your code is so badly formatted either you're not familiar with if statement. There is no need to put ; after if(something).

if(num1>num2)
    largest=num1;
if(num3>num1) {
    secondLargest=num1;
    largest=num3;
}

Moreover, there are two strange curly braces after the last scanf() and the last printf().

ForceBru
  • 43,482
  • 10
  • 63
  • 98
2

Firstly, are they also requiring the use of printf?

printf("The largest number is: %d\n"),largest;
printf("The second largest number is: %d"),secondLargest;

That's not correct syntax for the function. The format specifiers without arguments is likely injecting garbage.

printf("The largest number is: %d\n",largest);
printf("The second largest number is: %d",secondLargest);

That's the proper way, but you have other issues as well, but this should fix the output-ing of junk.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • Yes, the requirements of printf is there. I knew I must have missed something simple, so I greatly thank you for your input. – dfwlly Feb 08 '15 at 19:30
2

In C++, an if-statement looks like this:

if (condition) {
    Do this..
} else {
    Do that...
}

The curly braces are optional if only one line should be executed if the condition applies. The else-clause is also optional.

Second, your printf calls are wrong. All parameters have to be within the parantheses or they won't be compiled as arguments. Also, add a newline to your last printf call.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • You guys are WONDERFUL! I am very very new to this, and in the future, will try not to offend these professionals in how I ask for assistance. – dfwlly Feb 08 '15 at 19:26
0
  1. You used if expressions incorrectly. You shall not use ; end of if conditions.
  2. You forgot to consider all the situations.
  3. You use printf() incorrect!

This is part of your code :

if(num1>num2);
 largest=num1;
        if(num3>num1);
        secondLargest=num1;
            largest=num3;

That is equal to :

if(num1>num2)
    {
    }
largest=num1;
if(num3>num1)
    {
    }
secondLargest=num1;
largest=num3

So, first you must remove the ; characters from the end of if lines and add {} where it is needed (where you want to run more that one line command in if expression):

if(num1>num2)
    largest=num1;

if(num3>num1)
    {
      secondLargest=num1;
      largest=num3;
    }

Is the above code OK? No!

why?

Assume :

num1=10, num2=15, num3=5

What happens in this case? Nothing,As num1<num2 and num3<num1The program skip if expressions and your largest and secondLargest variables won't initialized. The value that you see in output is the random value that assigned to them when you defined them.

So you must add all the situations also.

And finally you must use printf() as below :

printf("The largest number is: %d\n",largest);

The final program looks like this:

  {
    int num1, num2, num3;
    int largest, secondLargest;

    printf("Enter number 1: ");
        scanf("%d",&num1);

    printf("Enter number 2: ");
        scanf("%d",&num2);

    printf("Enter number 2: ");
        scanf("%d",&num3);


    if(num1>num2)
        {
        largest=num1;
        secondLargest=num2;
        }
    else
        {
        largest=num2;
        secondLargest=num1;
        }

    if(num3>largest)
       {
        secondLargest=largest;
        Largest=num3;
       }
    else
    if (num3>secondLargest)
           secondLargest=num3;

    printf("The largest number is: %d\n",largest);
    printf("The second largest number is: %d",secondLargest);

    return 0;
}
Jean
  • 687
  • 1
  • 9
  • 25
  • Am1r, thank you for your patience and your assistance! I am seeing that I have no understanding of the else statements, as well as proper use of my syntax in various functions. – dfwlly Feb 08 '15 at 19:58
  • @dfwlly You're welcome.Yes, You must use some tutorial I think :) I think this is a good source : http://www.cplusplus.com/doc/tutorial/ - and if you received your answer among these answers, you can check the correct answer for future viewers :) – Jean Feb 09 '15 at 03:22