Sonderfall abfangen wenn int negativ

hagbard7995

Grünschnabel
Das Programm soll eine int-zahl einlesen und die Zahl als Pixelmatrix ausgeben.
Die Ausgabe von positiven Int-werten ist kein Problem,
kommt aber ein Vorzeichen vor der zahl, weiß ich nicht mehr weiter.

die matrix sieht so aus:

Code:
const int zeichen = 12;
const pixel_font_t matrix[zeichen] = {
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', 'X', 'X'},
        {'X', ' ', 'X', ' ', 'X'},
        {'X', ' ', 'X', ' ', 'X'},
        {'X', 'X', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '}
    },
    {
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', 'X', 'X', ' '},
        {' ', 'X', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '}
    },
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', 'X', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {'X', ' ', ' ', ' ', ' '},
        {'X', 'X', 'X', 'X', 'X'}
    },
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', ' ', 'X'},
        {' ', ' ', 'X', 'X', ' '},
        {' ', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '}
    },
    {
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', 'X', 'X', ' '},
        {' ', 'X', ' ', 'X', ' '},
        {'X', ' ', ' ', 'X', ' '},
        {'X', 'X', 'X', 'X', 'X'},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '}
    },
    {
        {'X', 'X', 'X', 'X', 'X'},
        {'X', ' ', ' ', ' ', ' '},
        {'X', ' ', ' ', ' ', ' '},
        {'X', 'X', 'X', 'X', ' '},
        {' ', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'x', ' '}
    },
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' '},
        {'X', ' ', 'X', 'X', ' '},
        {'X', 'X', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '}
    },
    {
        {'X', 'X', 'X', 'X', 'X'},
        {' ', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', 'X', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '}
    },
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '}
    },
    {
        {' ', 'X', 'X', 'X', ' '},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', 'X', 'X'},
        {' ', 'X', 'X', ' ', 'X'},
        {' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', 'X'},
        {' ', 'X', 'X', 'X', ' '}
    },
    {
        {' ', ' ', ' ', ' ', 'X'},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', ' ', 'X', ' '},
        {' ', ' ', 'X', ' ', ' '},
        {' ', ' ', 'X', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {' ', 'X', ' ', ' ', ' '},
        {'X', ' ', ' ', ' ', ' '}
    },
    {
        {' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' '},
        {'X', 'X', 'X', 'X', 'X'},
        {' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' '}
    }
};

int fillPixelLineWithInt(int int_input, pixel_int_line_t char_output)
{
    int int_output, addfield;
    
    //Ermittelt Anzahl der Stellen der eingegebenen Zahl
    if (int_input < 0)
    {
    //Wenn int_input negativ ist, dann eine Stelle mehr wg. Vorzeichen
        addfield = 2;
    }
    else
    {
        addfield = 1;
    }
    int_output=log10((double)int_input)+ addfield;

    //Füllen des Arrays mit INT-wert
    char chararray[13];
    sprintf( chararray,"%d", int_input);
    
    int indexstelle = 0;


    for( int zeile = 0; zeile < 8; zeile = zeile + 1)
    {
        for( int index = indexstelle; index < int_output; index = index + 1)
        {

            for( int spalte = 0; spalte < 5; spalte = spalte + 1)
            {
                char_output[zeile][(spalte +(index * 6))]=matrix[chararray[index]-'0'][zeile][spalte];
            }
        }
    }


    for( int zeile = 0; zeile < 8; zeile = zeile + 1)
        {
            for( int spalte = 0; spalte < (6 * int_output); spalte = spalte + 1)
            {
                printf( "%c", char_output[zeile][spalte]);
            }
           cout << "\n";
        }


    //Rückgabe der Stellen
    return int_output;
}

Da die Pixelmatrix für '-' in matrix[11] abgelegt ist, wollte ich prüfen ob int_input negativ ist und wenn das der fall ist, sollen die ersten 5 bzw 6 spalten mit der Pixelmatrix für'-' gefüllt werden.
Ich dachte mir das so:

Code:
if (int_input < 0)
	{    
		for( int zeile = 0; zeile < 8; zeile = zeile + 1)
		{
			for( int spalte = 0; spalte < 5; spalte = spalte + 1)
			{
					char_output[zeile][spalte] = matrix[11][zeile][spalte];
					
			}
			
		}
		indexstelle = 1;
	}
Das programm gibt dann aber nichts aus.
Wo liegt der Fehler?

:confused:
 
Zuletzt bearbeitet:
Zurück