2013-01-24 37 views
-3

这仅用于练习目的。我正在编写税务表格的代码,以便学生从IRS 1040中的贷款中扣除他们的利息。根据输入信息的人的法律状态,我使用了几种有条件的陈述。如何将条件语句的结果存储到字符串变量中?

我需要将条件语句的结果存储到字符串变量中。字符串变量稍后将在程序中用于根据条件语句的结果输​​出结果。例如:

这里是我的代码在C++中:

#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; 
相关问题