Beginner Code help for calculator!

Hi all,
Just a day since I started to learn C++ myself. I have no previous coding experience and thus, and struggling alot. I have tried to create a simple calculator and expand on it.

At the moment, I would love to create another function to allow users to decide should they wish to do another operation or quit. I have succeeded in having lengthy code with the decision made at the end of the operation. But would wish (as mentioned above) want to have new function to tidy up slightly.

Seems that I can't compile the following code. Any help would be lovely :)

thank you!

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
128
129
130
131
132
133
134
135
136
137
138
  #include <iostream>

using namespace std;

float addition(float number1, float number2)
{
return number1 + number2;
}
float subtraction(float number1, float number2)
{
    return number1 - number2;
}
float multiplication(float number1, float number2)
{
    return number1 * number2;
}
float division(float number1, float number2)
{
    return number1 / number2;
}

int decision()
{
    char y;
 char n;
 char selection;
cout <<"\nDo you want to do another operation? Y or N?" <<endl;
cout <<"Your selection: ";
cin >> selection;
if(selection=="y")
{
    return main();
}
else
{if(selection=="n")
 {
     return 0;
 } else
 {
     cout <<"Invalid Selection"<<endl;
     return decision();
 }
}
}






int main()
{
 float num1;
 float num2;
 int choice;


 for(;;)
 {
    //program commencement
        cout << "\nCalculator function enabled. \n1: addition 2: subtraction 3: multiplication 4: division" << endl;
        cout << "Your selection: ";
        cin >> choice;
        cout << ""<<endl;
    if(choice==1)
    {
        cout << "You are doing an Addition" << endl;
        cout << "First Number: ";
        cin >> num1;
        cout << "Second Number: ";
        cin >> num2;
        cout << "Your answer is: " << addition(num1, num2) << endl;
        return decision();
    }
    else
    {if(choice==2)
    {
        cout << "You are doing a Subtraction" << endl;
        cout << "First Number: ";
        cin >> num1;
        cout << "Second Number: ";
        cin >> num2;
        cout << "Your answer is: " << subtraction(num1, num2) << endl;
        return decision();
    }
      else
      {if(choice==3)
          {
             cout << "You are doing a multiplication" << endl;
             cout << "First Number: ";
             cin >> num1;
             cout << "Second Number: ";
             cin >> num2;
             cout << "Your answer is: " << multiplication(num1, num2) << endl;
             return decision();
          }
          else
          {if(choice==4)
          {
              cout << "You are doing a division" << endl;
               cout << "First Number: ";
               cin >> num1;
               cout << "Second Number: ";
               cin >> num2;

               if(num2==0)
               {
                   cout <<"\nInappropriate Operation\n";
                   return main();
               }
               else
               {
                   cout << "Your answer is: " << division(num1, num2) << endl;
                   return decision();
               }


          }
               else
               {
                   cout<< "Please select the appropriate operation\n"<<endl;
                   return main();
               }
          }
      }
    }

 }



}





1) Your code is technically invalid, as standard prohibits you wrom calling main()
2) Move your code relating to the selection of operations in some function, say, menu().
Add function asking a question and returning a boolean value, call it ask:
1
2
3
4
5
6
7
8
9
10
11
bool ask(const std::string& question) 
{
    std::cout << question;
    char answer;
    do {
        std::cin >> answer;
        answer = tolower(answer);
        if(answer != 'y' && answer != 'n') std::cout << "invalid input\n";
    } while(answer != 'y' && answer != 'n');
    return answer == 'y';
}

Add a loop in main calling your menu() function and using value returned by ask() as loop condition:
1
2
3
do {
    menu();
while(ask("\nDo you want to do another operation? Y or N?"));
Hi Thank you MiiNiPaa,

I am truly sorry but I don't really get what you mean by prohibition from calling main().

If it is not troublesome, I would appreciate if you could show me the lump of code that you would use to fix my error.

Sorry! >,<

JungMin
what you mean by prohibition from calling main().
It is just what it says. Standard prohibits calling function named main() anywhere in program.
Standard wrote:
3.6.1.3
The function main shall not be used within a program.

Lines 32, 109 and 122 will lead to error on standard compliant compiler.

Here how I would do it (code shortened slightly) http://ideone.com/ZLODpY
Thank you so much MiiNiPaa!
Topic archived. No new replies allowed.