程序无缘无故结束(C语言)

时间:2015-04-17 21:12:30

标签: c

我是用C语言编程的新手,我正在尝试做一个简单的洗碗机。我有简单的代码填充printf,scanf和开关。当我在程序选择中按1-3时,代码工作正常,但是当我按下4时,它会突然下降。

我看不到函数degree_70与其他函数不同(quick,eco,degree_65)

你们可以建议并帮助我吗?

/////////////////////////////////////////////////////////
// Includes
/////////////////////////////////////////////////////////
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/////////////////////////////////////////////////////////
// Types
/////////////////////////////////////////////////////////
typedef enum {
    SEM_STATES_TURNING_ON,
    SEM_STATES_STAND_BY,
    SEM_STATES_WORKING,
    SEM_STATES_SELF_CLEAN,
    SEM_STATES_BREAK,
    SEM_STATES_TURN_OFF,
    SEM_STATES_QUICK,
    SEM_STATES_ECO,
    SEM_STATES_65,
    SEM_STATES_70,
} SEM_STATES;
/////////////////////////////////////////////////////////
// Global variables
/////////////////////////////////////////////////////////
SEM_STATES status;
int tablet = 10;
char o;
int p;
/////////////////////////////////////////////////////////
// Function prototypes
/////////////////////////////////////////////////////////
void turning_on(void);
void stand_by(void);
void working(void);
void self_clean(void);
void quick_state(void);
void eco(void);
void degree_65(void);
void degree_70(void);
void break_st(void);
void turn_off(void);

int _tmain(int argc, _TCHAR* argv[])
{
    status = SEM_STATES_TURNING_ON;
    while (1)
    {
        /////////////////////////////////////////////////////////
        // Main loop
        /////////////////////////////////////////////////////////
        switch (status)
        {
        case SEM_STATES_TURNING_ON:
            turning_on();
            break;
        case SEM_STATES_STAND_BY:
            stand_by();
            break;
        case SEM_STATES_WORKING:
            working();
            break;
        case SEM_STATES_SELF_CLEAN:
            self_clean();
            break;
        case SEM_STATES_QUICK:
            quick_state();
            break;
        case SEM_STATES_ECO:
            eco();
            break;
        case SEM_STATES_65:
            degree_65();
            break;
        case SEM_STATES_70:
            degree_70();
            break;
        case SEM_STATES_TURN_OFF:
            turn_off();
            return 0;
        default:
            break_st();
            break;
        }
    }
    return 0;
}

void turning_on(void)
{
        printf("\n\t\t::::::::::::::::::: TURNING ON :::::::::::::::::::\n\n");
        status = SEM_STATES_STAND_BY;
}

void stand_by(void)
{
    printf("\n\t\t::::::::::::::::::: STAND BY :::::::::::::::::::\n\n");
    printf("\t\tDo you want to continue [Y] or turn off? [N]\n");
    scanf_s("%c%*c", &o);

    if ((o == 'Y') || (o == 'y')){
        status = SEM_STATES_WORKING;}
    else{
        status = SEM_STATES_TURN_OFF;}
}

void turn_off(void)
{
    printf("\n\t\t::::::::::::::::::: TURNING OFF :::::::::::::::::::\n\n");
    system("pause");
    system("cls");
}

void working(void)
{
    printf("\t\tPlease push 1 - 4 to select the wash program\n");
    printf("\t\t 1. QUICK WASH\n");
    printf("\t\t 2. ECONOMIC WASH\n");
    printf("\t\t 3. 65\xf8\C DEGREES WASH\n");
    printf("\t\t 4. 70\xf8\C\ DEGREES WASH\n");
    scanf_s("%d%*c", &p);
    tablet--;
    switch (p){
    case 1:
        printf("\t\t You pushed: %d - QUICK WASH \n\n", p);

        if (tablet == 0) {
            printf("\t\tNo more tablets! Please put in the new one!");
            status = SEM_STATES_TURN_OFF;
        }    
        status = SEM_STATES_QUICK;
        break;
    case 2:
        printf("\t\t You pushed: %d - ECONOMIC WASH \n\n", p);    
        if (tablet = 0) {
            printf("\t\tNo more tablets! Please put in the new one!");
            status = SEM_STATES_TURN_OFF; 
        }    
        status = SEM_STATES_ECO;
        break;
    case 3:
        printf("\t\t You pushed: %d - 65\xf8\C DEGREES WASH \n\n", p);

        if (tablet == 0) {
            printf("\t\tNo more tablets! Please put in the new one!");
            status = SEM_STATES_TURN_OFF; 
        }    
        status = SEM_STATES_65;
        break;
    case 4:
        printf("\t\t You pushed: %d - 70\xf8\C\ DEGRËES WASH \n\n", p);    
        if (tablet == 0) {
            printf("\t\tNo more tablets! Please put in the new one!");
            status = SEM_STATES_TURN_OFF;
        }    
        status = SEM_STATES_70;
    default:
        printf("\n You pushed a wrong button!\n");
        status = SEM_STATES_BREAK;
        break;
    }
}


void break_st(void)
{
    tablet++;
    status = SEM_STATES_WORKING;
}

void self_clean(void)
{
    printf("\n\t\t::: SELF CLEANING ");
    printf("::: SELF CLEANING DONE \n\n");
    status = SEM_STATES_STAND_BY;
    system("pause");
    system("cls");
}

void quick_state(void)
{
    printf("\n\t\t::: SAMPOO :::");
    printf(" WASHING :::");
    printf(" DONE :::");
    printf(" PLEASE REMOVE DISHES :::");
    status = SEM_STATES_SELF_CLEAN;
}


void eco(void)
{
    printf("\n\t\t::: SAMPOO :::");
    printf(" ECO WASHING & WATER FILTERING:::");
    printf(" DONE :::");
    printf(" PLEASE REMOVE DISHES :::");
    status = SEM_STATES_SELF_CLEAN;
}

void degree_65(void)
{
    printf("\n\t\t::: PRE WASHING :::");
    printf(" SHAMPOO :::");
    printf(" WASHING :::");
    printf(" DRYING :::");
    printf(" PLEASE REMOVE DISHES :::");
    status = SEM_STATES_SELF_CLEAN;
}

void degree_70(void)
{
    printf("\n\t\t::: PRE WASHING :::");
    printf(" SHAMPOO :::");
    printf(" WASHING :::");
    printf(" DRYING :::");
    printf(" PLEASE REMOVE DISHES :::");
    status = SEM_STATES_SELF_CLEAN;
}

2 个答案:

答案 0 :(得分:0)

working中的案例4没有中断,因此会打印&#34;按下错误的按钮&#34;信息。我认为这就是你的意思&#34;突然下降&#34;

答案 1 :(得分:0)

{1}}中缺少break;

working