Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator v1
#11
Oh!,well that's ok I guess,not a big deal is it? :S
Reply
#12
Thank's Xenocide i will give a try later Smile
Reply
#13
its easy to make but for a beginner its good Smile
Reply
#14
Update:

Ok so I'm back from holidays,I was kinda bored today so I decided to add a goto statement in it to re-do the put new number thing and not exit and re-open the program to do it.
Code:
#include <iostream>
using namespace std;

int main()
{
    double a,b;
    int option;
labelgoto:
    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. Enter new numbers \n";
    cout << "6. Quit \n";
    do {
    cout << "Enter your choice: ";
    cin >> option;
    } while(option<1 || option>6);
    
    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:
        goto labelgoto;
        break;
    case 6:
        cout << "Goodbye \n";
        break;
    }
    
    } while(option!=6);
    
    
    return 0;
    }
Reply
#15
(01-09-2010, 07:23 AM)Beta™ Wrote: I decided to add a goto statement

No! Goto statements are considered bad practice in C++ because they cause spagetti-code. Use some other form of conditional statement where possible.

Quote:The primary problem with goto is that it allows a programmer to cause the point of execution to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code. Spaghetti code has a path of execution that resembles a bowl of spaghetti (all tangled and twisted), making it extremely difficult to follow the logic of such code.

As Dijkstra says somewhat humorously, "the quality of programmers is a decreasing function of the density of go to statements in the programs they produce".

To read Dijkstra's paper, click here.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#16
Yeah I kinda know it's bad thing to use in your code but I couldn't find any other way.

Thanks for the link MrD. I'll read it.
Reply
#17
What do you think about packing it into 2 function, void showMenu() and int Calculate()

Then you can use the while statement in the main function to loop whole day through your code.
Reply
#18
Is it possible to do this?

Well it won't compile cause I'm getting only 1 error.
Quote:error C2447: '{' : missing function header (old-style formal list?)

Code:
#include <iostream>
using namespace std;
void myfunc();
void myfuncc();
int main()
{
    double a,b;
    int option;

    myfuncc();
    {

    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. Enter new numbers \n";
    cout << "6. Quit \n";
    do {
    cout << "Enter your choice: ";
    cin >> option;
    } while(option<1 || option>6);
    
    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:
        myfunc();
        break;
    case 6:
        cout << "Goodbye \n";
        break;
    }
    
    } while(option!=6);
    
    
    return 0;
    }
}

    void myfunc();
{
myfuncc();
}
Reply
#19
Here I don't have the time to debug your code, but I have an modification to offer you ;)
Compiled and works as you wanted it to!

Code:
#include <iostream>
using namespace std;

int option = 0;
double a = 0;
double b = 0;

int showMenu()
{
    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";
    cout << "5. Enter new numbers \n";
    cout << "6. Quit \n";
    cout << "Enter your choice: ";
    cin >> option;  
    
    return option;
}

void Calc(int option, double num1, double num2) {
    a = num1;
    b = num2;
    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:
             break;
        case 6:
             cout << "Goodbye \n";
             break;
    }
}

int main()
{
    do {
        option = showMenu();
        do {
           Calc(option, a, b);
        } while(option<1 || option>6);
    } while(option!=6);
        return 0;
}
Reply
#20
(01-10-2010, 05:58 AM)Master of the Universe Wrote: Here I don't have the time to debug your code, but I have an modification to offer you ;)
Compiled and works as you wanted it to!

Code:
#include <iostream>
using namespace std;

int option = 0;
double a = 0;
double b = 0;

int showMenu()
{
    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";
    cout << "5. Enter new numbers \n";
    cout << "6. Quit \n";
    cout << "Enter your choice: ";
    cin >> option;  
    
    return option;
}

void Calc(int option, double num1, double num2) {
    a = num1;
    b = num2;
    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:
             break;
        case 6:
             cout << "Goodbye \n";
             break;
    }
}

int main()
{
    do {
        option = showMenu();
        do {
           Calc(option, a, b);
        } while(option<1 || option>6);
    } while(option!=6);
        return 0;
}
or that lol,very smart move Thumbsup
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi Purpose Calculator Reality 21 5,117 12-14-2010, 06:11 PM
Last Post: Buzz Lightyear
  my own calculator WHITE PANDA 8 2,193 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: 21 Guest(s)