base template always return 0 | Sololearn: Learn to code for FREE!

0

base template always return 0

but the template specialization works properly, why? https://code.sololearn.com/coCFFSxB4Q2r/?ref=app

8/16/2019 9:41:15 AM

BinaryEden

17 Answers

New Answer

+2

BinaryEden It's not prompting for input. This is sololearn issue. A cpp file without cin will not prompt for input. Add this line anywhere (add after header) #define cin >>

+2

BinaryEden It's not a bug, it's the way it is. You cannot read a char into an int field while taking an input, be it using cin >> or scanf, since the input is formatted. One should use unformatted input functions like (cin.get() or fgetc) to achieve that. If you already have a char, then assigning to int will convert it to it's ascii value.

+2

BinaryEden Well what i said yesterday about cin and scanf holds true, but i completely forgot about unformatted input. When you read data with scanf or cin >>, then the data read is formatted input that is why due to format mismatch you get wrong results. But if you use unformatted (the function does not try to interpret the type of data, it just reads raw data) input, you can get what you are looking for int x = cin.get(); // and you enter 'A', then x will have ascii value of 'A' which is 65 The same is achieved in C using fgetc function

+1

The correct line is: cin >> character >> number; not cin >> character, number;

+1

BinaryEden You want to read an int, if you specify a char, the input will be terminated immediately. And if there is anything before entering a char, it will be assigned to int else 0 will be assigned try this 123a - Input will terminate on entering char 'a' and 123 will be assigned to int.

+1

thx ~ swim ~ but problem persists: num always 35

+1

~ swim ~ now throws error, check if u wish

+1

BinaryEden Move #define cin>> after main and you need to enter data as say 'Ba' (B will go in int and 'a' in char. Input on separate line will not work because the second read will read newline char instead of actual char. To make it work with input on separate line, you need to write like this n = cin.get(); cin.ignore(100,'\n'); // ignore next 100 chars or '\n' whichever happens first c = cin.get(); cin.ignore(100,'\n');

0

Dennis thx, but the problem persists when pass a char in number, it should return its ASCII num

0

Did you enter 2 inputs? For example t 5 or t 5

0

both does the same, returns 0 when pass char as number

0

If you want the char to output its ascii value then you should also cast it to an int.

0

Dennis casted it in the output f() but no works int(var) or int(var) so it must be casted before passing it to a func will use scanf and printf to doit, C always saving your ass

0

BinaryEden Did you read my comment? trying to read a char in an int variable using cin will terminate the input.

0

yes, read it now I used scanf and printf and don't know why, now char empty and int 35

0

BinaryEden I tried the input as b 70 or b 70 and got what i entered.

0

yes, but mind while enter just chars it should return ascii num im using scanf and printf now maybe a sololearn's compiler bug?