我在C ++中编译程序时遇到问题。我编写了一个程序,当我在程序中输入内容时,我立即收到此消息:
http://abload.de/image.php?img=hweteu89.png
这是翻译:
程序已停止工作
问题导致程序正常工作。如果有可用的解决方案,Windows将关闭该程序并通知您。
这是我的源代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{float SLN,R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R123,R23,R1234,R12345,R78,R678,R12345678,Rg,Uq=12+(SLN*0,1),Ig,I10,I9,I12345678,I12345,I678,U10,I6,U6,U78=I678*R78,I7,I8,U12345,U1234=U12345,U5=U12345,I1234,U123,U4,I5,U1=U123,I1,U2,U3,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,M,I2,I3=I2,I4=U4/R4,U7=U78,U8=U78,U9=I9*R9,Pg,Pg1;
cout<<"Bitte geben sie die Schülerlistennumer ein."<<endl;
cin>>SLN;//SLN eingeben
//Widerstandsvariablen definieren
R1=100;
R2=200;
R4=400;
R5=500;
R7=700;
R8=800;
R10=1000;
//Widerstände berechnen
R3=SLN*25;
R6=1000-(SLN*15);
R9=SLN*20;
R23=R2+R3;
R123=(R23*R1)/(R23+R1);
R1234=R123+R4;
R12345=(R1234*R5)/(R1234+R5);
R78=(R7*R8)/(R7+R8);
R678=R6+R78;
R12345678=R12345+R678;
Rg=1/((1/R12345678)+(1/R9)+(1/R10));
//Ströme berechnen
Ig=Uq/Rg;
I10=Uq/R10;
I9=Uq/R9;
I12345678=Uq/R12345678;
I12345=I12345678;
I678=I12345678;
I6=I678;
I7=U78/R7;
I8=U78/R8;
I1234=U1234/R1234;
I5=U5/R5;
I1=U1/R1;
I2=I12345678;
U10=I10*R10;
U6=I6*R6;
U12345=I12345*R12345;
U123=I1234*R123;
U4=I1234*R4;
U2=R2*I12345678;
U3=R3*I12345678;
//Leistungen berechnen
P1=I1*U1;
P2=I2*U2;
P3=I3*U3;
P4=I4*U4;
P5=I5*U5;
P6=I6*U6;
P7=I7*U7;
P8=I8*U8;
P9=I9*U9;
P10=I10*U10;
Pg=Uq*Ig;
Pg1=P1+P2+P3+P4+P5+P6+P7+P8+P9+P10;
//Variablen ausgeben(Spannung)
cout<<"Die spannung U1 betraegt"<<U1<<"Volt."<<endl;
cout<<"Die spannung U2 beträgt"<<U2<<"Volt."<<endl;
cout<<"Die spannung U3 betraegt"<<U3<<"Volt."<<endl;
cout<<"Die spannung U4 betraegt"<<U4<<"Volt."<<endl;
cout<<"Die spannung U5 betraegt"<<U5<<"Volt."<<endl;
cout<<"Die spannung U6 betraegt"<<U6<<"Volt."<<endl;
cout<<"Die spannung U7 betraegt"<<U7<<"Volt."<<endl;
cout<<"Die spannung U8 betraegt"<<U8<<"Volt."<<endl;
cout<<"Die spannung U9 betraegt"<<U9<<"Volt."<<endl;
cout<<"Die spannung U10 betraegt"<<U10<<"Volt."<<endl;
//Variablen ausgeben (Strom)
cout<<"Der Strom I1 betraegt"<<I1<<"Ampere."<<endl;
cout<<"Der Strom I2 betraegt"<<I2<<"Ampere."<<endl;
cout<<"Der Strom I3 betraegt"<<I3<<"Ampere."<<endl;
cout<<"Der Strom I4 betraegt"<<I5<<"Ampere."<<endl;
cout<<"Der Strom I5 betraegt"<<I5<<"Ampere."<<endl;
cout<<"Der Strom I6 betraegt"<<I6<<"Ampere."<<endl;
cout<<"Der Strom I7 betraegt"<<I7<<"Ampere."<<endl;
cout<<"Der Strom I8 betraegt"<<I8<<"Ampere."<<endl;
cout<<"Der Strom I9 betraegt"<<I9<<"Ampere."<<endl;
cout<<"Der Strom I10 betraegt"<<I10<<"Ampere."<<endl;
cout<<"Der Strom Iges betraegt"<<Ig<<"Ampere."<<endl;
//Variable ausgeben(Leistung)
cout<<"Die Leistung P1 betraegt "<<P1<<"Watt."<<endl;
cout<<"Die Leistung P2 betraegt "<<P2<<"Watt."<<endl;
cout<<"Die Leistung P3 betraegt "<<P3<<"Watt."<<endl;
cout<<"Die Leistung P4 betraegt "<<P4<<"Watt."<<endl;
cout<<"Die Leistung P5 betraegt "<<P5<<"Watt."<<endl;
cout<<"Die Leistung P6 betraegt "<<P6<<"Watt."<<endl;
cout<<"Die Leistung P7 betraegt "<<P7<<"Watt."<<endl;
cout<<"Die Leistung P8 betraegt "<<P8<<"Watt."<<endl;
cout<<"Die Leistung P9 betraegt "<<P9<<"Watt."<<endl;
cout<<"Die Leistung P10 betraegt "<<P10<<"Watt."<<endl;
cout<<"Die Leistung Pges betraegt "<<Pg<<"Watt."<<endl;
cout<<"Die Leistung Pg1 betraegt "<<Pg1<<"Watt."<<endl;
//Maschenregel ausgeben
M=U5-U10+U8+U6;
cout<<"Bei der Machenregel kommt"<<M<<"heraus."<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
我非常感谢帮助解决此错误。
答案 0 :(得分:4)
评论是真实的。你应该学会使用循环。
话虽如此,问题很可能是您正在使用未初始化的变量。见:
U78=I678*R78
I678的值未初始化,但您正在使用它来计算U78。我的Visual Studio还会将这些变量报告为在初始化之前使用:
1>warning C4700: uninitialized local variable 'R78' used
1>warning C4700: uninitialized local variable 'U12345' used
1>warning C4700: uninitialized local variable 'U123' used
1>warning C4700: uninitialized local variable 'I2' used
1>warning C4700: uninitialized local variable 'U4' used
1>warning C4700: uninitialized local variable 'R4' used
1>warning C4700: uninitialized local variable 'I9' used
1>warning C4700: uninitialized local variable 'R9' used
您直接或间接地在进行除法的行中使用这些,因此如果它们未初始化,程序将显示未定义的行为。因此,初始化变量,更好地格式化代码,使得行不是长度不足的字符,并开始使用循环。