Zugriffsverletzung beim Lesen an Position 0x00000000



  • hallo ich bin ein C++ neuling und habe hier mein erstes Programm geschrieben
    ich habe das Problem in der Main.cpp und das Programm ist nicht ausführbar.

    Ausnahme ausgelöst bei 0x54C6FF80 (ucrtbased.dll in Parkuhr.exe:
    0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000000

    das Problem tritt in der main.cpp auf
    bei
    float hours = std::stof(argv[1]);

    hier das Programm mit
    main.cpp
    parking.h
    parking.cpp

    main.cpp:

    #include <iostream>
    #include <stdio.h>
    #include <iomanip>
    #include <string>
    #include <vector>
    #include "parking.h"
    #include <array>
    using namespace std;

    int main(int argc, char** argv) {
    //allocate vectors to store time and change (money)
    vector<int> time;
    vector<int> change;
    if (argc < 2 || argc > 6)
    cout << "The number of input arguments has to be between one and five!" << endl;

    // Testing function calculate_fee()
    float hours = std::stof(argv[1]);
    cout << "\nFor " << hours << " hour(s) of parking, the charge is: ";
    cout << calculate_fee(hours) << " Euro(s)" << endl;
    
    // Testing function calculate_parkingtime()
    float cash = std::stof(argv[2]);
    cout << "\nFor " << cash << " Euro(s) the parking time is: ";
    time = calculate_parking_time(cash);
    for (int i = 0; i < 4; i++) {
    	cout << time[i];
    	if (i == 0) {
    		cout << "m ";
    	}
    	else if (i == 1) {
    		cout << "h ";
    	}
    	else if (i == 2) {
    		cout << "d ";
    	}
    	else {
    		cout << "y";
    	}
    }
    cout << endl;
    
    // Testing function convert_to_coins()
    cash = std::stof(argv[3]);
    cout << "\nExchanging " << cash << " Euro(s) into coins: \n";
    change = convert_to_coins(cash);
    for (int i = 0; i < 8; i++) {
    	cout << change[i];
    	if (i == 0) {
    		cout << "x2Euro ";
    	}
    	else if (i == 1) {
    		cout << "x1Euro ";
    	}
    	else if (i == 2) {
    		cout << "x50Cent ";
    	}
    	else if (i == 3) {
    		cout << "x20Cent ";
    	}
    	else if (i == 4) {
    		cout << "x10Cent ";
    	}
    	else if (i == 5) {
    		cout << "x5Cent ";
    	}
    	else if (i == 6) {
    		cout << "x2Cent ";
    	}
    	else if (i == 7) {
    		cout << "x1Cent";
    	}
    }
    cout << endl;
    
    // Actual application based on function bill_customer()
    // The function bill_customer() calls calculate_fee() and convert_to_coins()
    hours = std::stof(argv[4]);
    cash = std::stof(argv[5]);
    cout << "\nYou parked for " << hours << " hour(s) and paid " << cash << " Euro(s) your change/charge is: \n";
    change = bill_customer(hours, cash);
    for (int i = 0; i < 8; i++) {
    	cout << change[i];
    	if (i == 0) {
    		cout << "x2Euro ";
    	}
    	else if (i == 1) {
    		cout << "x1Euro ";
    	}
    	else if (i == 2) {
    		cout << "x50Cent ";
    	}
    	else if (i == 3) {
    		cout << "x20Cent ";
    	}
    	else if (i == 4) {
    		cout << "x10Cent ";
    	}
    	else if (i == 5) {
    		cout << "x5Cent ";
    	}
    	else if (i == 6) {
    		cout << "x2Cent ";
    	}
    	else if (i == 7) {
    		cout << "x1Cent";
    	}
    }
    cout << endl;
    
    return 0;
    

    }

    parking.cpp

    #include <iostream>
    #include <stdio.h>
    #include <iomanip>
    #include <string>
    #include <vector>

    using namespace std;

    double calculate_fee(double parking_hours)
    {
    double parkinghours = 0;
    double parkcost = 0;
    int p_integer = 0;
    double gerade = 0;
    int p_integer1 = 0;

    parkinghours = parking_hours;
    
    
    if (parkinghours <= 0)
    	parkcost = 0;
    
    else if (parkinghours <= 4.0)
    	parkcost = parkinghours * 2 - 1;
    
    else if (parkinghours > 4)
    {
    	p_integer = static_cast<int>(parkinghours);
    
    	double gerade = parkinghours / p_integer;
    
    	if (gerade == 1)
    	{
    		parkinghours = parkinghours - 1;
    		p_integer1 = static_cast<int>(parkinghours);
    		p_integer1 = p_integer1 + 1;
    		parkcost = (p_integer1 * 1.5) + 1;
    	}
    
    
    	else {
    		p_integer = static_cast<int>(parkinghours);
    
    		parkcost = (p_integer * 1.5) + 2.5;
    	}
    
    }
    
    
    return parkcost;
    

    }

    vector<int> calculate_parking_time(double amount) // Parkzeit
    {
    int stunde = 0;
    int minute = 0;
    int tage = 0;
    int jahre = 0;

    double rest = 0;
    int zeit_integer = 0;
    int amount_integer = 0;
    double zeit = 0;
    int zeit2 = 0;
    vector<int> parkzeit(4);
    int c3po = 0;
    
    if (amount < 1)
    
    	zeit = 0.5;
    
    else if (amount <= 7)
    {
    	amount_integer = static_cast<int>(amount); // Amount zu Integer (abrunden)
    
    	if (amount_integer == amount)	//Amount ist eine Ganze Zahl ->kann einfach ausgerechnet werden
    
    		zeit = ((amount - 1) * 0.5) + 1;
    
    	else
    	{
    		amount = amount_integer;	//Amont muss aberundet werden aufgrund Preismodell 1. 
    		zeit = ((amount - 1) * 0.5) + 1;
    	}
    }
    
    
    else if (amount > 7)
    {
    	amount = amount - 7;	// die 7 euro von Preismodell 1 abziehen
    
    	zeit = 4 + (amount / 1.5);	//zeit berechnen 
    	zeit_integer = static_cast<int>(zeit);
    
    	if (zeit == zeit_integer)		// ist zeit eine Ganze zahl kann sie übernommen werden 
    		zeit = zeit;
    
    	else
    	{
    		zeit2 = static_cast<int>(zeit_integer);		//ist zeit ein Double wert muss der Rest berechnet werden
    		rest = zeit - zeit2;
    		if (rest == 0.5)							// nur wenn der Rest 0.5 (30min) entspricht wird er übernommen
    			zeit = zeit;
    		else
    			zeit = zeit_integer;					//ansonsten wird abgerundet
    	}
    
    }
    
    zeit = zeit * 60;								// zeit umrechnung von std -> min
    cout << zeit << endl;
    c3po = static_cast<int>(zeit);					//erstellen eines Integers aus testzwecken
    
    cout << c3po << endl;
    
    jahre = floor(c3po / 525600);					//problem mit Arithmetischem überlauf....
    tage = floor((c3po - jahre * 525600) / 1440);
    stunde = floor((c3po - jahre * 525600 - tage * 1440) / 60);
    minute = floor((c3po - jahre * 525600 - tage * 1440 - stunde * 60));
    
    cout << minute << stunde << tage << jahre << endl;
    
    parkzeit[0] = minute;
    parkzeit[1] = stunde;
    parkzeit[2] = tage;
    parkzeit[3] = jahre;
    
    
    return parkzeit;
    

    }

    vector<int> convert_to_coins(double return_amount)
    {
    int referenz = 0;

    int a1ct = 0;
    int a2ct = 0;
    int a5ct = 0;
    int a10ct = 0;
    int a20ct = 0;
    int a50ct = 0;
    int a1euro = 0;
    int a2euro = 0;
    
    vector<int>Wechselgeld(8);
    
    if (return_amount < 0)
    	referenz = 1;
    
    return_amount = round(return_amount * 100);		//wechselgeld in ct
    
    if (return_amount >= 0)						// wechselgeld Positiv
    {
    
    	while (return_amount > 0)
    	{
    		if (return_amount >= 200)
    		{
    			return_amount = return_amount - 200;
    			a2euro++;
    		}
    
    		else if (return_amount >= 100)
    		{
    			return_amount = return_amount - 100;
    			a1euro++;
    		}
    
    		else if (return_amount >= 50)
    		{
    			return_amount = return_amount - 50;
    			a50ct++;
    		}
    		else if (return_amount >= 20)
    		{
    			return_amount = return_amount - 20;
    			a20ct++;
    		}
    		else if (return_amount >= 10)
    		{
    			return_amount = return_amount - 10;
    			a10ct++;
    		}
    		else if (return_amount >= 5)
    		{
    			return_amount = return_amount - 5;
    			a5ct++;
    		}
    		else if (return_amount >= 2)
    		{
    			return_amount = return_amount - 2;
    			a2ct++;
    		}
    		else if (return_amount >= 1)
    		{
    			return_amount = return_amount - 1;
    			a1ct++;
    		}
    	}
    }
    
    else
    {
    
    	while (return_amount < 0)					//wechselgeld negativ
    	{
    		if (return_amount <= -200)
    		{
    			return_amount = return_amount + 200;
    			a2euro--;
    		}
    
    		else if (return_amount <= -100)
    		{
    			return_amount = return_amount + 100;
    			a1euro--;
    		}
    
    		else if (return_amount <= -50)
    		{
    			return_amount = return_amount + 50;
    			a50ct--;
    		}
    		else if (return_amount <= -20)
    		{
    			return_amount = return_amount + 20;
    			a20ct--;
    		}
    		else if (return_amount <= -10)
    		{
    			return_amount = return_amount + 10;
    			a10ct--;
    		}
    		else if (return_amount <= -5)
    		{
    			return_amount = return_amount + 5;
    			a5ct--;
    		}
    		else if (return_amount <= -2)
    		{
    			return_amount = return_amount + 2;
    			a2ct--;
    		}
    		else if (return_amount <= -1)
    		{
    			return_amount = return_amount + 1;
    			a1ct--;
    		}
    
    	}
    
    }
    Wechselgeld[0] = a2euro;
    Wechselgeld[1] = a1euro;
    Wechselgeld[2] = a50ct;
    Wechselgeld[3] = a20ct;
    Wechselgeld[4] = a10ct;
    Wechselgeld[5] = a5ct;
    Wechselgeld[6] = a2ct;
    Wechselgeld[7] = a1ct;
    
    
    
    
    return Wechselgeld;
    

    }

    vector<int> bill_customer(double parking_hours, double amount)
    {
    double parkkosten = 0;
    vector<int> Wechselgeld1(8);

    parkkosten = calculate_fee(parking_hours);
    Wechselgeld1 = convert_to_coins(amount - parkkosten);
    
    
    
    return Wechselgeld1;
    

    }

    parking.h

    #pragma once
    #include <iostream>
    #include <stdio.h>
    #include <iomanip>
    #include <string>
    #include <vector>

    using namespace std;

    double calculate_fee(double parking_hours);
    vector<int> calculate_parking_time(double amount);
    vector<int> convert_to_coins(double return_amount);
    vector<int> bill_customer(double parking_hours, double amount);



  • Benutze einen Debugger, der zeigt dir genau an was schief läuft.



  • @SchaumWanne sagte in Zugriffsverletzung beim Lesen an Position 0x00000000:

    if (argc < 2 || argc > 6)
        cout << "The number of input arguments has to be between one and five!" << endl;
    
    // Testing function calculate_fee()
    float hours = std::stof(argv[1]);
    

    Der Code ist wegen der komischen Code-Tags etwas schlecht lesbar, aber kann es sein, dass dein Programm hier lesend auf argv[1] zugreift, obwohl argc < 2 gilt?

    Das sieht mir auf den ersten Blick nach der plausibelsten Erklärung aus. argv[1] ist wahrscheinlich 0 (nullptr) und liegt außerhalb des Array.


  • Mod

    Was du beschreibst kann in dem Code eigentlich nicht vorkommen. Da du extrem viel Code unformatiert postest, ist es schwer, den wirklichen Fehler zu finden. Reduziere deinen Code aufs Wesentliche und poste ihn dann hier zwischen Codetags! D.h. mit einer Zeile ``` davor und dahinter.



  • Editiere deinen Beitrag (Menü mit den drei Punkten: "Bearbeiten") und füge Code-Tags hinzu - so wird sich das keiner anschauen.

    Generell gilt bei einer Zugriffsverletzung, daß du auf Speicher zugreifst, der nicht dem Programm zugeordnet ist.
    Benutze daher einen Debugger, der wird dir die Zeile anzeigen, welche zu dem Fehler führt (und dann kannst du die Variablen überprüfen).

    PS: Ich sehe den Fehler schon...
    Hinweis: Wie rufst du das Programm auf (d.h. mit welchen Parametern)?

    Edit: Ups, schon andere Antworten hier (das passiert, wenn zwischendrin der Postbote klingelt ;- ).



  • @Th69 sagte in Zugriffsverletzung beim Lesen an Position 0x00000000:

    Edit: Ups, schon andere Antworten hier (das passiert, wenn zwischendrin der Postbote klingelt ;- ).

    Ich hoffe, nur einmal 😅



  • Ja, ich hatte Glück! 🙂



  • Hallo
    Vielen Dank fuer die schnelle und zuverlässige Hilfe ihr seit Spitze
    Da ich die Main.cpp von meinem Prof. Gestellt bekommen hatte und das ganze Programm über CMD laufen sollte war das problem, das die Main.cpp keine Werte bekommen hat da ich das ganze im lokalen windows Debugger starten wollte
    Das Problem ist gelöst
    Viel dank nochmal


Anmelden zum Antworten