![]() |
VOOZH | about |
Output: -727379968You might get some other negative value as output. So what is the problem here? The ints are not promoted to long long before multiplication, they remain ints and their product as well. Then the product is cast to long long, but we are late now as overflow has already occurred. Having one of x or y as long long should would work, as the other would be promoted. We can also use 1LL (or 1ll). LL is the suffix for long long, which is 64-bit on most C/C++ implementations. So 1LL, is a 1 of type long long.
Output: 1000000000000Here is another place where this trick can help you.
Output: 0
Input: 4 a b c d e f g h Output: 0 3 a b 3 c d 3 e fSo what is the problem here? This has little to do with the input you provided yourself but rather with the default behavior getline() exhibits. When you provided your input for the integer n (cin >> n), you not only submitted the following, but also an implicit newline was appended to the stream:
"4\n"A newline is always appended to your input when you select Enter or Return when submitting from a terminal. It is also used in files for moving toward the next line. The newline is left in the buffer after the extraction into n until the next I/O operation where it is either discarded or consumed. When the flow of control reaches getline(), the newline will be discarded, but the input will cease immediately. The reason this happens is because the default functionality of this function dictates that it should (it attempts to read a line and stops when it finds a newline). Because this leading newline inhibits the expected functionality of your program, it follows that it must be skipped our ignored somehow. One option is to call cin.ignore() after the first extraction. It will discard the next available character so that the newline is no longer intrusive. cin.ignore(n, delim); This extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.
Input: 4 a b c d e f g h Output: 3 a b 3 c d 3 e f 3 g hBut what if we don't know how many lines of input are going to be there? We can use this then:
Input: a b c d e f g h Output: a b c d e f g h