Forum

Einloggen | Registrieren | RSS  

silent_max(R)

E-Mail

31.03.2012,
16:58
(editiert von silent_max
am 31.03.2012 um 17:08)
 

Komische Fehlermeldung (Computertechnik)

Hallo liebe Gemeinde.

Ich habe folgenden Quellcode geschrieben:

#include <iostream>
#include <math.h>
using namespace std;

float Add(float a, float b)
{
cout << endl;
cout << "Addition!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> a >> b;
float c = a + b;
cout << a << " + " << b << " = " << c << endl;
cout << endl;
return c;
}
float Sub(float d, float e)
{
cout << endl;
cout << "Subtraktion!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> d >> e;
float f = d - e;
cout << d << " - " << e << " = " << f << endl;
cout << endl;
return f;
}
float Mul(float g, float h)
{
cout << endl;
cout << "Multiplikation!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> g >> h;
float i = g*h;
cout << g << " * " << h << " = " << i << endl;
cout << endl;
return i;
}
float Div(float j, float k)
{
cout << endl;
cout << "Division!" << endl;
cout << " Zwei Zahlen bitte, jedoch darf der Zähler nicht 0 sein!" << endl;
cin >> j >> k;
float l = j/k ;
cout << j << " / " << k << " = " << l << endl;
cout << endl;
return l;
}

float Wurzel(float m)
{
cout << endl;
cout << "Quadratwurzel!" << endl;
cout << "Eine Zahl bitte!" << endl;
cin >> m;
float n = sqrt (m);
cout << "Ergebnis ist " << n << endl;
cout << endl;
return n;
}

int main()
{
float a,b,c;
float d,e,f;
float g,h,i;
float j,k,l;
float m,n;

cout << "Rechenoperationen!" << endl;

c=Add(a,b);
f=Sub(d,e);
i=Mul(g,h);
l=Div(j,k);
n=Wurzel(m);
return 0;
}

Der Kompiler hat beim Linken keinen Fehler gemeldet. Nachdem ich jedoch auf "Debuggen ohne starten" geklickt habe, kommt folgende Fehlermeldung:




Könnt ihr mir sagen, was ich falsch gemacht habe?

Weil so wie ich die Fehlermeldung kapiert habe, sei die Variable initialisiert, wird aber nicht verwendet. Was aber nicht sein kann, da sie in der Funktion "Addition" verwendet wird.

Danke für die Antworten im Voraus.

Gruß

Max

Edit: Hier ist der Quellcode besser zu lesen:

http://www.c-plusplus.de/forum/p2197349#2197349

--
Where is the madness ...

gast_0815

31.03.2012,
20:30

@ silent_max

Komische Fehlermeldung

» Hallo liebe Gemeinde.
»
» Ich habe folgenden Quellcode geschrieben:
»
» #include <iostream>
» #include <math.h>
» using namespace std;
»
//schnipp//
»
»
» int main()
» {
» float a,b,c;
» float d,e,f;
» float g,h,i;
» float j,k,l;
» float m,n;
»
» cout << "Rechenoperationen!" << endl;
»
» c=Add(a,b);
» f=Sub(d,e);
» i=Mul(g,h);
» l=Div(j,k);
» n=Wurzel(m);
» return 0;
» }
»
» Der Kompiler hat beim Linken keinen Fehler gemeldet. Nachdem ich jedoch
» auf "Debuggen ohne starten" geklickt habe, kommt folgende Fehlermeldung:
»
»
»
»
» Könnt ihr mir sagen, was ich falsch gemacht habe?
»
» Weil so wie ich die Fehlermeldung kapiert habe, sei die Variable
» initialisiert, wird aber nicht verwendet. Was aber nicht sein kann, da sie
» in der Funktion "Addition" verwendet wird.
»

Die Fehlermeldung sagt, dass die Variable nicht initialisiert wurde, das heisst, keinen Wert hat.

Du musst den Variablen noch einen Wert zuweisen z.B.
a=3;
b=64.8973798749;

oder wie auch immer...

Ich bin zwar auch nicht der Programmierguru, aber ich denke, das ist das Problem.

Schönen Abend noch und frohes weiterprogrammieren!

Gruß 0815

silent_max(R)

E-Mail

01.04.2012,
12:01

@ gast_0815

Komische Fehlermeldung

Inzwischen habe ich die Fehlermeldung beheben können, dennoch ist ein neues Problem aufgetaucht.

Ich wollte zu den Rechenoperationen
->+
->-
->*
->/
->Wurzl

noch die Potenzfunktion hinzufügen.

Hier der Quellcode:

#include <iostream>
#include <math.h>
using namespace std;

float Add(float a, float b)
{
cout << endl;
cout << "Addition!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> a >> b;
float c = a + b;
cout << a << " + " << b << " = " << c << endl;
cout << endl;
return c;
}
float Sub(float d, float e)
{
cout << endl;
cout << "Subtraktion!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> d >> e;
float f = d - e;
cout << d << " - " << e << " = " << f << endl;
cout << endl;
return f;
}
float Mul(float g, float h)
{
cout << endl;
cout << "Multiplikation!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> g >> h;
float i = g*h;
cout << g << " * " << h << " = " << i << endl;
cout << endl;
return i;
}
float Div(float j, float k)
{
cout << endl;
cout << "Division!" << endl;
cout << " Zwei Zahlen bitte, jedoch darf der Zähler nicht 0 sein!" << endl;
cin >> j >> k;
float l = j/k ;
cout << j << " / " << k << " = " << l << endl;
cout << endl;
return l;
}

float Wurzel(float m)
{
cout << endl;
cout << "Quadratwurzel!" << endl;
cout << "Eine Zahl bitte!" << endl;
cin >> m;
float n = sqrt (m);
cout << "Ergebnis ist " << n << endl;
cout << endl;
return n;
}
float Potenz(float o, float p)
{
cout << endl;
cout << "Potenz!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> o >> p;
float q=pow(o,p);
cout << "Ergebnis ist " << q << endl;
return q;
}

int main()
{
float a=0,b=0,c=0;
float d=0,e=0,f=0;
float g=0,h=0,i=0;
float j=0,k=0,l=0;
float m=0,n=0;
float o=0,p=0,q=0;

cout << "Rechenoperationen!" << endl;

c=Add(a,b);
f=Sub(d,e);
i=Mul(g,h);
l=Div(j,k);
n=Wurzel(m);
q=pow(o,p);
return 0;
}

Beim Kompilieren kommt keine Fehlermeldung. Bloß beim Starten ohne Debugging taucht die Potenzfunktion einfach nicht auf, die Ausgabe hört einfach nach der Wurzelfunktion auf zu arbeiten.

Kann mir jemand von euch einen Tipp geben?

Danke für die Antworten im Voraus.

Gruß

Max

--
Where is the madness ...

Björn(R)

E-Mail

Dortmund,
01.04.2012,
20:41

@ silent_max

Komische Fehlermeldung

» Inzwischen habe ich die Fehlermeldung beheben können, dennoch ist ein neues
» Problem aufgetaucht.
»
» Ich wollte zu den Rechenoperationen
» ->+
» ->-
» ->*
» ->/
» ->Wurzl
»
» noch die Potenzfunktion hinzufügen.
»
» Hier der Quellcode:
»
» #include <iostream>
» #include <math.h>
» using namespace std;
»
» float Add(float a, float b)
» {
» cout << endl;
» cout << "Addition!" << endl;
» cout << "Zwei Zahlen bitte!" << endl;
» cin >> a >> b;
» float c = a + b;
» cout << a << " + " << b << " = " << c << endl;
» cout << endl;
» return c;
» }
» float Sub(float d, float e)
» {
» cout << endl;
» cout << "Subtraktion!" << endl;
» cout << "Zwei Zahlen bitte!" << endl;
» cin >> d >> e;
» float f = d - e;
» cout << d << " - " << e << " = " << f << endl;
» cout << endl;
» return f;
» }
» float Mul(float g, float h)
» {
» cout << endl;
» cout << "Multiplikation!" << endl;
» cout << "Zwei Zahlen bitte!" << endl;
» cin >> g >> h;
» float i = g*h;
» cout << g << " * " << h << " = " << i << endl;
» cout << endl;
» return i;
» }
» float Div(float j, float k)
» {
» cout << endl;
» cout << "Division!" << endl;
» cout << " Zwei Zahlen bitte, jedoch darf der Zähler nicht 0 sein!" <<
» endl;
» cin >> j >> k;
» float l = j/k ;
» cout << j << " / " << k << " = " << l << endl;
» cout << endl;
» return l;
» }
»
» float Wurzel(float m)
» {
» cout << endl;
» cout << "Quadratwurzel!" << endl;
» cout << "Eine Zahl bitte!" << endl;
» cin >> m;
» float n = sqrt (m);
» cout << "Ergebnis ist " << n << endl;
» cout << endl;
» return n;
» }
» float Potenz(float o, float p)
» {
» cout << endl;
» cout << "Potenz!" << endl;
» cout << "Zwei Zahlen bitte!" << endl;
» cin >> o >> p;
» float q=pow(o,p);
» cout << "Ergebnis ist " << q << endl;
» return q;
» }
»
» int main()
» {
» float a=0,b=0,c=0;
» float d=0,e=0,f=0;
» float g=0,h=0,i=0;
» float j=0,k=0,l=0;
» float m=0,n=0;
» float o=0,p=0,q=0;
»
» cout << "Rechenoperationen!" << endl;
»
» c=Add(a,b);
» f=Sub(d,e);
» i=Mul(g,h);
» l=Div(j,k);
» n=Wurzel(m);
» q=pow(o,p);
» return 0;
» }
»
» Beim Kompilieren kommt keine Fehlermeldung. Bloß beim Starten ohne
» Debugging taucht die Potenzfunktion einfach nicht auf, die Ausgabe hört
» einfach nach der Wurzelfunktion auf zu arbeiten.
»
» Kann mir jemand von euch einen Tipp geben?
»
» Danke für die Antworten im Voraus.
»
» Gruß
»
» Max

Hallo,

deine Funktion heißt Potenz, aufrufen tust du aber pow.

Konzeptionell geht hier aber auch einiges durcheinander: Du deklarierst globale Variablen, die aber nur lokal in den Funktionen verwendet werden. Du gibst das Ergebnis der Berechnung zurück, verwendest es in der Main aber nicht. Besser ist einer der beiden Wege:

1. Du machst die Ein- und Ausgabe wie bisher in den Funktionen selbst, verwendest aber lokale Variablen, sparst Dir also die Parameterübergabe über die Funktion, außerdem auch die Rückgabe, z.B.:

void Add()
{
cout << "Addition!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
float a, b, c;
cin >> a >> b;
float c = a + b;
cout << a << " + " << b << " = " << c << endl;
}

2. Du machst die Ein- und Ausgaben in der Main und nur die Berechnung in den Funktionen:

float Add(float a, float b)
{
return (a + b);
}

float Sub(float a, float b)
{
return (a - b);
}

int main()
{
float a, b, c;

cout << "Addition!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> a >> b;
c = Add(a, b);
cout << a << " + " << b << " = " << c << endl;
cout << endl;

cout << "Subtraktion!" << endl;
cout << "Zwei Zahlen bitte!" << endl;
cin >> a >> b;
c = Sub(a, b);
cout << a << " - " << b << " = " << c << endl;
return 0;
}

Schöne Grüße,
Björn