菜单类型程序

时间:2015-05-20 13:36:26

标签: c function menu linked-list structure

我有一个编写程序来支持C中的美术馆。它必须是使用列表的基于菜单的程序。我写了菜单的第一个功能,我需要一些帮助来编写其他三个。所以我有一个独特的绘画代码结构,作者的名字,绘画的名称,价格,绘画的年份。我必须使用唯一代码创建一个删除绘画的功能,打印出每个绘画的所有信息,并使用所述代码再次修改绘画。数据必须使用链表进入动态类型结构。到目前为止这是该计划。

   public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';

            //redirect to a 404 page
            $this->_redirect('/error404');
            break;

    }
    $this->view->exception = $errors->exception;
    $this->view->request   = $errors->request;
}

1 个答案:

答案 0 :(得分:0)

第一个问题:您缺少main()函数的右大括号(})。 (但我相信你知道的)

结构大小错误的原因是 您试图在struct painting函数中创建void addPainting()的实例,当它是在main函数中使用局部作用域创建的,因此 对函数 不可见。如果您想以这种方式使用它,请创建具有全局范围的struct painting

这将构建(并运行) ,但仅针对定义的函数,其他内容已被注释掉。还有其他问题需要解决或询问。

EDITED 修复scanf()语句,显示使用fopen()/ fclose(),使用sprintf()/ fputs()创建和编写字符串。 。

void addPainting(void);
//void erasePainting(void);
//void printData(void);
//void modifyData(void);

typedef    struct //created with global scope, visible to all functions
{
    char code[20];
    char name[50];
    char paintingName[50];
    double price;
    int year;

}PAINTING;

PAINTING painting[100];//array of instance of PAINTING

#define PATH "C:\\play\\painting.txt"  //edit to your need


int main()
{

    int choice;
    do
    {
        printf("Menu\n");
        printf("To add a painting press 1.\n");
        printf("To erase a painting press 2.\n");
        printf("To print data for all paintings by authors alphabetically press 3.\n");
        printf ("To modify data for a painting press 4.\n");
        printf("To exit the program press 5.\n");
        scanf("%d",&choice);

        switch(choice)
        {
           case 1:
            {
                addPainting();
                break;
            }
            case 2:
            {
                //erasePainting();
                break;
            }
            case 3:
            {
                //printData();
                break;
            }
            case 4:
            {
                //modifyData();
                break;
            }

            default: printf ("Wrong choice. Try again\n");
                break;
        }
    }while (choice !=5);
}

void addPainting(void)
{
    FILE *fp;
    char stringToWrite[80];
    //Note: this function could be prototyped with an int argument
    //        to be used as an index for the array arguments of your
    //        structure.  Currently, the indexes are hard-coded to 0, 


    printf("Enter code:");
    //scanf("%s", &painting[0].code); 
    scanf("%s", painting[0].code); //& not needed for char array (char *) et. al.
    printf("Enter the name of the author:");
    scanf("%s", painting[0].name);
    printf("Enter the name of the painting:");
    scanf("%s", painting[0].paintingName);
    printf("Enter price:");
    scanf("%lf", &painting[0].price);
    printf("Enter the year of creation:");
    scanf("%d", &painting[0].year);
    fp = fopen (PATH, "a");//open for create/append text file (not write binary, "wb")

    if(fp)
    {
        sprintf(stringToWrite, "Painting Code is: %s\n", painting[0].code);
        fputs(stringToWrite, fp);
        // do others same way...
        //...
        fclose(fp);
    }
}