如何将二维数组类型 char(字符串)作为函数参数传递?

Posted

技术标签:

【中文标题】如何将二维数组类型 char(字符串)作为函数参数传递?【英文标题】:how to pass 2D array type char (string) as function parameter? 【发布时间】:2018-05-08 08:52:35 【问题描述】:

我无法编译程序。我的函数参数有什么问题吗?是因为我的数组声明(char category[SIZE][15])吗?我把它做成二维数组,因为列部分包含字母表的大小。你们能指出我的代码有什么问题以及如何将数组作为函数参数传递吗?对不起,我对这个话题完全陌生。

#include<iostream>
#include<string.h>
using namespace std;
const int COLSIZE=3;
const int NAMESIZE=30;
void inputPersonalData(int, long[],int,char[],float[],float[]);
void calcBMI(int,float[],float[],float[]);
void bmiCategory(int,float[],char[]);
void displayPersonalData(int,long[],char[],float[],float[],float[],char[]);
void inputCal(int,char[],long[],float[][COLSIZE],char[]);
int main()

    int SIZE;
    long ID[SIZE];
    char name[NAMESIZE];
    float weight[SIZE];
    float height[SIZE];
    float BMI[SIZE];
    char category[SIZE][15];
    char meal[COLSIZE][10];
    float calIntake[SIZE][COLSIZE];

    cout<<"Enter the number of models: ";
    cin>>SIZE;

    inputPersonalData(SIZE,ID,NAMESIZE,name,weight,height);
    calcBMI(SIZE,weight,height,BMI);
    bmiCategory(SIZE,BMI,category);
    displayPersonalData(SIZE,ID,name,weight,height,BMI,category);

    strcpy(meal[0],"BREAKFAST");
    strcpy(meal[1],"LUNCH");
    strcpy(meal[2],"DINNER");
    inputCal(SIZE,name,ID,calIntake,meal);

    return 0;

void inputPersonalData(int rowSize, long id[],int nameSize, char nama[],float berat[],float tinggi[])

    for(int i=0;i<rowSize;i++)
    
        cout<<"\nModel "<<i+1<<"'s information\n\n";
        cout<<"ID: ";
        cin>>id[i];
        cout<<"Name: ";
        cin>>ws;
        cin.getline(nama,nameSize);
        cout<<"Weight in kg: ";
        cin>>berat[i];
        cout<<"Height in m: ";
        cin>>tinggi[i];
        cout<<endl<<"***************************************"<<endl;
    

void calcBMI(int rowSize,float berat[],float tinggi[],float bmi[])


    for(int j=0;j<rowSize;j++)
    
        bmi[j]=(berat[j]/tinggi[j])/tinggi[j];
    


void bmiCategory(int rowSize,float bmi[],char category[])

    for(int k=0;k<rowSize;k++)
    
        if(bmi[k]>25)
            strcpy(category[k],"OVERWEIGHT");
        else if(bmi[k]>18)
            strcpy(category[k],"NORMAL");
        else
            strcpy(category[k],"UNDERWEIGHT");
    

void inputCal(int rowSize,char nama[],long id[],float calorie[][COLSIZE],char mealName[])

    for(int row=0;row<rowSize;row++)
    
        cout<<"Model "<<row+1<<" ("<<nama[row]<<", ID: )"<<id[row]<<" DAILY CALORIE INTAKE:\n";
        for(int col=0;col<COLSIZE;col++)
        
            cout<<"Calorie intake for "<<mealName[col]<<": ";
            calorie[row][col];
        
    

void displayPersonalData(int rowSize,long id[],char nama[],float berat[],float tinggi[],float bmi[],char category[])

    cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\tCategory\n";
    for(int m=0;m<rowSize;m++)
    
        cout<<m+1<<"\t"<<id[m]<<"\t"<<nama[m]<<"\t"<<berat[m]<<"\t"<<tinggi[m]<<"\t"<<bmi[m]<<"\t"<<category[m]<<endl;
    

【问题讨论】:

那是因为你有一个同名的局部变量bmiCategory 哦哦哦好的2谢谢先生 首先你声明你的数组未定义的 SIZE 然后你把 SIZE 放在输入上,更不用说这个方法需要扩展 我对未定义的 SIZE 没有任何问题,因为我可以编译与未定义的 SIZE 相关的代码的其他部分,直到我包含 2D 数组类型 char 函数。 【参考方案1】:

您没有以正确的方式将二维数组传递给函数。阅读this topic 了解更多详情。顺便说一下,这是您的代码,经过一些修改:

#include<iostream>
#include<string.h>
using namespace std;
const int COLSIZE=3;
const int NAMESIZE=30;
void inputPersonalData(int, long[],int,char[],float[],float[]);
void calcBMI(int,float[],float[],float[]);
void bmiCategory(int,float[],char[][15]);
void displayPersonalData(int,long[],char[],float[],float[],float[],char[][15]);
void inputCal(int,char[],long[],float[][COLSIZE],char[][10]);
int main()

    int SIZE;
    long ID[SIZE];
    char name[NAMESIZE];
    float weight[SIZE];
    float height[SIZE];
    float BMI[SIZE];
    char category[SIZE][15];
    char meal[COLSIZE][10];
    float calIntake[SIZE][COLSIZE];

    cout<<"Enter the number of models: ";
    cin>>SIZE;

    inputPersonalData(SIZE,ID,NAMESIZE,name,weight,height);
    calcBMI(SIZE,weight,height,BMI);
    bmiCategory(SIZE,BMI,category);
    displayPersonalData(SIZE,ID,name,weight,height,BMI,category);

    strcpy(meal[0],"BREAKFAST");
    strcpy(meal[1],"LUNCH");
    strcpy(meal[2],"DINNER");
    inputCal(SIZE,name,ID,calIntake,meal);

    return 0;

void inputPersonalData(int rowSize, long id[],int nameSize, char nama[],float berat[],float tinggi[])

    for(int i=0;i<rowSize;i++)
    
        cout<<"\nModel "<<i+1<<"'s information\n\n";
        cout<<"ID: ";
        cin>>id[i];
        cout<<"Name: ";
        cin>>ws;
        cin.getline(nama,nameSize);
        cout<<"Weight in kg: ";
        cin>>berat[i];
        cout<<"Height in m: ";
        cin>>tinggi[i];
        cout<<endl<<"***************************************"<<endl;
    

void calcBMI(int rowSize,float berat[],float tinggi[],float bmi[])


    for(int j=0;j<rowSize;j++)
    
        bmi[j]=(berat[j]/tinggi[j])/tinggi[j];
    


void bmiCategory(int rowSize,float bmi[],char category[][15])

    for(int k=0;k<rowSize;k++)
    
        if(bmi[k]>25)
            strcpy(category[k],"OVERWEIGHT");
        else if(bmi[k]>18)
            strcpy(category[k],"NORMAL");
        else
            strcpy(category[k],"UNDERWEIGHT");
    

void inputCal(int rowSize,char nama[],long id[],float calorie[][COLSIZE],char mealName[][10])

    for(int row=0;row<rowSize;row++)
    
        cout<<"Model "<<row+1<<" ("<<nama[row]<<", ID: )"<<id[row]<<" DAILY CALORIE INTAKE:\n";
        for(int col=0;col<COLSIZE;col++)
        
            cout<<"Calorie intake for "<<mealName[col]<<": ";
            calorie[row][col];
        
    

void displayPersonalData(int rowSize,long id[],char nama[],float berat[],float tinggi[],float bmi[],char category[][15])

    cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\tCategory\n";
    for(int m=0;m<rowSize;m++)
    
        cout<<m+1<<"\t"<<id[m]<<"\t"<<nama[m]<<"\t"<<berat[m]<<"\t"<<tinggi[m]<<"\t"<<bmi[m]<<"\t"<<category[m]<<endl;
    

【讨论】:

感谢您的链接和修改。现在我也必须修复其他部分。无论如何,非常感谢阿里。【参考方案2】:

固定

#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
const int COLSIZE=3;
const int SIZE=15;
const int NAMESIZE=30;
void inputPersonalData(int, long[],char[][NAMESIZE],float[],float[]);
void calcBMI(int,float[],float[],float[]);
void bmiCategory(int,float[],char[][15]);
void displayPersonalData(int,long[],char[][NAMESIZE],float[],float[],float[],char[][15]);
void inputCal(int,char[][NAMESIZE],long[],float[][COLSIZE],char[][10]);
void totalCal(int,float[][COLSIZE],float[]);
void idealCal(int rowSize,float bmi[],float idealCal[]);
void condition(int,float [],float [],long [],char[][NAMESIZE]);
void searchId(int,long[],char[][NAMESIZE],float[],float[],float[],char[][15]);
int main()

    int numdata;
    long ID[SIZE];
    char name[SIZE][NAMESIZE];
    float weight[SIZE];
    float height[SIZE];
    float BMI[SIZE];
    char category[SIZE][15];
    char meal[COLSIZE][10];
    float calIntake[SIZE][COLSIZE];
    float totalCalIntake[SIZE];
    float idealCalIntake[SIZE];

    cout<<"Enter the number of models: ";
    cin>>numdata; //assume user input lower than size

    inputPersonalData(numdata,ID,name,weight,height);
    calcBMI(numdata,weight,height,BMI);
    bmiCategory(numdata,BMI,category);
    displayPersonalData(numdata,ID,name,weight,height,BMI,category);

    strcpy(meal[0],"BREAKFAST");
    strcpy(meal[1],"LUNCH");
    strcpy(meal[2],"DINNER");
    cout<<"\nDAILY CALORIE INTAKE for:\n";
    inputCal(numdata,name,ID,calIntake,meal);
    totalCal(numdata,calIntake,totalCalIntake);
    idealCal(numdata,BMI,idealCalIntake);
    condition(numdata,totalCalIntake,idealCalIntake,ID,name);
    searchId(numdata,ID,name,weight,height,BMI,category);

    return 0;

void inputPersonalData(int rowSize, long id[], char nama[][NAMESIZE],float berat[],float tinggi[])

    for(int i=0;i<rowSize;i++)
    
        cout<<"\nModel "<<i+1<<"'s information\n\n";
        cout<<"ID: ";
        cin>>id[i];
        cout<<"Name: ";
        cin>>ws;
        cin.getline(nama[i],NAMESIZE);
        cout<<"Weight in kg: ";
        cin>>berat[i];
        cout<<"Height in m: ";
        cin>>tinggi[i];
        cout<<endl<<"***************************************"<<endl;
    

void calcBMI(int rowSize,float berat[],float tinggi[],float bmi[])


    for(int j=0;j<rowSize;j++)
    
        bmi[j]=(berat[j]/tinggi[j])/tinggi[j];
    


void bmiCategory(int rowSize,float bmi[],char category[][15])

    for(int k=0;k<rowSize;k++)
    
        if(bmi[k]>25.0)
            strcpy(category[k],"OVERWEIGHT");
        else if(bmi[k]>18.0)
            strcpy(category[k],"NORMAL");
        else
            strcpy(category[k],"UNDERWEIGHT");
    

void displayPersonalData(int rowSize,long id[],char nama[][NAMESIZE],float berat[],float tinggi[],float bmi[],char category[][15])

	cout<<"\n****************************************************************************************************************\n";
    cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
    for(int m=0;m<rowSize;m++)
    
        cout<<m+1<<"\t"<<id[m]<<"\t"<<nama[m]<<"\t\t"<<berat[m]<<"\t\t"<<tinggi[m]<<"\t\t"<<bmi[m]<<"\t\t"<<category[m]<<endl;
    

void inputCal(int rowSize,char nama[][NAMESIZE],long id[],float calorie[][COLSIZE],char mealName[][10])

    for(int row=0;row<rowSize;row++)
    
        cout<<"\nModel "<<row+1<<" ("<<nama[row]<<", ID: "<<id[row]<<") \n";
        for(int col=0;col<COLSIZE;col++)
        
            cout<<"Calorie intake for "<<mealName[col]<<": ";
            cin>>calorie[row][col];
        
    

void totalCal(int rowSize,float calorie[][COLSIZE],float totalCalorie[])

	for(int p=0;p<rowSize;p++)
	
		totalCalorie[p]=0;
		for(int q=0;q<COLSIZE;q++)
		
			totalCalorie[p]=totalCalorie[p]+calorie[p][q];
		
	

void idealCal(int rowSize,float bmi[],float idealCal[])

	for(int n=0;n<rowSize;n++)
	
		if(bmi[n]>25.0)
		
			idealCal[n]=1500;
		
		else if(bmi[n]>18.0)
		
			idealCal[n]=2000;
		
		else
		
			idealCal[n]=2500;
		
	

void condition(int rowSize,float totalCalorie[],float idealCal[],long id[],char nama[][NAMESIZE])

	float minuteXercise[rowSize];
	for(int r=0;r<rowSize;r++)
	
		if(totalCalorie[r]>idealCal[r])
		
			minuteXercise[r]=(totalCalorie[r]-idealCal[r])/15.95;
			cout<<fixed<<setprecision(2);
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<") need to excercise for "<<minuteXercise[r]<<" minutes to burn excess calorie.\n";
		
		else if(totalCalorie[r]<idealCal[r])
		
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<") need to add "<<idealCal[r]-totalCalorie[r]<<" more calories to gain more weight.\n";
		
		else
		
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<")'s calorie intake are perfect.\n";
		
	

void searchId(int rowSize,long id[],char nama[][NAMESIZE],float berat[],float tinggi[],float bmi[],char category[][15])

	long searchModel;
	bool foundId=false;
	cout<<"\nEnter ID to be search: ";
	cin>>searchModel;
	for(int s=0;s<rowSize;s++)
	
		if(searchModel==id[s])
		
			foundId=true;
			cout<<"\n****************************************************************************************************************\n";
    		cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
    		cout<<s+1<<"\t"<<id[s]<<"\t"<<nama[s]<<"\t\t"<<berat[s]<<"\t\t"<<tinggi[s]<<"\t\t"<<bmi[s]<<"\t\t"<<category[s]<<endl;
		
	
	while(!foundId)
	
		cout<<"\nThe ID is not in our system!\n";
		cout<<"Enter ID to be search: ";
		cin>>searchModel;
		for(int s=0;s<rowSize;s++)
		
			if(searchModel==id[s])
			
				foundId=true;
				cout<<"\n****************************************************************************************************************\n";
   				cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
   				cout<<s+1<<"\t"<<id[s]<<"\t"<<nama[s]<<"\t\t"<<berat[s]<<"\t\t"<<tinggi[s]<<"\t\t"<<bmi[s]<<"\t\t"<<category[s]<<endl;
			
		
	

【讨论】:

以上是关于如何将二维数组类型 char(字符串)作为函数参数传递?的主要内容,如果未能解决你的问题,请参考以下文章

C-二维数组作为函数参数, 字符数组

如何将char类型数组转换为unicode字符串

C++中,如何讲CHAR数组转换成FLOAT型数据。

如何将二维数组作为函数参数

如何将String类的字符串 赋值给Char数组

如何将String类的字符串 赋值给Char数组