[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1890
  • Last Modified:

C++ Function

I got error ... can someone help....

here is the code


#include <iostream>

using namespace std;

// prototype for function max3


int max3(int, int,int);

 
// function max3 takes three integer values and returns the largest

int max3(int num1, int num2, int num3)


{


    int maxint; // local variable



 if(n1>n2){
  if(n1>n3) return n1;
  else return n3;
 }
 {
 else{
  if(n2>n3) return n2;
  else return n3;
 
 }
void main()


 {
 cout << "Enter integer value 1 => " ; // ask user to enter three integer values

    cin >> n1;

    cout << "Enter integer value 2 => " ;

    cin >> n2;

    cout << "Enter integer value 3 => " ;

    cin >> n3;

     cout << "The max is " << max3(n1,n2 n3) << endl;  // display the maximum of the entered integers using max3 function
}
 }
0
mhho
Asked:
mhho
  • 3
  • 2
1 Solution
 
jkrCommented:
1st of all you need to declare your variables and return the values correctly from your function. Also, the curly braces should match, e.g.

int max3(int num1, int num2, int num3)


{


    int maxint; // local variable



 if(n1>n2){
  if(n1>n3) maxint = n1;
  else maxint =  n3;
 }

 else{
  if(n2>n3) maxint = n2;
  else maxint = n3;
 
 }

 return maxint;
}
void main()


 {

int n1,n2,n3; // was missing

 cout << "Enter integer value 1 => " ; // ask user to enter three integer values

    cin >> n1;

    cout << "Enter integer value 2 => " ;

    cin >> n2;

    cout << "Enter integer value 3 => " ;

    cin >> n3;

     cout << "The max is " << max3(n1,n2 n3) << endl;  // display the maximum of the entered integers using max3 function

 }
0
 
brettmjohnsonCommented:
You have mis-balanced braces.  The max3() function is missing a closing curly brace, and main() has one too many.  Consider using a programmer's editor that automatically indents code and matches braces/brackets/parenthesis to avoid such obvious errors.


0
 
DarrylshCommented:
In your function's parameter list you have num1, num2, num3 but in the function body you have n1, n2, n3.

The braces match, I think it's just the weird formatting that makes it look like it doesn't.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DarrylshCommented:
oh wait the braces don't match... but still the variable names need to match
0
 
DarrylshCommented:
You are also missing a comma in your last cout statement:
Here is the fixed code

#include <iostream>
using namespace std;

int max3(int n1, int n2, int n3)
{
    int maxint; // local variable
    if(n1>n2)
    {
        if(n1>n3) maxint = n1;
        else maxint =  n3;
    }

    else
    {
        if(n2>n3) maxint = n2;
        else maxint = n3;
    }
    return maxint;
}
int main()
{

    int n1,n2,n3; // was missing
    cout << "Enter integer value 1 => " ; // ask user to enter three integer values
    cin >> n1;
    cout << "Enter integer value 2 => " ;
    cin >> n2;
    cout << "Enter integer value 3 => " ;
    cin >> n3;
    cout << "The max is " << max3(n1,n2, n3) << endl;  // display the maximum of the entered integers using max3 function

}
0
 
jkrCommented:
???
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now