my calculator

Pages: 123
Can you also please post a snippet of code with more functions in this code. I get how to make functions and all but I don't get where I would use it and where I would like put it in this program
Googling something takes about 5 seconds, and this website has a surprisingly well written tutorial as well. A crucial part of being a good programmer is being able to do research, but I'll help you out anyways: http://lmgtfy.com/?q=c%2B%2B+functions+tutorial

Since you say you know how to write functions, think about why they're useful. In your program you're executing roughly the same pieces of code over and over with minor changes. Instead of typing them out each time, why not write yourself one or two neat little functions and call them every time you need to get input?

As for your problems with Code::Blocks, you most likely didn't have g++ installed on your computer. I'm assuming you've fixed it now anyways :/
Last edited on
i tryed a function on case two but it didnt work.it says the variables are undeclared
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>

using namespace std;

    void title(){
        system("TITLE calculator");
        system("color 2");
        cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
    }
    int processtwo(){
    cout << "Please enter the first number you want to use: ";
            cin >> dnum1;
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "Please enter the second number you would like to use: ";
            cin >> dnum2;

            switch (cchar){
            
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " = "
                << (dnum1 + dnum2) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " = "
                << (dnum1 - dnum2) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " = "
                << (dnum1 * dnum2) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " = "
                << (dnum1 / dnum2) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            }
            cout << "Would you like to start again (y or n): ";
            cin >> cagain;
                break;
                }
         }
    
    
    int main()
{
    int howmany;
    int choice;
    double dnum1;
    double dnum2;
    double dnum3;
    double dnum4;
    double dnum5;
    double average;
    char cchar;
    char cagain;
    
    do{
    cout << "type 1 to use a regular calculator." << endl;
    cout << "type 2 to use a average calculator" << endl;
    cout << endl;
    cin >> choice;
    
    switch (choice){
           case 1:
    
    cout << "please enter the amount of numbers you want to calculate: ";
    cin >> howmany;
    
    switch (howmany){
    case 1:
        cout << "invalid number" << endl;
        break;
    case 2:
         processtwo();
    case 3:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter three numbers you want to use: ";
            cin >> dnum1 >> dnum2 >> dnum3;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << "+" << dnum3 << " = "
                << (dnum1 + dnum2 + dnum3) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " = "
                << (dnum1 - dnum2 - dnum3) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " = "
                << (dnum1 * dnum2 * dnum3) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " = "
                << (dnum1 / dnum2 / dnum3) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            }
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
            break;
    case 4:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter four numbers: ";
            cin >> dnum1 >> dnum2 >> dnum3 >> dnum4;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " + " << dnum3 << " + " << dnum4 << " = " 
                << (dnum1 + dnum2 + dnum3 + dnum4) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " - " << dnum4 << " = "
                << (dnum1 - dnum2 - dnum3 - dnum4) << endl;
                break;
     
                     
                     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
       case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " * " << dnum4 << " = "
                << (dnum1 * dnum2 * dnum3 * dnum4) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " / " << dnum4 << " = "
                << (dnum1 / dnum2 / dnum3 / dnum4) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            }
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
    case 5:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter five numbers: ";
            cin >> dnum1 >> dnum2 >> dnum3 >> dnum4 >> dnum5;
            switch (cchar){
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " + " << dnum3 << " + " << dnum4 << " + " << dnum5 
                << " = " << (dnum1 + dnum2 + dnum3 + dnum4 + dnum5) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " - " << dnum4 << " - " << dnum5 
                << " = " << (dnum1 - dnum2 - dnum3 - dnum4 - dnum5) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " * " << dnum4 << " * " << dnum5 
                << " = " << (dnum1 * dnum2 * dnum3 * dnum4 * dnum5) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " / " << dnum4 << " / " << dnum5  
                << " = " << (dnum1 / dnum2 / dnum2 / dnum3 / dnum4) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            default:
            cout << "Invlaid number.you can only use up to 5" << endl;
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
            break;
            }
    break;
    case 2:
         do{
         cout << "Enter how many numbers you want to find the average of (up to five)" << endl;
         cin >> howmany;
         
         switch (howmany){
                case 1:
                     cout << "You cant use that number it is too low" << endl;
                     break;
                case 2:
                     cout << "Please enter the two numbers you want to use number you want to use: ";
                     cin >> dnum1 >> dnum2;                   
                     
                     average = (dnum1 + dnum2) /2;
                     cout << "the average of those numbers is " << average << endl;
                     break;
                case 3:
                     cout << "Please enter three numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3;
                     average = (dnum1 + dnum2 + dnum3) /3;
                     cout << "the average of those numbers is " << average << endl;
                     break;
                case 4:
                     cout << "Please enter the four numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3 >> dnum4;
                     average = (dnum1 + dnum2 + dnum3 + dnum4) /4;
                     cout << "the average of those four numbers is " << average << endl;
                     break;
                case 5:
                     cout << "Please enter the five numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3 >> dnum4 >> dnum5;
                     average = (dnum1 + dnum2 + dnum3 + dnum4 + dnum5) /5;
                     cout << "the average of those five numbers is " << average << endl;
                     break;
                     }
                     break;
    cout << "would you like to start again (y or n)";
    cin >> cagain;
    
    }while (cagain == 'y' || cagain == 'Y');
                     }
    system("pause");
    return 0;
}
?
You've clearly ignored both my own posts, as well as everyone else who have tried to help you, which I find insulting.

Now let's use our "expert programmer" analysis skills to fix your compiler errors. Because your compiler says you are using variables that aren't declared in your function, it's safe to assume that your problems are only in this function. Let's take a look at it from the computers perspective:

Line 1: Output a string to a stream using the cout object.
Line 2: Read from the input stream using the cin object, and store the value in dnum1. WAIT!!??!!!!!111one, I have no idea what dnum1 is, is it a class, a function, a variable, a bird, a plane, superman? Compiler error.
Line 3: Output a string to a stream using the cout object.
Line 4: Read from the input stream using the cin object, and store the value in cchar. Again, what is cchar? I haven't head of it.

See where I'm going here? Now for the third time, I recommend you use pastebin so you don't have to spread your code across multiple posts which is an inconvenience for both you and the people trying to help you. And if you need me to hold your hand through a problem again because you're scared of your compiler please just ask.

While I'm at it please stop bumping your posts with thought-provoking messages such as "help plz" or "?."

Lastly, it's safe to assume that the only errors in your program at the moment are in your new function, so there isn't a need to post everything else and make it harder for the people trying to help you to do so?
Last edited on
sorry i actually wasnt quite fond of that website. i didnt really get how to use it. and as for the functions im still trying to implement them in my code. 2morrow ill work on what you said in the last post and im sorry if im being in polite ill be more polite...ill start using pastebin
Last edited on
ok i used pastebin to post my code.press the link.i fixes the function but doesnt that function just make that program longer??? http://pastebin.com/m2hGfcCM
If you are going to use a pastebin I recommend ideone.com or codepad.org that has syntax highlighting and can run your code.
OMG... You miss the entire point of using a function! You're supposed to make a function to carry out repetitive lines of code! But all you've done is cut-paste a section of code, put it in a function and then called that function... Goodness... Ohk, here's what you do, write four functions, each for the four operations, viz (+), (-), (*), (/) which takes two arguments... Then wherever you're using addition, instead of calculating the expression again and again, just call the function. First just write the four functions, and show them, via Pastebin only...
If you use proper indentation you should see that things doesn't line up. The do loop has no ending } while (...);. You are missing break; on a number of places and some code is unreachable because it is placed after a break;
Last edited on
I think the problem here is, that you code as you go... Don't do that.. Plan out what you want.. Try and think out the repetitive lines of code, and where your code becomes unreadable or unmanageable.. Write out a format of a sample output.. Imagine what you'd want in a good proper calculator. What good is a calculator, that can do ONLY one type of operation on several numbers! Think out an algorithm for your basic program, and then start... This way, you won't hit roadblocks as far as logic is concerned, your code will be much much cleaner, and the program will be readable and user-friendly... If you're doing this calculator thing for just some practice, I'd suggest starting over.
ok and how would i do the function for lets say addithion.ok i call the function at all of the case'+'s but on each one you add a different amount of numbers.so how will that work because i add a diferent amount of numbers on each case'+'?
I quit.
Oh you gotta be kidding me... here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float add(float a, float b)
{
   return (a+b);
}

void main()
{
   //blah blah blah

    float a, b, c, d, e;     

    cout<<"Enter 5 numbers to add: ";
    cin>>a>>b>>c>>d>>e;
    cout<<"The result is: "<<(add(add(add((add(a, b)), c), d), e)); //First adds a and b, then their result to c, etc etc...
}


How hard was this????



¿How is that better than a+b+c+d+e ?
Or than std::accumulate(array, array+size(array), 0);
Ohk, here's what I'm gonna do... I'm gonna write a calculator program in my free time in my way, post it as "My Calculator" and then you can see the difference.. (I'm not trying to be condescending, just trying to show how much simpler and efficient a program can be...)
ok are you going to private message me it?? and thanks for all the help..im sorry for being annoying which i realized am being.
and this program says main must return int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

float add(float a, float b)
{
   return (a+b);
}

void main()
{
   

    float a, b, c, d, e;

    cout<<"Enter 5 numbers to add: ";
    cin>>a>>b>>c>>d>>e;
    cout<<"The result is: " <<(add(add(add((add(a, b)), c), d), e));
    
    system("pause");
    
}
When running a C++ program, the operating system looks for the main function to start running the program (the only function the operating system explicitly calls). This means that the name main is reserved for the main function, and in the above program you've declared your own version of main that returns a void value. You need to declare main to return an int.
Pages: 123