C++ : What is the usage of int('0') in this code? [duplicate] - c++

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 7 years ago.
This code Find the sum of all digits that occur in a string.
Example
sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2
Can anyone explain the need for use int('0') in this code!?
int sumUpDigits(std::string inputString) {
int answer = 0;
for (int i = 0; i < inputString.size(); i++) {
if ('1' <= inputString[i] && inputString[i] <= '9') {
answer += int(inputString[i]) - int('0');
}
}
return answer;
}

It converts char into ASCII code to make number out of string
int('9') - int('0') = 9

Related

Why does this program not give out binary output [duplicate]

This question already has answers here:
Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?
(11 answers)
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 2 days ago.
I'm new to C++ and this program that I wrote does not give the output, which is a binary form of an integer input.
It does give the result in Python Tutor. But in VSCode the result is always one less than the actual binary output.
Example-
5 = 100
6 = 109
17 = 10000
#include <iostream>
#include <cmath>
int main(){
int n;
std::cout << "Enter n:- ";
std::cin >> n;
int ans = 0;
int i = 0;
while (n != 0){
int bit = n & 1;
ans = (bit * pow(10, i)) + ans;
n = n >> 1;
i++;
}
std::cout << ans;
return 0;
}
What did I do wrong?

Why this c++ for loop is going into infinite loop [duplicate]

This question already has answers here:
vector.size() - 2 leads to infinite loop - C++ [duplicate]
(2 answers)
How do unsigned integers work
(3 answers)
How compare vector size with an integer? [closed]
(3 answers)
Closed 9 months ago.
The value for i keeps decreasing forever. Technically it should stop after first iteration because ans.size()-k = 0, but it doesn't stop. If I put 0 manually over there then it works completely fine.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> ans(1);
int k = 1;
for (int i = ans.size() - 1; i >= ans.size() - k; --i)
{
cout << i << endl;
}
}

Counting the number of certain characters in a string [duplicate]

This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 2 years ago.
I'm new to C++, and since my first computer language is Python, I don't really understand what I did wrong here.
The purpose of this code is to find out how many certain alphabets are included in a string of length 8, but I keep getting the following error:
ISO C++ forbids comparison between pointer and integer [-fpermissive]
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
string temp;
cin >> temp; // the input string will be of length 8
for (int i = 0; i < 8; i++) {
if (temp[i] == "F") {
cnt += 1;
};
};
cout << cnt << endl;
return 0;
}
When I print out temp[i] in the code, I can confirm that temp[i] is printed out as a single character, which I believe can be compared with another character, in this case the character "F".
I've been trying to find out why this is happening, but ended up coming here to ask for help.
if (temp[i] == "F") { should be if (temp[i] == 'F') {.
'F' is a character (char)
"F" is a C-string (const char[2] which decays to const char*).

Print a sequence of n^n using do while [duplicate]

This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
What is the C++ function to raise a number to a power?
(17 answers)
Closed 3 years ago.
Print a sequence of n^n for 1 to 15. This code only prints out 0. What am I doing wrong?
int main()
{
int i, n;
i = 0;
n=1;
do {
n = n^n;
cout << n;
n++;
i++;
}while(i<15);
return 0;

Int to char c++ [duplicate]

This question already has answers here:
Char to int conversion in C
(10 answers)
Convert an int to ASCII character
(11 answers)
Closed 6 years ago.
Can somebody explain me how the following tochar function works? As an experiment I added the following to tochar:
cout<<'0' + value
When run, I got a result of:
51 50 49 52
My code is:
static int tochar(int value)
{
return '0' + value;//This is the part i don't understand
}
int main()
{
char c[20];
int n = 4123;
int count = 0;
int number = log10(n)+1; //number of digits
for (int i = number; i >= 1; i--)
{
c[i] = tochar(n % 10);
n = n / 10;
count++;
}
for (int i = 1; i <=count; i++)
cout<<c[i];
system("pause");
}
You should change your function to
static char tochar(int value)
{
return '0' + value;//This is the part i don't understand
}
This function converts a single digit int to the character representing the digit. It assumes that 0-9 digits are consecutive in the char map, and works by computing the difference between the given argument and the '0' character.