simple input program issue

Hi, I have a problem where I need some help. The below program is a simple conversion from Dollar to Euro and vice-versa.
The issue I have with entering the values for "Euros". In this case, a number followed by a letter D or E.
Now, when I enter 1.22D everything is ok, when I enter 1.22 E I receive the text in the else statement. It does not recognize the E character as such. Any other character (from A to Z does work, only E is translated to a space...
any ideas?
I made the same program but with a switch - case instead of If else, same result.

I have Windows 10, latest updates and Visual Studio Community 2017 version 15.5.3

(the program is a try it from the C++ book Bjarne Stroustrup second edition)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include "stdafx.h"
#include "std_lib_facilities.h"

int main()
{
	constexpr double dollar = 0.803674;
	double amount = 0;
	char unit = ' ';
	cout << "Please enter an amount followed by a currency D or E):\n";
	cin >> amount >> unit;
	if (unit == 'D')
		cout << amount << "Dollar==" << dollar * amount << "Euro\n";
	else if (unit == 'E')
		cout << amount << "Euro==" << amount / dollar << "Dollar\n";
	else
		cout << "Sorry, I do not recognize the currency specified: " << int(unit) << "\n";
	return 0;
}
I compiled the program and didn't have any issues. I used #include <iostream> and using namespace std instead of #include "std_lib_facilities.h"

Input: 1.22 E
Output:
1.22Euro==1.51803Dollar
Hello bedmylle,

Just looking at your program what I see is when you would enter "1.22D" or "1.22E" what happens is cin >> amount >> unit; is extracting the amount correctly, as the formatted input will extract the number up to the first non numeric number, white space or new line that is encountered, but the letter may not be extracted into "unit". Since "unit" was initialized to a space this could also be a problem.

A simple solution would be to make your entry as "1.22 D" with a space between the number and letter. This way each variable would get what it needs. Try this while I load up the program and check it.

Hope that helps,

Andy
Hello bedmylle,

A couple of points to start with:

1. "stdasx.h" is specific to VS and you should try not to include it in your post.

2. "std_lib_facilities.h" is not a C++ header file and also not available to me with my VS. Try not to use any header file that ends in ".h" in a C++ program.

3. If you will be using "std::cout", "std::cin" you will need to include the header file "iostream".

4. Avoid using "using namespace std;" as it WILL get you in trouble some day. Refer to #3 for writing things in the "std" namespace.

In the program below you will see how blank lines make the program easier to read and see what is happening. Also I made some changes to the output to make it easier to read. Mostly to show you what can be done. Also I included the header file <string> and offer a different way to extract the number and letter.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//#include "std_lib_facilities.h"
#include <iostream>
#include <string>

int main()
{
	constexpr double dollar = 0.803674;

	double amount{};  // <--- In your way it should have been 0.0 not the single 0.
	char unit{};  // <--- This way of initializing a variable is from C++11 on. Also will initializze as '\0'.
	char unit2{};
	std::string line;

	std::cout << "Please enter an amount followed by a currency D or E):\n";

	//std::cin >> amount >> unit;  // <--- A space between each variable for input is best. The "E" seems to be a problem.
	std::getline(std::cin, line);

	amount = std::stof(line);
	unit = line[line.length() - 1];

	if (unit == 'D')
		std::cout << amount << " Dollar = " << dollar * amount << " Euro\n";c
	else if (unit == 'E')
		std::cout << amount << " Euro = " << amount / dollar << " Dollar\n";//#include "std_lib_facilities.h"
#include <iostream>
#include <string>

int main()
{
	constexpr double dollar = 0.803674;

	double amount{};  // <--- In your way it should have been 0.0 not the single 0.
	char unit{};  // <--- This way of initializing a variable is from C++11 on. Also will initialize as '\0'.
	char unit2{};
	std::string line;

	std::cout << "Please enter an amount followed by a currency D or E):\n";

	//std::cin >> amount >> unit;  // <--- A space between each variable for input is best. The "E" seems to be a problem.
	std::getline(std::cin, line);

	amount = std::stof(line);
	unit = line[line.length() - 1];

	if (unit == 'D')
		std::cout << amount << " Dollar = " << dollar * amount << " Euro\n";
	else if (unit == 'E')
		std::cout << amount << " Euro = " << amount / dollar << " Dollar\n";
	else
		std::cout << "Sorry, I do not recognize the currency specified: " << unit << "\n";  // <--- Removed the type cast.

	return 0;
}

Comments in the program should help you understand.

In your way the best solution is to put a space between the amount and the letter or use the string method, which will work for any size number.

Hope that helps,

Andy

Edit:
Last edited on
thank you all so far for these replies. I can agree with all the suggestions but... what is confusing to me is "why the E???". All the rest seem to work.
Also, the std_lib_facilities is a required by the C++ programming principles by Bjarne Stroustrup (page 45 "the classic first program"...), that is the book I use to learn C++ ...

danny
PS: removing the #include "stdafx.h" results in a compilation error...

danny
Because e or E is a valid part of a floating-point number - for example 2e3 means 2000.

I suspect that 2e just translates as 2e0 or just 2 ... with no following unit.

That's scientific notation.
> Now, when I enter 1.22D everything is ok, when I enter 1.22 E I receive the text in the else statement.
> It does not recognize the E character as such.

Your program is fine as originally written;
when you enter input, leave a space between the amount and the currency.
ie. enter: 1.23<space>E

In general, when entering two values into a program, always put a space between the two values,


> . Any other character (from A to Z does work

The reason is that E or e can be a valid character while reading a floating point value.
For example: typing in 1.234E+2 is equivalent to typing in 123.4
Last edited on
Thanks!!!!! Understood...

danny
Hello bedmylle,

Danny, I do knot know why the "E" is a problem either. I have not encountered anything like this before.

Just saying that the "std_lib_facilities.h" header file is not available with the standard VS 2015 version that I have. It may have been made available in the book that you are using, but I do not have that book yet.

I think you misunderstood me. I did not mean for you to remove "stdafx.h" from your program just when you post it here is where it is not needed.

I understand how the input should work, but maybe someone else may have an answer why the "E" does not work.

Andy
did you see the answers from lastchance and JLBorges? I belive that is the answer...
The std_lib_facilities.h is not available in the 2017 version I use either, but "the book" explains it how to get it...
The author of "the book" is the designer and original implementer of C++, so...
Google Bjarne Stroustrup and you will see he is an authority on the matter.

danny
Hello Danny,

Yes I did. I do not use scientific notation very often, so that one slipped by me, but it makes sense now.

I did look at the "std_lib_facilities.h" and put it in with my include files so I could use it with this program, but not something I would use very often. Although some of what is in the header file could be useful.

Just playing around I did change std::cout << "Please enter an amount followed by a currency D or E ( 10.45 D): "; to show an example of how to write the input. This will work with either method of input.

I did try the "keep_window_open()" function from the header file, but would have to change it for what I like.

Andy
Registered users can post here. Sign in or register to post.