output a _getche() value using toupper

Hello all,

I'm building a very simple calculator program for currency exchange rates where the user selects, with the press of a letter from a list of choices, the currency of an original amount, and then the currency he desires the funds to be converted into.

When using the _getche() function before the if statements, I want the letter that was input to be displayed in uppercase, hence the toupper. My problem is this: the _getche() values do not show as uppercase except on the last two lines of code where the final amount is displayed.

Is this related to my if/else structures or I'm missing something here??

Variables are in french, but I can translate if needed... I don't simply want the answer either, I'm here to learn... Thanks guys!

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
55
  void main()
{
	//Constantes
	const float DOLLAR = 1.09557f ;
	const float EURO = 1.41474f ;
	const float BAHT = 0.3395f ;
	const float ROUPIE = 0.01801f ;
	const float COURONNE = 0.18997f ;

	//Variables
	char DeviseInitiale ;
	char DeviseConversion ;
	float MontantInitial ;
	float MontantConverti ;
	float MontantCanadien ;

	int esp = 4, esp2 = 20, esp3 = 26, esp4 = 10, esp5 = 14, esp6 = 18 ;				// Espacement

	//Input
	cout << "Entrez le montant a convertir : " ;
	cin >> MontantInitial ;
	cout << endl << endl ;
	cout << "Entrez la devise de ce montant : " << endl << endl ;

	cout << setw(esp) << " " << left << setw(esp2) << "$ -> Dollar" << setw(esp3) << "<Canadien>" << endl ;
	cout << setw(esp) << " " << setw(esp2) << "D -> Dollar" << setw(esp3) << "<\144tats-unis>" << endl ;
	cout << setw(esp) << " " << setw(esp2) << "E -> Euro" << setw(esp3) << "<Union Europ\202ene>" << endl ;
	cout << setw(esp) << " " << setw(esp2) << "B -> Baht" << setw(esp3) << "<Thailande>" << endl ;
	cout << setw(esp) << " " << setw(esp2) << "R -> Roupie" << setw(esp3) << "<Inde>" << endl ;
	cout << setw(esp) << " " << setw(esp2) << "C -> Couronne" << setw(esp3) << "<Danemark>" << setw(esp4) << "Votre choix : " ;
	DeviseInitiale = _getche() ;
	DeviseInitiale = toupper(DeviseInitiale) ;
	cout << endl << endl ;

		if (DeviseInitiale == 'D')
				MontantCanadien = MontantInitial * DOLLAR ;
				else if (DeviseInitiale == 'E')
				MontantCanadien = MontantInitial * EURO ;
				else if (DeviseInitiale == 'B')
				MontantCanadien = MontantInitial * BAHT ;
				else if (DeviseInitiale == 'R')
				MontantCanadien = MontantInitial * ROUPIE ;
				else if (DeviseInitiale == 'C')
				MontantCanadien = MontantInitial * COURONNE ;
				else MontantCanadien = MontantInitial ;

	
// Second if/else goes here, same structure.

	cout << endl << endl << endl ;
	cout << fixed << setprecision(2) ;
// the char shows up properly here	
cout << setw(esp6) << "Montant initial  : " << right << setw(esp5) << MontantInitial << " " << DeviseInitiale << endl << endl ;
// Char for second if/else not shown in above code
	cout << setw(esp6) << "Montant converti : " << right << setw(esp5) << MontantConverti << " " << DeviseConversion << endl << endl ;
You could try DeviseInitiale = toupper(_getche());.
I assume that _getche() is capturing the user's input and returning whatever character the user input.
Are you using toupper so that you only need to check for capital letters?
You will only see the character as a capital letter after toupper has been called unless the user entered a capital letter in the first place.

If you want the user to see a capital letter as he/she is entering it, the user will only see a capital letter on the input line if he or she enters a capital letter.

I'm sorry if I didn't quite understand the questions.

If there is an issue with your output, can you provide some sample output?
JayZom, you understood my question perfectly and your answer makes all the sense in the world, that's also how I undestood the attribute would work... But when you say

You will only see the character as a capital letter after toupper has been called unless the user entered a capital letter in the first place.

If you want the user to see a capital letter as he/she is entering it, the user will only see a capital letter on the input line if he or she enters a capital letter.


Is there a way to 'transform' the letter inputed by the user to a capital even before it gets displayed on the screen??
My mistake. I am not familiar with conio.h.
Follow MiiniPaa's links for more info.
Unfortunately, we can't do that. Programs that aren't console-based can do that, but that is because other programs use different ways of displaying input. The console can't do that. The user will see whatever they typed into the console, but there isn't any way to transform that.
After the user enters input, you can do whatever you want with that input, though.
It would be cool if we could do that, though! But that could introduce security issues, since we would be able to force the console to think the user entered something else.
Last edited on
Yes, you can. I see you already using conio.h. In future: do not do that. Use something which did not die in pevious century.
For your current project you can use a number of ways: http://www.cplusplus.com/forum/general/3570/ this is the first which came when I searched for it.
Two other links:
http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed
http://stackoverflow.com/questions/6856635/hide-password-input-on-terminal

P.S. Please, write standard C++. For now, at least change void main() to int main()
Last edited on
Topic archived. No new replies allowed.