2013-01-24 1 views
-3

이것은 연습 목적으로 만 사용됩니다. 나는 세금 양식에 대한 코드를 작성하여 학생들이 IRS 1040의 대출금에서 그들의이자를 공제 할 수 있도록하고 있습니다. 정보를 입력하는 사람의 법적 지위에 따라 여러 가지 조건문을 사용합니다.조건문의 결과를 문자열 변수에 저장하려면 어떻게합니까?

조건문의 결과를 문자열 변수에 저장해야합니다. 문자열 변수는 나중에 프로그램에서 사용되어 조건문의 결과에 따라 결과를 출력합니다. 예를 들어

는 C++ HERE IN MY 코드 여기 비트 추측

#include <stdlib.h> // Directive for Pause 
#include <iostream.h> // Directive for Input/Output 
#include <iomanip> // Directive for Input/Output Manipulation 
#include <cmath> // Directive for use of math functions 

using namespace std; 

int main() { 

    int filing_status; 
    double interest_paid; 
    double total_income; 
    double income_adjustments; 
    double adjusted_income; // Declaration statements 
    double amount_limit; 
    const double MAXIMUM_INTEREST_PAID = 2500; 
    const double INCOME_LIMIT_NOT_MARRIED = 60000; 
    const double INCOME_LIMIT_MARRIED_JOINT = 120000; 

    cout << "Student Loan Interest Deduction Worksheet Program" << endl << endl; 

    cout << "1 - Single" << endl; 
    cout << "2 - Married, Filing Jointly" << endl; 
    cout << "3 - Head of Household" << endl; 
    cout << "4 - Qualifying Widow(er)" << endl; 
    cout << "5 - Married, Filing Separately" << endl; 
    cout << "Enter your filing status from above (1-5): "; 
    cin >> filing_status; 
    cout << endl; 

    if (filing_status == 5) { 
     cout << "Student Loan Interest Deduction is not allowed for Married, Filing Separately" << endl << endl; 
     cout << "***Enter 0 on form 1040 line 33" << endl << endl; 

     system("Pause"); 
     return 5; 
    } 
    if (filing_status < 5) 

    cout << "Enter Interest Paid on Student Loans: "; 
    cin >> interest_paid; 
    cout << endl; 

    cout << "Enter Total Income (from 1040 line 22): "; 
    cin >> total_income; 
    cout << endl; 

    cout << "Enter Adjustments to Income (from 1040, lines 23-32): "; 
    cin >> income_adjustments; 
    cout << endl << endl << endl; 

    cout << setw(45) << "Student Loan Interest Deduction Worksheet" << endl << endl; 

    cout << setw(18) << "Filing Status:"; 

    if (filing_status == 1) cout << "Single"; 

    else if (filing_status == 2) 

    cout << setw(48) << "Married, Filing Jointly"; 

    else if (filing_status == 3) cout << "Head of Household"; 

    else(filing_status == 4); 
    cout << "Qualifying Widow(er)"; 

    cout << endl << endl; 

    cout << showpoint << fixed << setprecision(2); 

    cout << "1." << setw(39) << "Total Interest Paid on Loans in 2012:" << setw(25); 

    if (interest_paid < MAXIMUM_INTEREST_PAID) 

    cout << interest_paid; 

    else cout << MAXIMUM_INTEREST_PAID; 

    cout << endl << endl; 

    cout << "2." << setw(35) << "Total Income (from 1040 line 22):" << setw(29) << total_income << endl; 

    cout << "3." << setw(48) << "Adjustments to Income (from 1040 lines 23-32):" << setw(16) << income_adjustments << endl; 

    adjusted_income = total_income - income_adjustments; 

    cout << "4." << setw(18) << "Adjusted Income:" << setw(46) << adjusted_income << endl; 

    cout << "5." << setw(15) << "Income Limit:" << setw(49); 

    if (filing_status == 2) cout << INCOME_LIMIT_MARRIED_JOINT; 

    else cout << INCOME_LIMIT_NOT_MARRIED; 

    cout << endl; 

    if (adjusted_income > INCOME_LIMIT_MARRIED_JOINT) 

    { 
     amount_limit = adjusted_income - INCOME_LIMIT_MARRIED_JOINT; 
     cout << "6." << setw(20) << "Amount over Limit:" << setw(44) << amount_limit; 
    } else cout << endl << endl; 
    cout << "9." << setw(3) << "Student Loan Interest Deduction:"; 

    cout << endl << endl; 
    cout << "***Enter amount from worksheet line 9 on form 1040 line 33"; 

    cout << endl << endl; 

    system("Pause"); 
    return 0; 

} 
+1

예를 들어 위의 어느 부분을 문자열로 저장 하시겠습니까? – naumcho

+0

if (interest_paid llSpectrell

+1

세르지오, SE에 오신 것을 환영합니다. 코드를 읽는 것은 매우 어렵습니다. 필요하지 않더라도 모든 if/else 블록에'{} '를 추가 할 것을 제안 할 수 있습니까? 또한, 내가 (그리고 다른 사람들이 기대하는) 완전히 명확하지 않기 때문에, 당신이 달성하기를 바라는 것을 절대적으로 명백하게하십시오. –

답변

1

은 바라건대이 도움이 될 것이다. 먼저 문자열을 선언 할 수 있습니다.

std::string conditionalString; 

문자열로 시작하려면 문자열이 비어 있어야합니다 (예 : ""). 나중에 문자열에 다른 값을 지정할 수 있습니다.

if (something) { 
    conditionalString = "value #1"; 
} else { 
    conditionalString = "value #2"; 
} 

심지어 나중에 원하는대로 문자열을 사용할 수 있습니다.

std::cout << conditionalString << std::endl;