Error C2065: Undeclared Identifier

owned139

Mitglied
Guten Abend,
ich habe heute mit c++ angefangen und mir auch schon ein V2B C++ video tutorial angesehen.
Danach wollte ich mir eine kleine Entgeltabrechnung schreiben, welches aber nicht ganz so funktioniert wie ich mir das vorstelle :/

Code:
#include <iostream>
#include <string>

using namespace std;

int main(){
	int BBMG_KP = 3750;
	int BBMG_RA = 5500;
	int alter;
	float kinder;
	string familienstand;
	string religion;
	float lohnsteuer;
	float bruttogehalt;

	cout << "Wie alt sind Sie?\n";
	cin >> alter;
	cout << "Wieviele Kinder haben Sie?\n";
	cin >> kinder;
	cout << "Wie lautet ihr aktueller Familienstand?\n";
	cin >> familienstand;
	cout << "Welcher Religion gehören Sie an?\n";
	cin >> religion;
	cout << "Wie hoch ist ihr Bruttogehalt?\n";
	cin >> bruttogehalt;
	cout << "Wie hoch ist ihre Lohnsteuer?\n";
	cin >> lohnsteuer;

	float soli = lohnsteuer * 5.5 / 100;
	cout << "Solidaritätszuschlag:     " << soli << endl;

	if(religion == "keine"){
		int kist = 0;
	}else{
		float kist = lohnsteuer * 9 / 100;
	}
	if(bruttogehalt >= BBMG_KP){
		float kv = BBMG_KP * 7.9 / 100;
		float pv = BBMG_KP * 0.975 / 100;
	}else{
		float kv = bruttogehalt * 7.9 / 100;
		float pv = bruttogehalt * 0.975 / 100;
	}
	if(bruttogehalt >= BBMG_RA){
		float rv = BBMG_RA * 9.95 / 100;
		float av = BBMG_RA * 1.4 / 100;
	}else{
		float rv = bruttogehalt * 9.95 / 100;
		float av = bruttogehalt * 1.4 / 100;
	}
	cout << "Kirchensteuer:            " << kist << endl;
	cout << "Krankenversicherung:      " << kv << endl;
	cout << "Pflegeversicherung:       " << pv << endl;
	cout << "Rentenversicherung:       " << rv << endl;
	cout << "Arbeitslosenversicherung: " << av << endl;
	cout << "=================================================" << endl;
	float nettogehalt = bruttogehalt - ( lohnsteuer + soli + kist + kv + pv + rv + av);
	cout << "Nettogehalt:              " << nettogehalt;

	return 0;
}

und diese Fehler bekomme ich:

Code:
1>------ Build started: Project: test, Configuration: Release Win32 ------
1>Compiling...
1>main.cpp
1>.\main.cpp(30) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(39) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(40) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(42) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(43) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(46) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(47) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(49) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(50) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>.\main.cpp(52) : error C2065: 'kist' : undeclared identifier
1>.\main.cpp(53) : error C2065: 'kv' : undeclared identifier
1>.\main.cpp(54) : error C2065: 'pv' : undeclared identifier
1>.\main.cpp(55) : error C2065: 'rv' : undeclared identifier
1>.\main.cpp(56) : error C2065: 'av' : undeclared identifier
1>.\main.cpp(58) : error C2065: 'kist' : undeclared identifier
1>.\main.cpp(58) : error C2065: 'kv' : undeclared identifier
1>.\main.cpp(58) : error C2065: 'pv' : undeclared identifier
1>.\main.cpp(58) : error C2065: 'rv' : undeclared identifier
1>.\main.cpp(58) : error C2065: 'av' : undeclared identifier

Habe google auch schon benutzt, allerdings helfen mir die glieferten Ergebnisse nicht, da ich C++ anfänger bin
 
Hi.

Statt "float" nimm überall den Datentyp "double".

Variablen werden nur innerhalb eines Blocks definiert. Die Variable kist ist bei der nur innerhalb der if Anweisung gültig, außerhalb ist die Variable nicht bekannt. Du kannst die Variable nicht wahlweise mit dem Typ float oder int definieren.

C++:
double kist;
if(religion == "keine"){
  kist = 0;
	}else{
		kist = lohnsteuer * 9 / 100;
	}
double kv, pv;
	if(bruttogehalt >= BBMG_KP){
		kv = BBMG_KP * 7.9 / 100;
		pv = BBMG_KP * 0.975 / 100;
	}else{
		kv = bruttogehalt * 7.9 / 100;
		pv = bruttogehalt * 0.975 / 100;
	}
double rv, av;
	if(bruttogehalt >= BBMG_RA){
		rv = BBMG_RA * 9.95 / 100;
		av = BBMG_RA * 1.4 / 100;
	}else{
		rv = bruttogehalt * 9.95 / 100;
		av = bruttogehalt * 1.4 / 100;
	}
	cout << "Kirchensteuer:            " << kist << endl;
	cout << "Krankenversicherung:      " << kv << endl;
	cout << "Pflegeversicherung:       " << pv << endl;
	cout << "Rentenversicherung:       " << rv << endl;
	cout << "Arbeitslosenversicherung: " << av << endl;
Gruß
 
Danke! :D
Hat die Frau im tut auch gesagt...dachte das zählt nur für for-schleifen.
Wäre nett wenn mir jez noch jemand sagt warum double statt float :>
 
Danke! :D
Hat die Frau im tut auch gesagt...dachte das zählt nur für for-schleifen.
Wäre nett wenn mir jez noch jemand sagt warum double statt float :>
float sind "floating-point" Zahlen, double sind "floating-piont" Zahlen mit doppelter Genauigkeit. Mit double kann man also genauer Rechnen. float verwendet man fast gar nicht mehr.

Die Warnungen resultierten daher, dass du double-Literale an float-Variablen zugewiesen hast (was eben zu Verlusten bei der impliziten Konvertierung führen kann); deshalb die Warnung C4244.
C++:
float f1 = 2.4f;
float f2 = 2.4; // Warnung C4244
double d = 2.4; // OK
Gruß
 
Zurück