我想从3个数字的最大值到最小值打印三个数字,当我尝试编译此代码时,它会向每个函数参数显示此错误C2065 "function parameter :Undeclared identifier function parameter"
。其他错误是C 2062 type "int" unexpected
。
这是我的代码
#include "stdafx.h"
#include <iostream>
using namespace std;
int max, min;//making global variable of max and min
void numMax(int x, int y, int z);//finding maximum number
void numMin(int x, int y, int z);/finding minimum number
int main()
{
int x, int y, int z;
int middle = 0;
cout << "This program will take 3 number and print them from minimum to maximum" << endl;
cout << "_________________" << endl;
cout << "Pleas enter three number" << endl;
cout << "num1 =";cin >> x;cout << endl << "\n";
cout << "num2 =";cin >> y;cout << endl << "\n";
cout << "num3 =";cin >> z;cout << endl << "\n";
numMax(x, y, z);
numMin(x, y, z);
if (x<max & x>min)
{
middle = x;
}
if (y<max & y>min)
{
middle = y;
}
if (z<max & z>min)
{
middle = z;
}
cout <<"ordered numbers are : "<< min << "\t"<< middle << "\t" <<max ;
return 0;
}
void numMAx(int x, int y, int z)
{
int max;
max = x > y ? x : y;
max = z > max ? z : max;
cout << max;
}
void numMin(int x, int y, int z)
{
int min;
min = x < y ? x : y;
min = min<z ? min : z;
cout << min;
}
首先我定义了我的函数,然后在main函数中我将参数传递给函数参数然后我提到了我的numMax和numMin函数来执行它们的任务。最后我用if
语句来确定中间数。我该怎么办?
答案 0 :(得分:0)
int numMax (int x,int y ,int z)
{
if(x>y && x>z)
return x;
else if (y>x && y>z)
return y;
else
return z;
}
int numMin(int x,int y ,int z)
{
if(x<y && x<z)
return x;
else if (y<x && y<z)
return y;
else
return z;
}
void main()
{
int x,y,z;
int max,min;
clrscr();
cout<<"\n Enter 3 Number: \n";
cout<<"1st Num: ";cin>>x;
cout<<"2nd Num: ";cin>>y;
cout<<"3rd Num: ";cin>>z;
max = numMax(x,y,z);
min = numMin(x,y,z);
if(x<max && x>min)
{
middle=x;
}
else if(y<max && y>min)
{
middle=y;
}
else
middle=z;
cout<<"Number from max to min are: \n "<<numMax(x,y,z)<<" "<<numMin(x,y,z)<<" "<<middle;
}
试试这个