Code::blocks calculator, help me fix this.

Full code here,
#include <iostream>
#include <windows.h>
using namespace std;

int choice;
double value1, value2, result;

int main()
{

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 3); cout <<"CALCULATOR\n";
SetConsoleTextAttribute(h, 3); cout <<"Made by Kristen\n\n";
SetConsoleTextAttribute(h, 7);


while(true) {

cout << "1. Add\n";
cout << "2. Subtraction\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout << "5. Exit\n\n";
cout << "What would you like to do? ";

cin >> choice;

if(choice == 5)
break;

cout << "\nPlease enter the first value: ";
cin >> value1;
cout << "Please enter the second value: ";
cin >> value2;

if(choice == 1)
result = value1 + value2;

else if(choice == 2)
result = value1 - value2;
else if(choice == 3)
result = value1 * value2;
else if(choice == 4)
result = value1 / value2;


cout << "\nThe result is: " << result << "\n\n";



}
cout << "\n\nThank you for using the Calculator!\n\n";
}


I don't mind if you guys copy paste it but, if I use a WORD, not a number, it continously loops and wont stop. How can I do that if you put a wrong number/word, it will return back where you have to choose a number?
Last edited on
What you could do is write a small function to get an integer (or a double) with suitable error handling.


A simple example

1
2
3
4
5
6
7
8
9
10
int get_integer()
{
    int num = 0;
    while (!(cin >> num))        // get the input and test the outcome
    {
        cin.clear();             // reset error flags
        cin.ignore(1000, '\n');  // remove invalid characters from buffer
    }
    return num;                  // we get here, everything was ok.
}


you would insert that into your code somewhere just before the start of main().

Then where you have
1
2
3
    cout << "What would you like to do? ";

    cin >> choice;

it now becomes:
1
2
3
    cout << "What would you like to do? ";

    choice = get_integer();


Explanation
If the user enters something which is not an integer, the cin stream enters a fail condition, which means no further input is possible. You might write that as
1
2
3
4
5
    cin >> choice;
    if (!cin)
    {
        cout << "an input error occurred";
    }

In the function get_integer(), the input is done and the result tested inside the condition of the while loop.

Next, there are two steps which need to be done before the stream can be used again.
1. reset the error flags.
2. remove the unwanted characters typed by the user.

I used a function (a) to avoid cluttering the main code, (b) because there may be several places where user input is done, the function allows code to be re-used.

For the values of type double, you could replicate the function, give it a different name and change int to double. (You might also use a function template, but I didn't want to go into that as well here).
Wow, thanks a lot man. Explanation is well clear, hope we find more people like you :D.
Can you do a switch function example too? I also like to see that function template you mentioned at the last sentence. Please? Also, how can I now make like this, whenever a person writes a higher number than 1-5, then he will be back to select the number from 1-5.
Last edited on
Here's the function template which uses the same code for both int and double. The compiler will generate separate functions based on the type of the parameter num.

I think you should have a go at writing the switch-case example, see how you get on. see tutorial:
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
8
9
template <class T>
void get_number(T& num)
{
    while (!(cin >> num))        // get the input and test the outcome
    {
        cin.clear();             // reset error flags
        cin.ignore(1000, '\n');  // remove invalid characters from buffer
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
        get_number(choice);
        
        if (choice == 5)
            break;
        
        if (choice < 1 || choice > 4)
            continue;     
        
        cout << "\nPlease enter the first value: ";
        get_number(value1);

        cout << "Please enter the second value: ";
        get_number(value2);
thanks a lot again, dude. Appreciate it! I'm learning a lot :3
But now, can how can I make it like that? If user calculates the number for example: 1 + 1 = 2, if user presses the button again like enter, then that calculate will clear it and start over fresh.
Topic archived. No new replies allowed.