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
Oh!,well that's ok I guess,not a big deal is it? :S
Thank's Xenocide i will give a try later Smile
its easy to make but for a beginner its good Smile
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;
    }
(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.
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.
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.
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();
}
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;
}
(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
Pages: 1 2 3