C++ class 관련 질문

조회수 353회

현재 코드 상태가 매우 이상한걸 알지만 프로그래밍 배운지 1달 좀 안되서 양해 부탁드립니다. class 짠것도 솔직히 vs 코드라서 작동이 된거지 리눅스였으면 class 프라이빗 변수를 자꾸 가져오고 할당해서 원래 에러떠야된는 것도 압니다. 하지만 그런거 제쳐두고 제 질문은 "car_rotation 이라는 각도가 왜 자꾸 0 이 되는지 이해가 안됩니다." 무엇이 잘못되고 어떡해 고쳐야 하나요? 감사합니다.

include

include

using namespace std; /* y(car rotation) = (1/12)* x(handle rotation). 45 도 = 1바퀴 반 ( 360+ 180 = 540 도) */

class Car { private: int final_velocity = 0; string gear_lvl = "park"; float handle_rotation; float a = 1; float b = 12; float deg_conv = a / b; float car_rotation = deg_conv * handle_rotation;

public: void ShowCarState(); void Accel(); void Brake(); void Gear(); void menu(); void exit(); void Rotation();

};

void Car::Rotation() { int handle;

cout << endl;
cout << "Current car degree: " << car_rotation << endl;
cout << "Enter the car handle degree: ";
cin >> handle;

if (handle > 540) {
    handle_rotation = 540;

    cout << "Car is going right in " << car_rotation << endl;
    return;
}
else if (handle < -540) {
    handle_rotation = -540;
    cout << "car is going left in " << car_rotation << endl;
    return;
}
else
{
    handle_rotation = handle;

    if (handle < 0) {
        cout << "car is going left in " << car_rotation << endl;
        return;
    }
    else if (handle == 0) {
        car_rotation = 0;
        return;
    }
    else //handle > 0
    {
        cout << "Car is going right in " << car_rotation << endl;
        return;
    }
}

}

void Car::menu() { int choice; do{ cout << " Menu " << endl; cout << "________________" << endl; cout << "1. Gear" << endl; cout << "2. Accelerate" << endl; cout << "3. Brake" << endl; cout << "4. Turn off" << endl; cout << "________________" << endl; cout << "Enter your choice : "; cin >> choice; cout << endl;

    switch (choice)
    {
    case 1:
        Gear();
        break;
    case 2:
        if (gear_lvl == "park")
        {
            cout << "Park Gear cannot move." << endl;
            menu();
        }
        else
        {
            cout << "Current Gear : " << gear_lvl << endl;
            Accel();
            break;
        }
        break;
    case 3:
        Brake();
        break;
    case 4: cout << "Exiting Program";
        exit();
        break;
    }
} while (choice != 4);

}

void Car::Gear() { ShowCarState(); string gear; if (final_velocity != 0) { cout << "Car is moving, cannot change gear" << endl; } else { cout << "Gear Option" << endl; cout << "------------" << endl; cout << "Drive" << endl; cout << "Reverse" << endl; cout << "Park" << endl; cout << "Change the gear to : "; cin >> gear; cout << endl << endl;

    if (gear == "drive") {
        cout << "The gear is now " << gear << " mode." << endl << endl;
        gear_lvl = gear;
    }
    else if (gear == "reverse") {
        cout << "The gear is now " << gear << " mode." << endl << endl;
        gear_lvl = gear;
    }
    else if (gear == "park") {
        cout << "The gear is now " << gear << " mode." << endl << endl;
        gear_lvl = gear;
    }
    else {
        cout << "you entered an improper name" << endl;
        return;
    }
}
menu();

}

void Car::Accel() { ShowCarState(); int acc; int time; cout << endl; cout << "Enter accel and time: "; cin >> acc >> time; Rotation(); if (gear_lvl == "drive") { final_velocity += (acc * time); if (final_velocity < 0) { cout << "Speed: " << -final_velocity << ", backward" << endl; } else { if (final_velocity > 0) { cout << "Speed: " << final_velocity << ", forward" << endl; } else { cout << "Speed: " << final_velocity << endl; } } } else if (gear_lvl == "reverse") { final_velocity -= (acc * time); if (final_velocity < 0) { cout << "Speed: " << -final_velocity << ", backward" << endl; } else { cout << "Speed: " << final_velocity << ", forward" << endl; } } else if (gear_lvl == "park") { final_velocity = 0; cout << "Speed: " << final_velocity << endl; } else return; menu(); }

void Car::Brake() { ShowCarState(); int deacc; int time;

cout << "Enter the brake (deacceleration) and time: ";
cin >> deacc >> time;
if (gear_lvl == "drive") {
    final_velocity -= (deacc * time);
    if (final_velocity <= 0) {
        final_velocity = 0;
        cout << "Speed: " << final_velocity << endl;
    }
    else {
        final_velocity = final_velocity;
        cout << "Speed: " << final_velocity << ", forward" << endl;
    }
    menu();
}
else if (gear_lvl == "reverse") {
    final_velocity += (deacc * time);
    if (final_velocity >= 0) {
        final_velocity = 0;
        cout << "Speed: " << -final_velocity << endl;
    }
    else {
        final_velocity = final_velocity;
        cout << "Speed: " << -final_velocity << ", backward" << endl;
    }
    menu();
}
else if (gear_lvl == "park") {
    final_velocity = 0;
    cout << "Speed: " << final_velocity << endl;
    menu();
}
else
    return;
menu();

} void Car::exit() { return; } void Car::ShowCarState() { cout << " Car State " << endl; cout << "_________________________" << endl; cout << "Gear: " << gear_lvl << endl; cout << "Speed: " << final_velocity << " km/h" << endl; cout << "Car rotate: " << car_rotation << " degree" << endl; cout << "_________________________" << endl << endl; }

int main(void) { Car run; run.menu(); }

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 코드를 돌려보진 못하고 대략 읽어봤는데, handle_rotation = handle; ← 이 라인 주변이 이상합니다. handle_rotation이 두번 다시 참조되지 않는데 왜 handle로 값을 변경하는지? 그 부분 중심으로 원래 구현하려고 하셨던 것을 재검토해 보시면 좋을 거 같네요. 엽토군 2021.6.28 11:51

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)