如何将条件语句的结果存储到字符串变量中?

时间:2013-01-24 21:05:49

标签: c++ string variables conditional-statements

这仅用于练习目的。我正在编写一份税表代码,允许学生从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 个答案:

答案 0 :(得分:1)

在这里猜一下,但希望这会有所帮助。首先,您可以声明字符串

std::string conditionalString;

首先,字符串将为空,即“”。稍后您可以为字符串分配不同的值。

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

甚至以后你可以将字符串用于你喜欢的任何目的。

std::cout << conditionalString << std::endl;
相关问题