我开始使用动态内存,而运行它的程序正在崩溃。我的输入是26付款,并输出到.csv文件,其中付款编号是25导致程序崩溃和分配的内存是错误的(抱歉我的英文不好,我想你明白我的意思)。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int year, month, day, payments;
float total, presentValue, interestRate;
float *Remaining, *Principal, *Interest;
cout << "Insert date" << endl;
cin >> year >> month >> day;
cout << "Insert Present Value" << endl;
cin >> presentValue;
cout << "Insert Interest Rate" << endl;
cin >> interestRate;
cout << "Insert number of payments" << endl;
cin >> payments;
Remaining = new(nothrow)float[payments];
Principal = new(nothrow)float[payments];
Interest = new(nothrow)float[payments];
Remaining[0] = presentValue;
total = (Remaining[0] * (interestRate / 1200)) / (1 - pow(1 + (interestRate / 1200), payments*(-1)));
cout << total;
ofstream myfile;
myfile.open ("payments3.csv");
myfile << "Payment #;Payment date;Remaining amount; \
Principal payment;Interest payment;Total payment;Interest rate" << endl;
for (int i = 0; i < payments; i++) {
myfile << i+1 << ";";
if (month > 12) {
month = 1;
year += 1;
}
myfile << year << '-'
<< month << '-'
<< day
<< ";";
month++;
Interest[i] = Remaining[i] * (interestRate / 1200);
Principal[i] = total - Interest[i];
Remaining[i+1] = Remaining[i] - Principal[i];
myfile << fixed << showpoint;
myfile << setprecision(2);
myfile << Remaining[i] << ";" << Principal[i] << ";" << Interest[i] << ";\
" << total << ";" << interestRate << endl;
}
myfile.close();
delete[] Remaining;
delete[] Principal;
delete[] Interest;
return 0;
}
答案 0 :(得分:0)
您特别使用Remaining[0]
和Remaining[i+1]
,这可能意味着您需要该阵列的一个额外元素。
否则[i+1]
将在循环的最后一个回合中超出范围。
答案 1 :(得分:0)
好吧,以下行尝试访问超出范围:
if (i + 1 < payments)
Remaining[i+1] = Remaining[i] - Principal[i];
试试这个:
[AWT-EventQueue-0] 0:0:0,0 - EDT performance is very slow triggering this exception!
[AWT-EventQueue-0] 0:0:0,1 - Exception: java.lang.ArrayIndexOutOfBoundsException - 1000
java.lang.ArrayIndexOutOfBoundsException: 1000
[AWT-EventQueue-0] 0:0:0,75 - EDT performance is very slow triggering this exception!
[AWT-EventQueue-0] 0:0:0,76 - Exception: java.lang.ArrayIndexOutOfBoundsException - 1000
at com.codename1.ui.Display.addPointerEventWithTimestamp(Display.java:1678)
at com.codename1.ui.Display.pointerDragged(Display.java:1704)
at com.codename1.impl.CodenameOneImplementation.pointerDragged(CodenameOneImplementation.java:1957)
at com.codename1.impl.CodenameOneImplementation.pointerDragged(CodenameOneImplementation.java:1845)
at com.codename1.impl.javase.JavaSEPort.access$1900(JavaSEPort.java:160)
at com.codename1.impl.javase.JavaSEPort$C.mouseDragged(JavaSEPort.java:957)
at java.awt.Component.processMouseMotionEvent(Component.java:6573)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)
at java.awt.Component.processEvent(Component.java:6294)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4550)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.ArrayIndexOutOfBoundsException: 1000
at com.codename1.ui.Display.addPointerEventWithTimestamp(Display.java:1676)
at com.codename1.ui.Display.pointerDragged(Display.java:1704)
at com.codename1.impl.CodenameOneImplementation.pointerDragged(CodenameOneImplementation.java:1957)
at com.codename1.impl.CodenameOneImplementation.pointerDragged(CodenameOneImplementation.java:1845)
at com.codename1.impl.javase.JavaSEPort.access$1900(JavaSEPort.java:160)
at com.codename1.impl.javase.JavaSEPort$C.mouseDragged(JavaSEPort.java:957)
at java.awt.Component.processMouseMotionEvent(Component.java:6573)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)
at java.awt.Component.processEvent(Component.java:6294)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4550)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1000
at com.codename1.ui.Display.addPointerEvent(Display.java:1637)
at com.codename1.ui.Display.pointerReleased(Display.java:1789)
at com.codename1.impl.CodenameOneImplementation.pointerReleased(CodenameOneImplementation.java:2123)
at com.codename1.impl.CodenameOneImplementation.pointerReleased(CodenameOneImplementation.java:1871)
at com.codename1.impl.javase.JavaSEPort.access$1600(JavaSEPort.java:160)
at com.codename1.impl.javase.JavaSEPort$C.mouseReleased(JavaSEPort.java:923)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)