17 AnswersNew Answer
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 >>
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.
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
The correct line is: cin >> character >> number; not cin >> character, number;
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.
thx ~ swim ~ but problem persists: num always 35
~ swim ~ now throws error, check if u wish
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');
Dennis thx, but the problem persists when pass a char in number, it should return its ASCII num
Did you enter 2 inputs? For example t 5 or t 5
both does the same, returns 0 when pass char as number
If you want the char to output its ascii value then you should also cast it to an int.
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
BinaryEden Did you read my comment? trying to read a char in an int variable using cin will terminate the input.
yes, read it now I used scanf and printf and don't know why, now char empty and int 35
BinaryEden I tried the input as b 70 or b 70 and got what i entered.
yes, but mind while enter just chars it should return ascii num im using scanf and printf now maybe a sololearn's compiler bug?