如何将2D数组类型char(string)作为函数参数传递?

时间:2018-05-08 08:52:36

标签: c++ arrays string function 2d

我无法编译程序。我的函数参数有什么问题吗?是因为我的数组声明(char类[SIZE] [15])?由于列部分保持字母表的大小,因此我将其设为2D数组。你们可以指出我的代码有什么问题,以及如何将数组作为函数参数传递?对不起,我对这个话题非常陌生。

#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;
    }
}

2 个答案:

答案 0 :(得分:0)

您没有以正确的方式将2D数组传递给函数。阅读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;
    }
}

答案 1 :(得分:0)

固定

&#13;
&#13;
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

PAGE="""\
<html>
description of webpage
</html>
"""

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    output = None

    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with StreamingHandler.output.condition:
                        StreamingHandler.output.condition.wait()
                        frame = StreamingHandler.output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True
    def __init__(self, address, handler, output):
        handler.output = output
        super().__init__(address, handler)

def main():
    with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
            output = StreamingOutput()
            camera.start_recording(output, format='mjpeg')
            try:
                address = ('', 8000)
                server = StreamingServer(address, StreamingHandler, output)
                server.serve_forever()
            except(KeyboardInterrupt):
                camera.stop_recording()

if __name__ == '__main__':
    main()        
&#13;
&#13;
&#13;