How getchar() function output - c++

I want to understand how is getchar() function is working here?
I read getchar() returns the next character from stdin, or EOF if the end of file is reached.
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int decimal;
while(!isdigit(decimal=getchar()));
cout<<decimal;
}
I give input 25. It outputs 50. I don't understand why?
How is it giving 50.

getchar() reads a single character from the input stream and returns it's value. In your case, that is the character '2'. Most implementations (including yours it seems) use ASCII encoding where the character '2' has the value 50. The value assigned to decimal is therefore 50. Since decimal is an int, std::cout interprets it as a numeric value and prints it accordingly.

decimal is storing the first digit character it finds, which happens to be '2'. You're storing the value to an int, so cout outputs the ordinal value of decimal. The ASCII ordinal value of '2' is 50. You never even reached the 5 you entered.
Simple fix to make it display the character, not the ordinal value, would be to change the output code to:
cout << (char)decimal;

When you enter 25 it reads first character from this input. And first character is 2 here. ASCII value of 2 is 50. That's why you get 50 at the output.
If you want to see 2 at the output use like this
cout << (char) decimal << endl;
Here type-casting 50 into character. That is 2.

The C library function int getchar(void) gets a character (an unsigned char) from stdin.
Moreover, decimal is an integer type and isdigit(decimal) will check the character at ASCII decimal position.
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int decimal;
while(!isdigit(decimal=getchar()));\\when you input 25. It first gets 2.
\\ 2 gets stored as 50 inside decimal
\\ isdigit() is called which returns true for 50 which is ASCII of 2 and while breaks
cout<<decimal; \\ 50 is printed here. Type cast it to print 2.
}

Related

How to print the word but with the first letter as a capital

Hello I am trying to do problem word capitalization on code forces and to do the problem i am trying to use ascii table(my professor said I cant use cmath and anything other than loops arrays ascii table and the basics) it prints the number of the capital letter on the ascii lets say the ascii code for small a is 96 and capital A is 100 it prints 100 not A here's my code.
#include <iostream>
using namespace std;
int main()
{
string s;
cin>>s;
char x=s[0];
cout<<x+22<<s;
}
Because 22 is an int, the type of x+22 is also int. You need to convert it back into a char for iostream to interpret it as a character:
cout << char(x+22) << s;
Note that I only addressed what you asked about: A number being printed instead of a character. There might be other small errors in there for you to find.

Slicing string character correctly in C++

I'd like to count number 1 in my input, for example,111 (1+1+1) must return 3and
101must return 2 (1+1)
To achieve this,I developed sample code as follows.
#include <iostream>
using namespace std;
int main(){
string S;
cout<<"input number";
cin>>S;
cout<<"S[0]:"<<S[0]<<endl;
cout<<"S[1]:"<<S[1]<<endl;
cout<<"S[2]:"<<S[2]<<endl;
int T = (int) (S[0]+S[1]+S[2]);
cout<<"T:"<<T<<endl;
return 0;
}
But when I execute this code I input 111 for example and my expected return is 3 but it returned 147.
[ec2-user#ip-10-0-1-187 atcoder]$ ./a.out
input number
111
S[0]:1
S[1]:1
S[2]:1
T:147
What is the wrong point of that ? I am totally novice, so that if someone has opinion,please let me know. Thanks
It's because S[0] is a char. You are adding the character values of these digits, rather than the numerical value. In ASCII, numerical digits start at value 48. In other words, each of your 3 values are exactly 48 too big.
So instead of doing 1+1+1, you're doing 49+49+49.
The simplest way to convert from character value to digit is to subtract 48, which is the value of 0.
e.g, S[0] - '0'.
Since your goal is to count the occurrences of a character, it makes no sense to sum the characters together. I recommend this:
std::cout << std::ranges::count(S, '1');
To explain the output that you get, characters are integers whose values represent various symbols (and non-printable control characters). The value that represents the symbol '1' is not 1. '1'+'1'+'1' is not '3'.

last character of a string in c++ using n.back() and casting

my input is 12. Why is the output 50?
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string n;
cin>>n;
cout << (int)n.back();
}
my input is 12. Why is the output 50?
Because the value that encodes the code unit '2' in the character encoding that is in use on your system happens to be 50.
If you wish to print the symbol that the code unit represents, then you must not cast to int type, and instead insert a character type:
std::cout << n.back();
If you wish to map a character representing '0'..'9' to the corresponding integer value, you can use character - '0'. Why this works: Regardless of what value is used to represent the digit '0', subtracting its own value from itself will produce the value 0 because z - z == 0. The other digits work because of the fact that all digits are represented by contiguous values starting with '0'. Hence, '1' will be represented by '0' + 1 and z + 1 - z == 1.

Conversion from int to char failed and console prints weird symbol

I am having a weird issue and I don't know how to explain it. When I run this code it prints this symbol -> .
This is my code:
#include <iostream>
int main() {
int num = 1;
char number = num;
std::cout<<number<<std::endl;
system("PAUSE");
return 0;
}
I don't understand why. Normally it should convert the integer to char. I am using Dev C++ and my language standard is ISO C++11. I am programming for 4 years now and this is the first time I get something like this. I hope I explained my issue and if someone can help me I will be grateful.
Conversion from int to char failed
Actually, int was successfully converted to char.
Normally it should convert the integer to char.
That's what it did. The result of the conversion is char with the value 1.
Computers use a "character encoding". Each symbol that you see on the screen is encoded as a number. For example (assuming ASCII or compatible encoding) the value of 'a' character is 97.
A char with value of 1 is not the same as char with the value that encodes the character '1'. As such, when you print a character with value 1, you don't see the number 1, but the character that the value 1 encodes. In the ASCII and compatible encodings, 1 encodes a non-visible symbol "start of heading".
I wanted to print 1 as a char.
You can do it like this:
std::cout << '1' << '\n';
Then, since 4 years you seem to misunderstand what char is. It's not directly a character, but a number. It's the encoding that turns that number into a readable character.
Essentially, char i=1 is not the same as char i='1' (ascii table).

c++ Using _getch() for instant user input, but it keeps returning different numbers

int choiceOne = 0;
choiceOne = _getch();
cout << choiceOne;
_getch();
system("CLS");
I would like choiceOne to = what the user enters, but it outputs (48+ The user input)
So if I enter 0 it will output 48, if I enter 5 it will output 53. I'm not sure where the 48 is coming from.
If more code is necessary I can post it.
you are reading in character variables but storing them in an a variable of type int. this will convert the input from char to int. what you are seeing is the corresponding ASCII integer values for the characters you are inputting. alter choiceOne to be a variable of type char for you code to work, and search for the ASCII table online to get a full reference of all the ASCII codes for each standard character
_getch returns ASCII coding. If you press 0, it gets ASCII coding of the character '0', which in hex is 0x30, and in dec is 48.