Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator v1
#1
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.
Reply
#2
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
Reply
#3
(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
Reply
#4
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.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#5
(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.
Reply
#6
(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
Reply
#7
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.
Reply
#8
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.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#9
(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.
Reply
#10
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.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi Purpose Calculator Reality 21 5,134 12-14-2010, 06:11 PM
Last Post: Buzz Lightyear
  my own calculator WHITE PANDA 8 2,199 07-14-2010, 03:19 PM
Last Post: Kondry
  Calculator in C Liberty 4 1,322 12-10-2009, 11:45 PM
Last Post: Sagittarius

Forum Jump:


Users browsing this thread: 6 Guest(s)