#include <iostream>
using std::cout;
using std::endl;
int main(void) {
std::string fx = "6x^2+6x+4";
int part1 = fx[0] * fx[3];
cout << fx[0] << endl;
cout << fx[3] << endl;
cout << part1;
}
So I have this string and fx[0] and fx[3] are obviously integers: when I print them to the console they print out just fine; however, part1 (their multiplication) equals some totally unrelated number? Can anyone help?
Here is the output:
6
2
2700
Your fx[0] and fx[3] variables are of type char (which is an integer type in C++). However, the actual values in those two elements of your fx string will be representations of the digits, 6 and 2, not the numerical values of those digits.
Very often, those representations will be ASCII codes (but that's not required); however, what is required is that the representations of the digits 0 thru 9 have contiguous, sequential values. Thus, by subtracting the value of the digit, 0, we can convert to their numerical representations.
In your case, the following line will do the conversion:
int part1 = (fx[0]-'0') * (fx[3]-'0');
The reason why you see the correct values when printing fx[0] and fx[3] is because the version of the cout << operator that takes a char argument is designed to print the represented character (not its 'ASCII' code); however, the cout << operator for an int type (like your part1) will print the actual value represented internally. (Try changing one of your lines to cout << (int)fx[0] << endl; to see the difference.)
P.S. Don't forget the #include <string> header – some implementations do that implicity inside the <iostream> header, but don't rely on that!
well, first of all, string::operator[] returns a char... then, a char can be casted to an int, and the cast works checking the ID in the ASCII table (in your case)
In ASCII, the ID of "6" and "2" are respectively 54 and 52 (you can check it here for example)... so your program is taking the two char, casting them to int, and multiplying them (54 * 50 = 2700)
If you need to interpret those as the integer value they represent, you can check this answer:
int val = '6' - '0'; // val == 6
Characters are values representing glyphs from some representation, usually the ASCII table. The numeric value of a character is not the same as the glyph that is printed on the screen. To convert a numeric-looking char to an actual "0-based" numeric value, subtract '0' from your char value.
(fx[3]-'0']) will be the numeric value of character represented at position 3.
You are multiplying character types. so the characters '6' and '2' will converted to its integer values 54 and 50 respectively then multiplication is applied. This works based on C++ type conversion rule. Then you will get 2,700. Try the modified sample code
#include <iostream>
using std::cout;
using std::endl;
int main(void) {
std::string fx = "6x^2+6x+4";
int part1 = fx[0] * fx[3];
cout << fx[0] << endl;
cout << fx[3] << endl;
cout << part1;
cout << std::endl;
cout << (int)fx[0] << " " << (int)fx[3] << std::endl;
}
And the results
6
2
2700
54 50
Related
The code asks for a positive integer, than the first output shows the corresponding ASCII code, the others are made to convert the integer to decimal, octal and hexadecimal equivalents. I understand the logic of the code, but I don't understand the assignment made on line 10 c=code than the assignment made on line 12 code=c. What happens on background when we 'swap' the two variables.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
unsigned char c = 0;
unsigned int code = 0;
cout << "\nPlease enter a decimal character code: ";
cin >> code;
c = code;
cout << "\nThe corresponding character: " << c << endl;
code = c;
cout << "\nCharacter codes"
<< "\n decimal: " << setw(3) << dec << code
<< "\n octal: " << setw(3) << oct << code
<< "\n hexadecimal: " << setw(3) << hex << code
<< endl;
return 0;
}
I could be wrong here so maybe someone else can weigh in, but I believe I know the answer.
If you assign a character a number, when you print that char it prints the corresponding character. Since c is of type char, the line c = code converts the integer entered into a character. You can test this yourself by assigning any int to a char variable and printing it out.
The second assignment, code = c, seems to be completely unnecessary.
That's not a swap. c is assigned the same value as code and then this value is assigned back to code. The original value of code is lost.
We can see this because unsigned char c is (usually) much smaller than unsigned int code and some information may be lost stuffing the value in code into c.
For example, code = 257. After c = code; code is still 257 and c, assuming an 8 bit char will be 1. After code = c;, both code and c will be 1. 257 has been lost.
Why is this being done? when given a char, operator<< will print out the character encoded, completely ignoring the request to print as hex, dec, or oct. So
<< "\n decimal: " << setw(3) << dec << c
is wasted. Given an int << will respect the modifiers, but if c and code have different values, you're comparing apples and Sasquatches.
string str = "abcdefg";
cout << str.back() + str.front();
C++ console:
200
#include <iostream>
int main() {
std::string str = "abcdefg";
for(int v : str) {
std::cout << v << " " << static_cast<char>(v) << "\n";
}
}
Possible output:
97 a // front()
98 b
99 c
100 d
101 e
102 f
103 g // back()
97+103 = 200
When i run the following code adding str.front() + str.back it gives me 200 but why?
Characters are encoded as integers. char is an integer type. When operands of an arithmetic operation (such as addition) are integer types smaller than int, those operands are promoted to int, which is also the result type of the expression. That is why the output is not an encoded character, but decimal representation of the integer instead.
Why the value 200, well it just so happens that in the character encoding of your system, the value that represents 'a' and the value that represents 'g' total 200 when added together.
You're adding two chars, which results in an integer.
If you want to combine the two characters to a string, you have to give at least one of them string type, e.g. :
cout << str.back() + string{str.front()};
or
cout << str.back() + str.substr(0, 1);
If you just want to output two characters, though - you can simply output them one at a time:
cout << str.back() << str.front();
I wrote the following code
#include <iostream>
#define circleArea(r) (3.1415*r*r)
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << circleArea('10') << std::endl;
std::cout << 3.1415*'10'*'10' << std::endl;
std::cout << 3.1415*10*10 << std::endl;
return 0;
}
The output was the following
Hello, World!
4.98111e+08
4.98111e+08
314.15
The doubt i have is why is 3.1415 * '10'*'10' value 4.98111e+08. i thought when i multiply a string by a number, number will be converted to a string yielding a string.Am i missing something here?
EDIT: Rephrasing question based on comments, i understood that single quotes and double are not same. So, '1' represents a single character. But, what does '10' represent
'10' is a multicharacter literal; note well the use of single quotation marks. It has a type int, and its value is implementation defined. Cf. "10" which is a literal of type const char[3], with the final element of that array set to NUL.
Typically its value is '1' * 256 + '0', which in ASCII (a common encoding supported by C++) is 49 * 256 + 48 which is 12592.
I'm learning about ASCII, and how it encodes characters. To my understanding (although I may be wrong because I'm still learning), it is that ASCII encodes numbers as characters. For example, something like this:
0011 0000 = 30 = '0' (this being an encoded character)
0000 0000 = 0 = 0 (while this being an actual number)
represents two different types of encoding of zero.
Question
Is there any way to display the true number for an ASCII char data type?
I attempted the following code:
#include <iostream>
int main(void) {
char e = '0';
std::cout << e << std::endl;
std::cout << sizeof(e) << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
I know this code is not close to the result I am trying to accomplish; however, it was all I could think of. I need a better understanding of how this conversion can be accomplished through C++ syntax.
Also, please excuse me of any misleading information or title. I am still learning the "ropes" of programming.
Cast the char to an int.
std::cout << static_cast<int>(e);
The char actually already is an integer, it's just that the overload of operator<< taking a char displays it as a character. By using the cast to change the type to int, we are causing a different overload of the operator to be called, one which is written to display as an integer.
As per my understanding you want the actual values stored in memory that is for '0'=0x30=48. In memory char data type is also stored as integer but in 8 bit format. Because of this if you type cast the variable with int then you will get the stored value in integer format not in Ascii char.
#include <iostream>
int main(void) {
char e = '0';
std::cout << (int)e << std::endl;
std::cout << sizeof(e) << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
Something like this
#include <iostream>
int main(void) {
char e = '0';
std::cout << e - '\0' << std::endl;
std::cout << sizeof(e) << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
Try it with char e equal any character and it will print the actual value of the character. Thanks to chris for suggesting another way of doing this; which is by casting the character to int.
std::cout << static_cast<int>(e) << std::endl;
I need a function that returns the ASCII value of a character, including spaces, tabs, newlines, etc...
On a similar note, what is the function that converts between hexadecimal, decimal, and binary numbers?
char c;
int ascii = (int) c;
s2.data[j]=(char)count;
A char is an integer, no need for conversion functions.
Maybe you are looking for functions that display integers as a string - using hex, binary or decimal representations?
You don't need a function to get the ASCII value -- just convert to an integer by an (implicit) cast:
int x = 'A'; // x = 65
int y = '\t'; // x = 9
To convert a number to hexadecimal or decimal, you can use any of the members of the printf family:
char buffer[32]; // make sure this is big enough!
sprintf(buffer, "%d", 12345); // decimal: buffer is assigned "12345"
sprintf(buffer, "%x", 12345); // hex: buffer is assigned "3039"
There is no built-in function to convert to binary; you'll have to roll your own.
If you want to get the ASCII value of a character in your code, just put the character in quotes
char c = 'a';
You may be confusing internal representation with output. To see what value a character has:
char c = 'A';
cout << c << " has code " << int(c) << endl;
Similarly fo hex valuwes - all numbers are hexadecimal numbers, so it's just a question of output:
int n = 42;
cout << n << " in hex is " << hex << n << endl;
The "hex" in the output statement is a C++ manipulator. There are manipulators for hex and decimal (dec), but unfortunately not for binary.
As far as hex & binary - those are just representations of integers. What you probably want is something like printf("%d",n), and printf("%x",n) - the first prints the decimal, the second the hex version of the same number. Clarify what you are trying to do -