1

(Largest of three numbers)
Write a function with the following header to display the largest of three numbers:

void displayLargest(double num1, double num2, double num3)

Write a test program that prompts the user to enter three numbers and invokes the function to display the largest of them.

The following is my program:

#include<iostream>
#include<iomanip>
using namespace std;

void displayLargest (double num1, double num2, double num3)
{  
    if (num1 > num2 && num1 > num3){
        cout << num1;
    }
    else if (num2 > num1 && num2 > num3){
        cout << num2;
    }
    if (num3 > num1 && num3 > num2){
        cout << num3;
    }
}

int main()
{
    double num1, num2, num3;
    cout << "Please enter 3 numbers:";
    cin >> num1, num2, num3;

    displayLargest (double num1, double num2, double num3);
    system("pause");
    return 0;
}

But, the system shows the following error:

main.cpp: In function ‘int main()’:
main.cpp:28:21: error: expected primary-expression before ‘double’
     displayLargest (double num1, double num2, double num3);
                     ^~~~~~
main.cpp:28:34: error: expected primary-expression before ‘double’
     displayLargest (double num1, double num2, double num3);
                                  ^~~~~~
main.cpp:28:47: error: expected primary-expression before ‘double’
     displayLargest (double num1, double num2, double num3);
                                               ^~~~~~
4

3 回答 3

1

This

displayLargest (double num1, double num2, double num3);

is a syntactically invalid construction.

The function call will look like

displayLargest(num1, num2, num3);

If you are using the standard C function system then you need to include the header <cstdlib>.

Also the function itself is incorrect. It does not take into account that the user can enter three or two equal numbers.

The function can be defined in different ways.

Here are two examples of the function definition.

Either

void displayLargest( double num1, double num2, double num3 )
{
    if ( !( num1 < num2 ) && !( num1 < num3 ) )
    {
        std::cout << num1 << '\n';
    }
    else if ( !( num2 < num3 ) )
    {
        std::cout << num2 << '\n';
    }
    else
    {
        std::cout << num3 << '\n';
    }
}

or

void displayLargest( double num1, double num2, double num3 )
{
    double largest = num1;

    if ( largest < num2 )
    {
        largest = num2;
    }

    if ( largest < num3 )
    {
        largest = num3;
    }

    std::cout << largest << '\n';
}

If you are allowed to use standard algorithms then the function can be implemented using the algorithm std::max.

Here is a demonstrative program.

#include <iostream>
#include <algorithm>

void displayLargest( double num1, double num2, double num3 )
{
    std::cout << std::max( { num1, num2, num3 } ) << '\n';
}

int main() 
{
    displayLargest( 1.2, 1.3, 1.1 );

    return 0;
}
于 2020-03-27T09:37:28.867 回答
0

更改此行:

displayLargest(double num1, double num2, double num3);

至:

displayLargest(num1, num2, num3);
于 2020-03-27T09:26:38.067 回答
0

首先,要在一行中读取三个数字,只需将cin管道扩展到所有三个变量。

您描述的问题是编译错误,因为您无法重新声明num1num2num3变量。请参阅以下更正这些问题的代码。

#include<iostream>

#include<iomanip>

using namespace std;

void displayLargest(double num1, double num2, double num3)

{
  if (num1 > num2 && num1 > num3) {
    cout << num1;
  } else if (num2 > num1 && num2 > num3) {
    cout << num2;
  }
  if (num3 > num1 && num3 > num2) {
    cout << num3;
  }

}

int main() {
  double num1, num2, num3;
  cout << "Please enter 3 numbers:";
  cin >> num1 >> num2 >> num3;

  displayLargest(num1, num2, num3);
  return 0;
}

这是一个示例输入/输出

./main
Please enter 3 numbers:1 2 3
3
于 2020-03-27T09:27:17.753 回答