Hilfe bei Aufgabe
-
Hallo ich hab folgendes Programm
//CapitalTrend.cpp #include <iostream> using namespace std; double capTrend(int n, double initialCapital, double rate); void capTrend2(int n, double& cap, double rate); int main() { cout << capTrend(5, 2000, 0.05) << endl << endl; cout << capTrend2(5,2000, 0.05) << endl <<endl; system("PAUSE"); return 0; } double capTrend(int n, double initialCapital, double rate){ // TODO: input useful comments double res = initialCapital; for (int year = 1; year <= n; year++) res += res*rate; return res; } void capTrend2(int n, double& cap, double rate){ // TODO: input useful comments for (int year = 1; year <= n; year++) cap += cap*rate; }
Fehlermeldung:
error C2664: 'capTrend2': Konvertierung des Parameters 2 von 'int' in 'double &' nicht möglichIch verstehe nicht welche Parameter capTrend2 von mir braucht. Ich weis das es etwas mit Referenzen zu tuen hat. Aber wie starte ich die Funktion nun richtig?
Vielen Dank für die Hilfe
-
Keitero13 schrieb:
Fehlermeldung:
error C2664: 'capTrend2': Konvertierung des Parameters 2 von 'int' in 'double &' nicht möglichIch verstehe nicht welche Parameter capTrend2 von mir braucht. Ich weis das es etwas mit Referenzen zu tuen hat.
Aber wie starte ich die Funktion nun richtig?Erstmal hat dieser Quellcode garnichts mit MFC zu sondern lediglich mit C++.
Der Compiler sagt doch, was er nicht mag. Der 2te Parameter muss ein LValue und
keine Konstante sein. Und wenn cout das ganze ausgeben soll, ist void als Rückgabewert auch sinnlos.Es müsste also so aussehen:
double capTrend(int n, double initialCapital, double rate); double capTrend2(int n, double& cap, double rate); int main() { double mycap = 2000; cout << capTrend(5, 2000, 0.05) << endl << endl; cout << capTrend2(5, mycap, 0.05) << endl <<endl; ...
-
Vielen Dank, aber das mit dem void ist mir so vorgegeben.
Gibt es auch eine Möglichkeit es mit dem Void zu lösen?Entschuldigung das ich es im falschen Forum geschrieben habe, kann ein Mod es bitte verschieben?
-
Keitero13 schrieb:
Vielen Dank, aber das mit dem void ist mir so vorgegeben.
Gibt es auch eine Möglichkeit es mit dem Void zu lösen?Habs mir schon gedacht, das ist auch wohl der eigentliche Sinn der Aufgabe!!
Man könnte die Variable mycap ausgeben ...
-
-
Hab es hinbekommen. Danke für den Typ mit dem myCap
Falls jemand ähnliche Probleme hat, ist hier die Lösung://CapitalTrend.cpp #include <iostream> using namespace std; double capTrend(int n, double initialCapital, double rate); void capTrend2(int n, double& cap, double rate); int main() { double mycap=2000; cout << capTrend(5, 2000, 0.05) << endl << endl; capTrend2(5, mycap, 0.05); cout << mycap; system("PAUSE"); return 0; } double capTrend(int n, double initialCapital, double rate){ // TODO: input useful comments double res = initialCapital; for (int year = 1; year <= n; year++) res += res*rate; return res; } void capTrend2(int n, double& cap, double rate){ // TODO: input useful comments for (int year = 1; year <= n; year++) cap += cap*rate; }