Support Forums

Full Version: Calculator v1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Code:
#include <iostream>
using namespace std;

int main()
{
    int a,b;
    int option;

    cout << ":::Calculator v1::: \n";

    cout << "Enter a Number: \n";
    cin >> a;
    cout << "Enter a Number: \n";
    cin >> b;
    
    cout << "1. Multiplication \n";
    cout << "2. Division \n";
    cout << "3. Addition \n";
    cout << "4. Subtraction \n";
    do {
    cout << "Enter your choice: ";
    cin >> option;
    } while(option<1 || option>4);
    
    switch (option) {
    case 1:
        cout << "The Result is: " << a*b;
        break;
    case 2:
    cout << "The Result is: " << a/b;
    break;
    case 3:
        cout << "The Result is: " << a+b;
        break;
    case 4:
        cout << "The Result is: " << a-b;
        break;
    }
    while(option!=4)
    
    return 0;
    }
Ok so this is my second C++ project so go easy on me lol.
I just don't get why you use while loop 2 times.
But for a second project nicely done.

I still would write a function to output the menu selection and ask for users selection and run it with the while loop until "option != 4"
then another function for the switch, but I guess I'm already getting too far for you....

So nice work!

EDIT:
Code:
cout << ":::Calculator v1::: \n";

is same as

Code:
cout << ":::Calculator v1::: " << endl;

I prefer the second Big Grin
(12-03-2009, 12:57 PM)Master of The Universe Wrote: [ -> ]I just don't get why you use while loop 2 times.
But for a second project nicely done.

I still would write a function to output the menu selection and ask for users selection and run it with the while loop until "option != 4"
then another function for the switch, but I guess I'm already getting too far for you....

So nice work!

EDIT:
Code:
cout << ":::Calculator v1::: \n";

is same as

Code:
cout << ":::Calculator v1::: " << endl;

I prefer the second Big Grin
You're not going too far for me Oui,like you mean loop the menu everytime user clicks an option? instead of exiting?
I prefer
Code:
cout << ":::Calculator v1::: \n";
Tongue
The difference between using std::endl and "\n" to end a line is that std::endl will also flush the buffer to the screen (ie, is the same as doing << "\n" << std::flush();). Useful if you want to make sure the user will always see what you are pushing into the buffer when you end the line.
(12-03-2009, 02:28 PM)MrD. Wrote: [ -> ]The difference between using std::endl and "\n" to end a line is that std::endl will also flush the buffer to the screen (ie, is the same as doing << "\n" << std::flush();). Useful if you want to make sure the user will always see what you are pushing into the buffer when you end the line.

+



(12-03-2009, 11:01 AM)Xenocide Wrote: [ -> ]
Code:
while(option<1 || option>4);
while(option!=4)

You see that, those two loops are almost the same.
You could easy put that all in one loop

Code:
while(option > 4) {
    switch(option) {
        // cases 1 to 4
        // case 1:
        // do();
        // break;
        default:
            return 1;
    }
}

The default part would execute on every number that's not in the range from 1-4, so no need for option < 1 and the second while loop.
So you can use the default to call the selection menu again.
(12-03-2009, 04:18 PM)Master of The Universe Wrote: [ -> ]+



(12-03-2009, 11:01 AM)Xenocide Wrote: [ -> ]
Code:
while(option<1 || option>4);
while(option!=4)
You see that, those two loops are almost the same.
You could easy put that all in one loop

Code:
while(option > 4) {
    switch(option) {
        // cases 1 to 4
        // case 1:
        // do();
        // break;
        default:
            return 1;
    }
}

The default part would execute on every number that's not in the range from 1-4, so no need for option < 1 and the second while loop.
So you can use the default to call the selection menu again.
Alright thanks man Thumbsup
Ok so i updated it a little bit
Code:
#include <iostream>
using namespace std;

int main()
{
    double a,b;
    int option;

    cout << ":::Calculator v1::: \n";
    
    cout << "Enter a Number: \n";
    cin >> a;
    cout << "Enter a Number: \n";
    cin >> b;
    do {
    cout << "1. Multiplication \n";
    cout << "2. Division \n";
    cout << "3. Addition \n";
    cout << "4. Subtraction \n";
    cout << "5. Quit \n";
    do {
    cout << "Enter your choice: ";
    cin >> option;
    } while(option<1 || option>5);
    
    switch (option) {
    case 1:
        cout << "The Result is: " << a*b << '\n';  
        break;
    case 2:
    cout << "The Result is: " << a/b << '\n';
    break;
    case 3:
        cout << "The Result is: " << a+b << '\n';
        break;
    case 4:
        cout << "The Result is: " << a-b << '\n';  
        break;
    case 5:
        cout << "Goodbye \n";
        break;
    }
    
    } while(option!=5);
    
    
    return 0;
    }
I added the double variable for more accurate results and the while(option!=5) for it to loop the menu after every result,before it didn't work as I forgot to put why the loop should stop at option 3 example so I added the Quit option,hope it's better.
You've still used two loops when you could have just used one.

Code:
#include <iostream>
using namespace std;

int main()
{
    double a,b;
    int option;

    cout << ":::Calculator v1::: \n";
    
    cout << "Enter a Number: \n";
    cin >> a;
    cout << "Enter a Number: \n";
    cin >> b;
    do {
    cout << "1. Multiplication \n";
    cout << "2. Division \n";
    cout << "3. Addition \n";
    cout << "4. Subtraction \n";
    cout << "5. Quit \n";
    cout << "Enter your choice: ";
    cin >> option;
    
    switch (option) {
    case 1:
        cout << "The Result is: " << a*b << '\n';  
        break;
    case 2:
        cout << "The Result is: " << a/b << '\n';
        break;
    case 3:
        cout << "The Result is: " << a+b << '\n';
        break;
    case 4:
        cout << "The Result is: " << a-b << '\n';  
        break;
    case 5:
        cout << "Goodbye \n";
        break;
    default:
        cout << "Invalid choice \n";
        break;
    }
    
    } while(option!=5);
    
    
    return 0;
}

Also using a double is pointless in this case since std::cout only shows 6 points of precision.
(12-05-2009, 07:48 AM)MrD. Wrote: [ -> ]You've still used two loops when you could have just used one.

Code:
#include <iostream>
using namespace std;

int main()
{
    double a,b;
    int option;

    cout << ":::Calculator v1::: \n";
    
    cout << "Enter a Number: \n";
    cin >> a;
    cout << "Enter a Number: \n";
    cin >> b;
    do {
    cout << "1. Multiplication \n";
    cout << "2. Division \n";
    cout << "3. Addition \n";
    cout << "4. Subtraction \n";
    cout << "5. Quit \n";
    cout << "Enter your choice: ";
    cin >> option;
    
    switch (option) {
    case 1:
        cout << "The Result is: " << a*b << '\n';  
        break;
    case 2:
        cout << "The Result is: " << a/b << '\n';
        break;
    case 3:
        cout << "The Result is: " << a+b << '\n';
        break;
    case 4:
        cout << "The Result is: " << a-b << '\n';  
        break;
    case 5:
        cout << "Goodbye \n";
        break;
    default:
        cout << "Invalid choice \n";
        break;
    }
    
    } while(option!=5);
    
    
    return 0;
}

Also using a double is pointless in this case since std::cout only shows 6 points of precision.
2 loops where needed for "Enter your choice" and the menu :\
Double is useful in this case for accurate results instead of rounded,I don't get what you mean.
Enter a number with more than six points of precision (1.1234567 would be an example), then enter the second number as 0, then do an add. You will only get 1.123456 displayed on screen because std::cout defaults to six points of precision for real numbers.
Pages: 1 2 3