c++ - 值未显示,并遇到异常。是什么赋予了?
问题描述
我正在为一个项目制作储蓄工作表。它从文件中提取值,并计算一定时期内的新总金额,其中包含每月的回报率和贡献。调试它有点工作,但我的值没有显示在输出部分。只需运行它(F5,我使用的是 Visual C++)就会引发异常 0x76FE9862。我已经为此工作了好几天,没有真正的解决方案,所以非常感谢任何帮助。谢谢!
重要提示:文件 'bucky' 具有以下值:
4000
12
15
37
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;
class savings {
double balance, periods, ROI, values[4];
string filename = "ROI.txt";
ifstream bucky;
char word[50];
char graph[65];
int count = 0;
vector<double> cont;
vector<double> saving;
vector<double> total;
double contr, save, tot;
double findsum(vector<double> &vect, int period) {
double total = 0;
for (int i = 0; i < period; i++) {
total += vect.at(i);
}
return total;
}
class stoc {
bool dec = false;
int dc = 0, count = 0;
string holder;
public:
void flen() {
while (dc < 2) {
if (str[count] == 46)
dec = true;
if (dec)
dc++;
count++;
}
if (str[count + 1] > 4, str[count + 1] < 10) {
str[count]++;
}
str.erase((count + 1), string::npos);
lengt = str.length();
}
string str;
int lengt;
};
stoc per, scont, ssave, ttot; //Reformatting these values to output as a graph
double tcont, tsave;
void addto(int count, stoc value) {
graph[count] = value.str[count];
cout << graph[count];
}
void makerow(int i) {
cout << endl;
per.str = to_string(i + 1) + ":";
if (i == (periods + 1)) {
per.str = "TOTALS:";
cont.push_back(findsum(cont, periods));
saving.push_back(findsum(saving, periods));
total.push_back(total.at(i - 1));
}
scont.str = "$" + to_string(cont.at(i));
ssave.str = "$" + to_string(saving.at(i));
ttot.str = "$" + to_string(total.at(i));
scont.flen();
per.lengt = per.str.length();
ssave.flen();
ttot.flen();
int limit[4] = { 0,14, 35, 53 }; //Marks when values start
bool limon = false;
int length[4] = { per.lengt, scont.lengt, ssave.lengt, ttot.lengt }; //How much space each value takes up
int countlim, countlen, count;
countlim = countlen = count = 0;
countlen = -1;
tcont = totals(cont);
tsave = totals(saving);
while (graph[count] != '\0') {
for (countlim = 0; countlim < 4; countlim++) {
if (count == limit[countlim]) {
limon = true;
countlen++;
}
}
if (limon) {
for (int x = 0; x < length[countlen]; x++) {
switch (countlen) {
case 0: addto(count, per);
case 1: addto(count, scont);
case 2: addto(count, ssave);
case 3: addto(count, ttot);
}
}
limon = false;
}
else {
cout << " ";
}
}
}
double totals(vector<double> value) {
double total = findsum(value, periods);
return total;
}
public:
void getinfo() {
bucky.open(filename);
if (!bucky.is_open())
exit(EXIT_FAILURE);
while (bucky.good()) {
bucky >> word;
values[count] = atoi(word);
count++;
}
balance = values[0];
periods = values[1];
ROI = values[2] / 100;
contr = values[3];
}
void findans() {
cout << setprecision(2) << fixed;
cout << "Initial Balance: $" << balance << endl;
cout << "Rate of Return: " << ROI * 100 << "%\n";
for (int i = 0; i < (periods); i++) {
//cout << "What are your contributions for period " << i + 1 << "?\n";
//cin >> contr;
cont.push_back(contr);
if (i == 0) {
//saving.push_back(save);
save = ROI * (balance + cont.at(i));
saving.push_back(save);
//total.push_back(tot);
tot = (balance + saving.at(i) + cont.at(i));
total.push_back(tot);
}
else {
//saving.push_back(save);
save = ROI * (total.at(i - 1) + cont.at(i));
saving.push_back(save);
//total.push_back(tot);
tot = total.at(i - 1) + cont.at(i) + saving.at(i);
total.push_back(tot);
}
//cout << "Contributions for Period " << i + 1 << ": $" << cont.at(i) << endl;
//cout << "Savings for Period " << i + 1 << ": $" << saving.at(i) << endl;
//cout << "Total for Period " << i + 1 << ": $" << total.at(i) << endl;
//cout << endl;
}
//cout << setprecision(2) << fixed;
//cout << "Total Contributions: $" << findsum(cont, periods) << endl;
//cout << "Total Savings: $" << findsum(saving, periods) << endl;
//cout << "Final Account Balance: $" << total.at(periods - 1) << endl;
}
void showgraph() {
cout << setprecision(2) << fixed;
cout << "-----------SAVINGS CALCULATOR----------\n";
cout << "----Balance: $" << balance << endl;
cout << "----Rate of Return: " << ROI * 100 << "%\n";
cout << "PERIOD--------CONTRIBUTIONS--------SAVINGS-----------TOTAL\n";
for (int f = 0; f < (periods+1); f++) {
makerow(f);
}
}
};
int main() {
savings annual;
savings *a = &annual;
annual.getinfo();
annual.findans();
annual.showgraph();
system("pause");
return 0;
}
解决方案
推荐阅读
- typescript - 如何将单个动态键添加到 TS 中的类型?
- javascript - 如何检查用户是否在Javascript中输入了日期值?
- java - 尝试发布到包时,Github Actions 工作流(私人仓库)挂起
- python - 如何比较两个不同数据框的两列并添加新的结果列
- c# - 如何将数据表中的信息导出到 SQLite 表?
- python - 当对象触摸特定线集时如何在视频帧中设置计时器?
- python - 从 Python 中的 Google Geocoding API 获取特定字段
- sparql - 通过 SPARQL 中的链接对象获取对象的子对象
- javascript - React hooks 在数量 prop 的基础上克隆一个组件
- javascript - 我可以通过 devtool 连接到 websocket 吗?