2

Write a program to display the sizes of basic four datatypes i.e integer, double, float, character.

Input: The first line of input contains integer T denoting the number of test cases. For each test case, the user can input any of the above data types.

Output: For each test case, there is a single line output displaying the size of that data type.

Constraints: 1<=T<=100

Example:

Input:

4 
1
@
7.98
9.985647851

Output:

4
1
4
8

I tried this

int main() {
    //code
    int x;
    cin>>x;
    std::string s;
    for(int i = 0;i<x;i++){
        cin>>s;
        cout << sizeof(s) << endl;
    }
    return 0;
}

Output was

32
32

Wrong Answer

paddy
  • 60,864
  • 6
  • 61
  • 103
Rahul Goyal
  • 121
  • 1
  • 5
  • Side note: Watch out with `endl`. It is both a new line and a stream flush, and the stream flush can be prohibitively expensive in competition (and often in real life). If all you want is a new line, print the new line character alone: `'\n'`. – user4581301 Jan 31 '19 at 06:19
  • This was a question provided in geeksforgeeks practice questions. But how be can detect typeof value being inputed and displaying its size in this way as stated in program. – Rahul Goyal Jan 31 '19 at 06:19
  • 2
    You cannot print out the size of the `string` because that will just give you the size of a `string`. You can read in strings, but then you will have to parse the string to find out what data type the string represents and then print out the size of the data type. – user4581301 Jan 31 '19 at 06:26
  • Don't forget to account for invalid input, spurious extra data, negative values, scientific notation, and valid ranges if specified. Usually, you should account for stuff like this unless it is explicitly guaranteed _not_ to appear in the input... Unless contest-style questions have gone soft in modern times. – paddy Jan 31 '19 at 06:44
  • @molbdnilo May be, in case of ambiguity, a "best match" is assumed. (I.e. if it could be `int`, `float`, `double` then prefer `int`.) The expected output let me think this. – Scheff's Cat Jan 31 '19 at 06:46
  • @Scheff The two last inputs can't be represented as either `float` or `double`, so it's very unclear how anyone could arrive at those results. – molbdnilo Jan 31 '19 at 06:56
  • geeksforgeeks is a terrible site, full of errors, guesses, and misconceptions. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 31 '19 at 06:58
  • @molbdnilo Please, don't get me wrong. I don't think it's the best question ever read (but also not the worst). This ambiguity could still be solved with the above suggested logic. (If it is readable as `float` or `double` prefer `float`.) But I just saw you don't like geeks for geeks in general... ;-) – Scheff's Cat Jan 31 '19 at 06:59

1 Answers1

0

I started to work on this problem to provide the OP with some insight however I have come to conclusion that many others have within the comment section of the OPs original question: and that is that this question or problem has an indeterminate solution!


Reasoning:

If the user enters any of the following digits as a single entity for input { 0, 1, ... 9 } into the console this can be at the least interpreted as an int or a char type and at worst even a possible double, but without the '.' character being present we could eliminate that as a candidate. This problem definitely has ambiguity to it. Checking to see if it is a float or a double is easy; all one has to do is check the string to see if there is at least a '.' then it's either a double or a float then check the last character of the string to see if it is a f and if so then it is a float. Checking to see if it is a character that is a non digit is easy, however distinguishing a single character digit between a char and an int is the troublesome part!


Work around:

You could conclude that if the input string is a single character and is a non digit then it is definitely a char type.

You could conclude that if the input string is a single character and is a digit ASCII[48 - 57] then you could conclude that it is an int type. This would be considered a restraint.

You could conclude that if it isn't the above two it is at least a float or a double and it is a float if and only if the last character of the string is a f, but a . must be present for it to be either of the two. Again these would be restraints that you would put on the accepted data input.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59