0

I dont understand the datatype char very well. Its because of that example:

char test;
cin >> test;
cout << test;

If i input now more than one char, the program only prints the first letter.

char test = (char)"Hello";

But If I cast a string like hello to a char, the program doesnt take the first letter.

void menu() {
    char mode = ' ';
    cout << "Gebe einen Modus an. 1 Addition, 2 Subtraktion, 3 Multiplikation, 4 Division: ";
    cin >> mode;
    switch (mode) {
    case '1':
        addition();
        break;
    case '2':
        subtraktion();
        break;
    case '3':
        multiplikation();
        break;
    case '4':
        division();
        break;
    default:
        cout << "Ungueltige Eingabe, versuch es nochmal\n";
        menu();
        break;

    }
}

Also in this example, If I give the program as an input more than one letter, the default condition will be executed so many times, as the length of the input.

I dont understand those three examples well, could anybody explain me everything in easy from the start=? That would be really nice! Thanks in advance

TheEnd1278
  • 47
  • 4

3 Answers3

0

char is for only one character. If you want to take multiple characters, you have to use array of char or string.

char test = (char)"Hello";
Please use this: char test[6] = "Hello";

  • 2
    `(char)"Hello"` says - take the **address** of the string literal, interpret it as an integer, and then truncate that integer so only one least-significant byte remains. `(char)"Hello"` does not produce character `H`, except perhaps by pure coincidence. – Igor Tandetnik Aug 01 '21 at 12:41
  • Yes, you are right. @IgorTandetnik. I was focusing on the questioner's string printing. I didn't notice about I might gave a wrong logic. But `(char)"Hello"` will produce an error. I have edited that line. – Kawshik Kumar Paul Aug 01 '21 at 12:46
0
  1. since you expect a type of char (which is one character, so one letter only) from stdin you will only get the first character you input.
  2. you can not cast a string literal "Hello" (which is a pointer-type: const char*) to char like that. You would have to do the following to only get the first character:
char test = *("Hello");

Learn more about pointers and references here: https://www.cplusplus.com/doc/tutorial/pointers/

D3PSI
  • 154
  • 2
  • 11
  • Ok, im understanding now the first two problems, but what about the third? stdin expects a type of char, so only one letter, but if I type more than one letter, it prints "ungueltige Eingabe..." multiple times. How does that work, if it only takes the first character= – TheEnd1278 Aug 01 '21 at 14:11
0

You're misunderstanding what a char is and mistaken it for a string which is an array of char with a terminating null character.

The char data type is used to store a single character.

Therefore, you will only get 1 character from the input. In order to get a stream of multiple characters you should use the datatype char* or std::string.

Wahalez
  • 489
  • 1
  • 6
  • 22