C++ can't compare string position and int [duplicate] - c++

This question already has answers here:
Explain integer comparison with promotion
(2 answers)
Does it matter if I don't explicitly cast an int to a char before comparison with a char?
(2 answers)
Closed 3 months ago.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string codigo = "12345678917";
int remainder = 7;
if (remainder == codigo[10]) {
cout<<"equal" << endl;
}
return 0;
}
It's just a simple comparison. This code doesnt print "equal".
I changed the type of the int to char or string but it didnt work. I tried comparison methods and still nothing. Am missing something here?
It did work when i changed remainder to '7'. But idk why comparing with variable doesnt work

in your code you are comparing an int to a char, in this situation there will be an implicit conversion from char to int.
using
cout << "int value: " << (int)codigo[10] << endl;
you can see that the int value of the character is 55, as 7 does not equal 55 the condition will not be true.
It also won't work if you just change the type to char as this will cast 7 to a char which is not the character '7'.
Using single quotes around the 7 causes the value to be a character literal, as it is stored in an int its value will be 55. Since this is equal to the character value of codigo[10] the condition will be true.

Related

Can't cast type int to char using function parameters C++ [duplicate]

This question already has answers here:
Convert an int to ASCII character
(11 answers)
Closed 6 months ago.
Hello im trying to understand why i can't cast type int
To a char using a user defined function with parameters.
Im following along with learncpp. And i am a beginner,
So please could i have the simplified versions.
If i create a user function, And try a return the value back it will just output the integer instead of an ASCII character.
Here is my following code.
int ascii(int y)
{
return static_cast<char>(y);
}
int main()
{
std::cout << ascii(5) << std::endl;
return 0;
}
The issue is the type of your return value. It should be a char. Not an int
char ascii(int y)
{
return static_cast<char>(y);
}

Difference between char in C and C++? [duplicate]

This question already has answers here:
Why are C character literals ints instead of chars?
(11 answers)
Closed 1 year ago.
I know that C and C++ are different languages.
Code - C
#include <stdio.h>
int main()
{
printf("%zu",sizeof('a'));
return 0;
}
Output
4
Code- C++
#include <iostream>
int main()
{
std::cout<<sizeof('a');
return 0;
}
Output
1
https://stackoverflow.com/a/14822074/11862989 in this answer user Kerrek SB(438k Rep.) telling about types in C++ nor mentions char neither int but just integral.
is char in C++ is integral type or strict char type ?
is char in C++ is integral type or strict char type ?
Character types, such as char, are integral types in C++.
The type of narrow character constant in C is int, while the type of narrow character literal in C++ is char.
is char in C++ is integral type or strict char type ?
Usage of type_traits lets you know the type:
#include <iostream>
#include <type_traits>
int main()
{
std::cout << std::is_integral<char>();
}
Output:
1
As others mentioned, In C 'a' is char constant and treated as an integer.
In C++ it is integral.
Also you can check the difference between char c = 'a' and 'a' in C++ by using RTTI (run time type info) as follows:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
char c = 'a';
// Get the type info using typeid operator
const type_info& ti2 = typeid('a');
const type_info& ti3 = typeid(c);
// Check if both types are same
if (ti2 != ti3)
cout << "different type" << endl;
else
cout << "same type"<< endl;
return 0;
}
The output is : same type.
However, char c = 'a' and 'a' are NOT same in C.

problem during typecasting during function calling [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 2 years ago.
In the program i am converting the character datatype into integer but it is displaying the ASCII code of the character, why? what should i do to print 4 as output?
void fun(int x)
{
cout<<x;
}
int main()
{
char ch='4';
fun((int)ch);
return 0;
}
I have tried changing the parameter from 'int x' to 'char x' and then typecasting in cout as 'cout<<(int)x;'
Casting a char to int will do what it should, cast the stored value into an integer. When you say char ch='4';, the variable holds the ASCII value of '4', it does not store the integer value 4. So casting it will not give you the integer value 4.
To get the integer value of ch (get 4 from '4'), we can subtract the integer value by '0' so that we get the actual integer value,
void fun(int x)
{
cout << x - '0';
}

How to calculate a string and store it in a variable? [duplicate]

This question already has answers here:
Evaluating arithmetic expressions from string in C++ [duplicate]
(7 answers)
Closed 3 years ago.
I'm writing a c++ calculator but I keep getting stuck on the part which changes the std::string into float variable for mathematical calculation.
I've already tried atoi and using 'var' (single-quote) but it seems to result in erratically large numbers and some variations of the code won't even compile saying "Line 13 Column 18 C:\Users\User\Desktop\calculator.cpp [Error] cannot convert 'std::string {aka std::basic_string}' to 'float' in initialization".
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{
std::cout << "Input arithmetic calculation: \n";
std::string calc;
std::cin >> calc;
atoi( calc.c_str() );
float result=calc;
std::cout << "Result = ";
std::cout << result << '\n';
}
I expect the output to be calculated such as
10*9=90
but it ends up as
10*9
or (when adding single-quote to calc in float result=calc)
1.93708e+009.
[...] I keep getting stuck on the part which changes the std::string into float variable for mathematical calculation.
Because:
you discard the value of atoi(). The usage would look like this: float result = atoi(calc.c_str());
atoi() doesn't do what you think it does. It does not perform any mathematical evaluations. It simply converts text that can be represented as a number, to said number, i.e. float x = atoi("5"); will yield x == 5. You can't use atoi() and expect it to perform mathematical calculations. It just converts.
You would need to implement this behaviour yourself.

& printing out the rest of a string instead of the address [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Printing C++ int pointer vs char pointer
(3 answers)
Closed 4 years ago.
Keep in mind that my knowledge of pointers is quite small, as I just started learning about them.
While I was messing around in C++, I wrote this small bit of code thinking it would just print out the address of each character in the string
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << &a[i] << endl;
}
return 0;
}
When I compiled and ran this, however, it resulted in it printing as if the string moved to the left.
It just doesn't make sense why when it uses &, which I thought would retrieve the address, would instead get the rest of the string.
As said in the comment, &a[i] is a pointer to a char, and << operator will print null terminated string starting from this character, not its address. So if you want to print the address, you must cast it to void *, as follow :
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << (void *)&a[i] << endl; //cast to (void *) to get the address
}
return 0;
}
string subscript ([]) operator returns char. So & operation returns a pointer to char. And cout operator<< has an overloading for it, which consider it should print out the parameter as a c-string. You should cast it to void* so cout wouldn't think it is a string.
(void*)&a[i]