2013-02-14 3 views
0

프로젝트의 방향은 다음과 같은 상태 : ". 활동 수준, 입력이 유효하지 않은 경우, 메시지를 인쇄하고 당신은 그들이 앉아있는 것을 가정하는 사용자에게"잘못된/잘못된 입력을 입력하면 어떻게 기본값을 설정할 수 있습니까?

다음

현재 코드입니다, 사용자가 SA 또는 sa로 입력 할 수있는 "Sedentary"으로 기본 설정하려고합니다. 잘못된 입력에 대한 기본값은 프로그램이 기본적으로 좌식으로 설정되어 있다고 가정하고 올바른 방향에 있는지 여부에 대해 조금 혼란스러워합니다.

else if (gender == 'F' || gender == 'f') { 
    cout << "Please enter your Height in inches. (You may include a decimal) "; 
    cin >> height; 

    cout << "Please enter your Weight in pounds as a whole number. "; 
    cin >> weight; 

    cout << "Please enter your Age in years. "; 
    cin >> age; 
    cout << endl; 

    if (activity_level = 'SA' || activity_level = 'sa' || activity_level = 'LA' || activity_level = 'la' || 
     activity_level = 'MA' || activity_level = 'ma' || activity_level = 'VA' || activity_level = 'va') { 
     cout << "Please enter your activity level." << endl << endl; 
     cout << "You may enter one of the following choices." << endl << endl; 
     cout << "Sedentary (Little or no exercise) \"SA\" or \"sa\" " << endl; 
     cout << "Lightly Active (Light exercise/sports 1-3 days a week) \"LA\" or \"la\" " << endl; 
     cout << "Moderately Active (Moderate exercise/sports 3-5 days a week) \"MA\" or \"ma\" " << endl; 
     cout << "Very Active (Hard exercise/sports 6-7 days a week) \"VA\" or \"va\" " << endl << endl; 
     cout << "Enter your activity level now. "; 
     cin >> activity_level; 
     cout << endl; 
    } 
    // Output for the message to defualt to Sedentary if you do not select activity level throught he proper input. 
    else { 
     activity_level = 
     cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise. " 
    } 
} 

기본적으로 내가하고있는 일이 무엇인지 궁금합니다. else if 진술서 내에서 또 다른 if 문을 사용하면 문제가 해결되며, 현재 설정 한 방식으로 필요한 결과가 나오는지 궁금합니다.

+1

'activity_level = 'SA'는 비교가 아닌 'activity_level'에 * 할당 *을합니다. 'activity_level == "SA"'는 비교 일 것입니다. –

+0

사용자가 입력하기 전에 activity_level을 확인하는 이유는 무엇입니까? –

+0

좋은 점, 나는 그것을 게시 한 후에 적어도 실수를 보았습니다. – user2072795

답변

0

다음이 작업을 수행 할 수 있습니다 : 그것은 문자열이기 때문에

//Assume activity_level has had something assigned to it, to compare to. 

    if (activity_level == "SA" || activity_level == "sa" || activity_level == "LA" || activity_level == "la" || 
     activity_level == "MA" || activity_level == "ma" || activity_level == "VA" || activity_level == "va") 
    { 
     //Do stuff if input is valid 
    } 
    // Output for the message to defualt to Sedentary if you do not select activity level throught he proper input. 
    else 
    { 
     activity_level = "SA"; 
     std::cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise."; 
    } 

또한 작은 따옴표 뭔가는 하나의 char 수 있습니다, 어떤 다른, 그것의 주위에 따옴표를 필요로한다.

+0

도움에 감사드립니다. 고맙습니다. – user2072795

0

변수에`activity_level '값이 지정되어 있는지 확인해야합니다. 또한 비교를 위해 ==를 사용해야합니다. 그리고 당신은! 연산자를 사용하여 조건을 무효화합니다. 당신이 SA로 기본 설정하려면

관련 문제