just got back into learning c++ after about 2 years, how can i improve this

well it works as i want it to, however there can be some improvements, such as when you enter the amount and press enter, it adds a value to the variable, but when you press F1/F3/F5 you usually have to enter in one more value before it takes effect, im unsure and have been thinking about a way to solve it for about 2 hours without much luck of coming up with any solution, i wrote this off of memory of what i remembered before i gave up on learning the language. i was trying to remember how to call a function when i wrote this as well(it was first a calculator and now this so things may be a little scattered)

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
  void taxCalculation() 
{
	float total, x, subtract;
	total = 0;
		cout << "Start adding your numbers, press F1 to see your total, press F3 to subtract, press F5 to start fresh" << endl;
		while (true) {
			if (GetAsyncKeyState(VK_F1)) {
				cout << "Your total is: " << total << endl;
			}
			if (GetAsyncKeyState(VK_F3)) {
				cout << "Enter amount to subtract: ";
				cin >> subtract;
				total = total - subtract;
				cout << "Your total is: " << total << endl;
			}
			if (GetAsyncKeyState(VK_F5)) {
				system("CLS");
				cout << "Start adding your numbers, press F1 to see your total, press F3 to subtract, press F5 to start fresh" << endl;
				total = 0, x = 0, subtract = 0;
			}
				cin >> x;
				total = total + x;
				cout << x << " Added, new total " << total << endl;
			}
}

int main()
{
	taxCalculation();
	

    return 0;
}
Last edited on
First, I would move line 5 to above line 7, so it is the first thing in the "while" loop.

Next, I would take out line 18, since it is now within the "while" loop.

As far as your "if" statements, you should first check to see your Async key state. If your input is numeric, just add it to the total. Use the minus sign for subtraction -- something like:

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

while( true )
    {
    cout << "Start adding ...";

    if( GetAsyncKeyState( VK_F1 ) )
        {
        // show total
        }
    else if( GetAsyncKeyState( VK_F5 ) )
        {
        total = 0.0;

        system( "cls" );

        }  // if VK_F5
else
        {
        cin >> number;

        total += number; 

        }



    }    //    while( true )

is this what you meant? its still doing the same thing, im not sure how to have the program wait at the top of the loop, and have it detect if the key F1,F3,F5 were pressed, or if a number was entered. such as detect if the user presses F1,F3,F5 first, then do whats in the if statement, but if none of those keys are pressed and the user enters in 1,2,3,4,5,6,7,8,9,10,11,12 ect. then add the numbers to the float total.

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
void taxCalculation() 
{
	float total, x, subtract;
	total = 0;
		cout << "Start adding your numbers, press F1 to see your total, press F3 to subtract, press F5 to start fresh" << endl;
		while (true) {

			if (GetAsyncKeyState(VK_F1)) {
				cout << "Your total is: " << total << endl;
			}
			else if (GetAsyncKeyState(VK_F3)) {
				cout << "Enter amount to subtract: ";
				cin >> subtract;
				total -= subtract;
				cout << "Your total is: " << total << endl;
			}
			else if (GetAsyncKeyState(VK_F5)) {
				total, x, subtract = 0;
				system("CLS");
				cout << "Start adding your numbers, press F1 to see your total, press F3 to subtract, press F5 to start fresh" << endl;
			}
			else {
				cin >> x;
				total += x;
				cout << x << " Added, new total " << total << endl;
			}
			}
}
Last edited on
Other than the message to the user, you have it right. I guess I thought if they pressed a function key your "if"s would catch it.
the if's do catch it, however if i press one of the function keys i have to do one final cycle of the loop before it activates whichever function key i pressed which means entering in one more value instead of getting a instant popup of whats in the if statements which is fustrating
Topic archived. No new replies allowed.