Has anyone seen this before?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include "std_lib_facilities.h"

//conert yen ('y' 0.0078), kroner ('k' 0.10), and Euro ('e' 1.05) into dollars (USD).
int main() {

	constexpr double yen = 0.0078;
	constexpr double kroner = 0.10;
	constexpr double euro = 1.05;
	double input = 0.0;
	char unit = ' ';

	cout << "Enter the amount of currency and unit to convert to US dollar (ex: 75y (y = Yen, k = kroner, e = Euro): ";
	cin >> input >> unit;

	switch (unit) {

	case 'y':case'Y':
			cout << input << " Yen" << " = $" << input * yen << '\n';
			break;
	case 'k':case'K':
			cout << input << " Kroner" << " = $" << input * kroner << '\n';
			break;
	case 'e':case'E':
			cout << input << " Euro" << " = $" << input * euro << '\n';
			break;
		default:
			cout << "Sorry, " << unit << "is not a recognized currency unit in this program!\n";
			break;
	}
}
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.
Last edited on
@Ynotnow,
You need to read up on "scientific notation".

You can enter floating-point numbers like 1.23e2

It will be fine for other chars with or without a space.
Last edited on
Thanks,

I'm a bit surprised the book didn't mention that fact. Much appreciate the quick response.
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.

Thank you for the assistance.
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.
Yes, I tend to agree. I have a feeling as I get further into my studies that particular twist will be discussed. It's definitely interesting.
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/

Learn C++ is frequently up-dated.

A decent C++20 book that I'd recommend is:
Beginning C++20: From Novice to Professional From Novice to Professional
https://www.amazon.com/gp/product/1484258835/

I have several other C/C++ books (eBooks actually) I'd recommend if interested.

Being a self-taught, still learning C++ programming hobbyist books and tutorial sites are useful, along with doing net searches for select topics.
Ynotnow wrote:
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.
Thanks, George P,

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.

Sincerely
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.

A toolkit I mashed together from this:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf
Topic archived. No new replies allowed.