为什么在这个switch语句之后没有执行代码?

时间:2018-04-24 02:13:05

标签: c++ switch-statement

由于某些原因,在main()中tulip.flowerHealth()之后没有执行任何操作。我是否对开关盒做错了或者我是否错误地创建了对象?

的main.cpp

#include <iostream>
#include "flower.h"

using namespace std;

int main()
{
    flower tulip;
    int i = 1;
    cout << "Press 1 to water. Press 2 to do nothing." << endl;

    while( tulip.curWater() > 0 && tulip.curWater() < 6 ){
        cin >> i;
        if ( i == 1){
            tulip.water();
        } else {
            tulip.dry();
        }
        cout << "Flower water level is: " << tulip.curWater() << " " << tulip.flowerHealth() << endl;
    } // end of while loop

    cout << "Your flower has died. :(" << endl;

    return 0;
}

flower.h

#ifndef FIRSTPROJ_FLOWER_H
#define FIRSTPROJ_FLOWER_H

#include <string>

using namespace std;

class flower
{
public:
    string flowerHealth();
    int curWater();
    void water();
    void dry();
    int waterLvl = 1;
};

#endif //FIRSTPROJ_FLOWER_H

flower.cpp

#include "flower.h"

int flower::curWater(){
    return waterLvl;
}

void flower::water(){
    waterLvl++;
};

void flower::dry() {
    waterLvl--;
};

string flower::flowerHealth(){
    switch(curWater()){
        case 1 : return "Flower desperately needs water!";
        case 2 : return "Flower is doing alright.";
        case 3 : return "Optimal water for the flower to grow.";
        case 4 : return "Flower is doing alright.";
        case 5 : return "Flower is being over watered!";
    }
}

0 个答案:

没有答案