Как сохранить результаты условного оператора в строковую переменную?

Это только для практических целей. Я пишу код для налоговой формы, чтобы позволить студентам вычитать свои проценты из кредита в 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;

}

-3

Решение

Здесь немного догадываюсь, но, надеюсь, это будет полезно. Сначала вы можете объявить строку

std::string conditionalString;

Для начала строка будет пустой, т.е. «». Позже вы можете назначить другое значение для строки.

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

И даже позже вы можете использовать строку для любых целей, которые вам нравятся.

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector