lundi 25 décembre 2017

C++ 11 int8_t buggy input/output

guys. This is a snipped of code from my program. I am using the int8_t data type, and I'm having some problems with input/output. Apparently, using int8_t in the program requires the -std=c++11 flag to be set for the g++ compiler. I did that, but there are runtime errors. Here's my code:

#include <iostream>
#include <cstdint>
using std::cout;
using std::cin;
using std::endl;
int main() {
  int8_t number;

  cout << "Enter a number [1-100]: ";
  cin >> number;
  cout << "You entered: " << number << endl;
  cout << number << " / 10 = " << (number / 10) << endl;

  return 0;
}

Here is the output of the program:

 $ ./a.out
Enter a number [1-100]: 95
You entered: 9
9 / 10 = 5

The $ is the shell prompt. Anyway, it seems that only the first digit is read from the input stream. Even so, the integer division 9 / 10 should return 0, not 5. This program is intended to do 95 / 10 and return a result of 9. Theoretically, this program should work, but apparently, even the division operator is buggy. I should make a note that using int instead of int8_t causes the program to work as intended. I suspect that in this case someone would just tell me to use the int data type and deal with it. But we are software engineers! I would like to know if the int8_t data type is inherently flawed. Why it can't accept proper user input, but also breaks the division operator?! Any programming language worth using must be free of such imperfections. And I think that C++ is popular enough that eventually someone realized that int8_t is flawed in some sense. That is something I need to know the intricacies of.

Aucun commentaire:

Enregistrer un commentaire