Hello, I am studying the Bjarne Stroustrup Programming Principles and Practice Using C++ (Second Edition). I created this program to convert 3 currencies to the US dollar. The first version of the program used if-statements and worked fine. This one was converted to use switch statements. It works fine with the exception of my last case 'e':case'E': I ask for the user input value of currency followed by the unit of currency (e for Euro). It works if I add a space between the currency and the unit (500 e or E), but it should not matter, as the Yen and Kroner cases recognize (500y or 500Y or 500k or 500K). In my troubleshooting, I've swapped out the (e and E) with any other letters and it works as expected without using a space between the currency value and unit, for instance, I tried this case by swapping e/E with a/A without issue, it's like just the letter e/E is acting up. Below is my code; also, the header file is provided by the book and downloaded from the Author's website.
Any advice is greatly appreciated. I have a feeling it's a non-issue and probably would go away if I was to delete this program and retype it but I am more curious as to what would cause this so I could keep an eye out for it in the future.
Actually yes: you need to enter a space on line 13 for the currency symbol. The white space is a separator the stream needs to determine where an entry starts and ends.
If you don't want a space you need to read it as a [std::]string and separate it yourself.
[EDIT]
The stream seems to be able to distinguish numeric from non numeric.
The problem with 'e' is that it determines the exponent which is part of the double and hence still numeric and not recognized as a char on its own.
The first version of the program used if-statements and worked fine.
I wonder how, if the input-part (cin >> input >> unit;) was the same.
Does the book explicitly say that input like "3.14Y" is ok, or does it just assume that the user types "3.14 Y"?
(It should be clear now that a user can write anything and the program must check and cope with it all. Input is not trivial.)
You are correct, I went back through my notes and for the section on if-statements and I noted that it acted the same with 'e". I think I was just tired and irritated and that fact slipped my mind.
As for the book, the author used British Pound but I chose the Euro. However, the book shows the input like 500y for Yen, 500K for Korner, and 500p for pounds. Anyhow, for now, I am adding a space between the value of the currency and the unit. I am only on chapter 4 of a very big book so I'm hoping and looking forward to learning a great deal.
As for the book, the author used British Pound but I chose the Euro.
In other words the author either chose to not use less trivial examples and omitted footnote about the twists, or did not even remember that scientific notation, hexadecimal values, etc exists.
Saint Bjarne's C++ Programming book is good, after all he created C++, but it is out-dated. No C++17, no C++20. A lot has changed with C++ since the book was written and published.
Relying on a single source without the feedback of classroom instruction for learning C++ one can miss some things C++11/14 can bring to the programming toolbox. After all C++11 is unofficially recognized as being "Modern C++."
I'd recommend using multiple books if at all possible, along with an online tutorial that's free: Learn C++ https://www.learncpp.com/
I'm a bit surprised the book didn't mention that fact.
The book is derived from Stroustrup's uni lectures, a classroom environment is easier to notice and talk about certain "problems" that can happen.
C++ is HUGE, and the learning curve is quite steep at the start. Especially if relying on a single book for instruction.
I've been trying to learn C++ since before C++98 was the standard. Asking questions of the experts here in the last couple of years certainly has helped.
I totally agree, I have attempted C++ a few times in the past few years. This time I have made a personal commitment to stick with it this time. I will surely check out the book you recommended. I have been utilizing Google, Youtube, and this website for answers but this is my first posting. I am pleased with the positive and helpful comments I am receiving. I'm sure I will be reaching out in the forum much more in the future.
I tend to avoid youtube since it is a classroom experience with someone lecturing, but no way to ask questions if some point is unclear.
Plus, my opinion now, most people who do youtube videos are some of the worst people to try to learn from. They have little to no experience of teaching others.
I read books, lurk in online tutorials, ask questions here.
And I code to death some aspect of C++ I'm interested in learning. I have several personal use mini-libraries I created for doing various things. Want to generate some random numbers? I add to the project my header-only/C++20 module interface file for my random number toolkit.