I am new to C ++ and am working on some examples of the book "Principles and Practices of Programming Using C ++" (2nd edition). I wrote the following simple Program (in the Main.cpp file):
#include <iostream> #include <string> int main () { double d = 0; std::string s = ""; while (std::cin >> d >> s) { std::cout << "--" << d << " " << s << "\n"; } std::cout << "FATAL? "<< d << " " << u << "\n"; }
Compiling the program (on the command line) using CLang ( Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.2.0 Thread model: posix ):
clang++ -o Main -std=c++11 -stdlib=libc++ Main.cpp
works fine without errors. However, when I run the program, it behaves strangely. I tested the following input:
123m
that leads to
--123 m
which is excellent (this is also true for entering 123 m ). But by entering the following:
123a
leads to:
FATAL? 0 m
The same thing happens for most other characters (e.g. b , c , ...). Entering 123 a works fine (output: --123 a ).
Using GNU g ++ works, on the other hand. Also, the problem does not occur on a Linux machine compiling the same program with CLang.
As already mentioned, I am new to C ++ and this seems to be a Mac OS X problem. Is this a bug in the implementation of the Mac CLang or am I doing something seriously wrong here: (?
Thanks in advance!
source share