【发布时间】:2018-11-22 07:03:08
【问题描述】:
我的程序参数要求我有一个包含在程序过程中输入的变量值的单个格式化字符串。由于涉及的数据量,每个新数据点的换行符将是理想的。
我正在使用 Visual Studio 的 C++ 编译器,并且已经有以下标头:
//preprocessors
#include <iostream>
#include "MortCalc.h"
#include <string>
#include <istream>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
我尝试像这样连接值和字符串片段:
//write info to string
string mortgageInfo =
" Principal Of Loan: $" + mortData.principal + "\n"
+ " Interest Rate: " + mortData.interest + "%\n"
+ " Monthly Payment: $" + monthlyPayment + "\n"
+ " Total Loan Paid: $" + total + "\n"
+ " Total Interest Paid: $" + interestOverLife + "\n"
+ setprecision(0) + fixed + "\n"
+ " Years: " + mortData.term + "\n"
+ " Start Date of Loan: " + mortData.dayStart + "/"
+ mortData.monStart + "/" + mortData.yearStart + "\n"
+ " End Date of Loan: " + mortData.dayEnd + "/"
+ mortData.monEnd + "/" + mortData.yearEnd + "\n";
但我不断收到此错误:“表达式必须具有整数或非范围枚举类型”。
我将这种格式基于 cout 语句的工作原理,并将所有 '
我在正确的轨道上吗?遗漏了一些明显的东西?这能做到吗?
【问题讨论】:
-
您绝对不能将整数添加到字符串...使用
std::to_string()... -
你应该使用字符串流
标签: c++ string visual-c++ c++17