C++ :: Aufgabe #1 :: Lösung #4

12 Lösungen Lösungen öffentlich
#1

Zeichen als Dezimal- bzw. Hexadezimalwert ausgeben

Anfänger - C++ von Gustl - 12.08.2012 um 15:26 Uhr
Schreiben Sie ein Konsolenprogramm, das ein Zeichen einliest und dieses Zeichen sowohl
als Zeichen als auch als Dezimal- bzw. Hexadezimalwert ausgibt.
Verwenden Sie sowohl scanf als auch cin für die Eingabe, bei der Ausgabe probieren Sie
cout und printf aus.
#4
vote_ok
von Lex (20 Punkte) - 01.12.2015 um 20:07 Uhr
Quellcode ausblenden C-Code
#include <iostream> //for 'cout' and 'cin'
#include <cstdio> //for 'printf' and 'scanf'

int main()
{
    char a,b;
    int int_b = 0;

    printf("Using 'printf' and 'scanf' from cstdio.\n");
    printf("Please enter one character!\n");
    scanf("%s", &a);
    printf("The corosponding decimal number is: %d\n", a);
    printf("The corosponding hexadecimal number is: %x\n\n", a);

    std::cout << "Using 'cout' and 'cin' from iostream.\n";
    std::cout << "Please enter one character!\n";
    std::cin >> b;
    int_b = static_cast<int>(b);
    
    std::cout << "The corosponding decimal number is: "
              <<  int_b << std::endl;
    std::cout << "The corosponding hexadecimal number is: "
              << std::hex << int_b << std::endl;
    return 0;
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben