Very basic calculator

So I've been learning a bit of C++, i've only been messing with it for 2 days i believe and with small ambitious goal to make a simple calculator with a simple text based gui for the user to navigate. Jumping around and learning new ways to program in C++ i try to fix the issues i keep running into.

Current issue that i can't figure out for the life of me is while the code has no errors and all seems to be perfectly in place is that the RESTART function won't work when i place it at the end of my addition or subtraction functions to direct it back to the menu function. Originally i made it so after the math was done it would go straight back to the menu. Then trying to make my code a bit more complex i decided to add the RESTART function so the function would prompt the user if they wanted to do more or end the program. I also took the opportunity to try and figure out so the user could enter "y" or "n" instead of like "1" or "2" but i have failed to make any significant progress past this stage.

I have considered possible ways to rewrite the code to maybe fix this issue but inexperience doesn't permit me to know whether or not my ideas would work and i figure since this is the best out of 15 progressive tries i best try and get this one to work.

What i'm trying to accomplish:

-Being prompted to restart or quit after receiving your total
-Fix errors where i don't get "PLEASE PICK ONE!" infinitely by entering a letter or double symbol such as "--" or "==" as just an example. Or simply allowing the user to only enter 1 to 3 in the menu.
-eventually make it so the user can decide on how many numbers they wish to enter rather than only being able to add or subtract just 2 numbers.
-finish my first program

My knowledge of the program is very limited by i keep experimenting, searching for different approaches to try and get certain results, so far my knowledge is loops, very basic variables, functions and a little bit about classes.

I added a bunch of comments, sorry if it makes it hard to read but i put it in place so people can have an easier time understanding it and also as a learning aid to myself so i can look back and understand what i did.

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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cctype>

using namespace std;

void MENU(); // Function prototype for MENU function
void Addition(); // Function prototype for Addition function
void Subtraction(); // Function prototype for Addition function
void RESTART(); // Function prototype for RESTART function

//Function prototypes used to keep the code clean and readable.

int main() //Main function
{

MENU(); // MENU first to run in the main

    return 0;
}

void MENU() //When called upon the basic main menu should show up.
{      
    int answer; //answer variable used to navigate the menu
    int Addition(); //Function variable (i think) for Addition
    int Subtraction(); //Function variable possible for Subtraction

    cout << "     MENU\n"; //Text for the menu reading off the options the user has from 1 to 3
    cout << " 1.ADDITION\n";
    cout << "2.SUBTRACTION\n";
    cout << "   3.EXIT\n";
    cin >> answer; //User enters his answer

    switch(answer){ //Switch runs through its options based on that answer variable
case 1:// if case 1 is picked then it goes to the addition function
  Addition();
break;

case 2:// if 2 is picked then it goes to the subtraction function
    Subtraction();
    break;
case 3: // If the user enters 3 then it clears screen and ends the program.
    system("cls"); // a poor insecure way to clear the screen before exiting the program, 
                  //but currently the only way i know how to get it to work the way i desire it to.
    exit(0);
case 4:
    if(answer > 3) // if the answer is greater than 3
    {
    cout << "PLEASE PICK ONE!\n"; //Prompting user to pick a valid answer
    MENU(); //Redirecting user back the MENU function
    }
    break;

default :

    if(answer <= 0){ // If the answer is given is less than or equal to 0
    cout << "PLEASE PICK ONE!\n";//Prompting user to pick a valid answer
    MENU();//Redirecting user back the MENU function
    }
  }
}

void Addition() //When called upon the program should do a basic 2 number addition.
{
    int RESTART();
    int fn;
    int sn;
    int sum;

    cout << "       ADDITON\n"; //Very simplistic almost DOS like menu for the Addition user interface
    cout << "ENTER YOUR FIRST NUMBER:\n"; //Prompting user for first number
    cin >> fn; // User enters first number
    cout << "\nENTER YOUR SECOND NUMBER:\n"; //Prompting user for first number
    cin >> sn; // User enters second
    sum = fn + sn; //Program adds the two together
    cout << "\nYOUR TOTAL IS: " << sum << endl; //Program prints out sum
    cout << endl;
    RESTART; // Supposed to ask if the user wants to restart, instead goes straight to menu
}

void Subtraction() //Everything here is supposed to act the same as addition 
//except its main purpose is to subtract the second number from the first number 
//but it fails the same way as the Addition function
{
    int MENU(); 
    int RESTART();
    int fn;
    int sn;
    int sum;

    cout << "       SUBTRACTION\n";
    cout << "ENTER YOUR FIRST NUMBER:\n";
    cin >> fn; //All functions here are the same as addition with the exception it subtracts values rather than adds them
    cout << "\nENTER YOUR SECOND NUMBER:\n";
    cin >> sn;
    sum = fn - sn;
    cout << "\nYOUR TOTAL IS: " << sum << endl;
    cout << endl;
    RESTART; // same issue happens as soon as it's supposed to hit this bit of code
}

void RESTART() //When called upon the program will restart or end the program, or so it should.
{

    void MENU(); //Menu function
    string input;

    cout << "DO YOU WISH TO START OVER? Y/N"; //None of this makes an appearance in the code
    cin >> input;
if (input == 'y')
{
    MENU();
}else if(input == 'n')
{
    system("cls");// a poor insecure way to clear the screen before exiting the program 
    //but currently the only way i know how to get it to work the way i desire it to.
    exit(0);
}


}


I know it has some issues, probably the most notable from a security stand point far as i know is system("cls") but i'm still working on it and when i find a better way to do that particular function i will.

I've been only programming since friday and i've only been programming with c++ since monday probably.

If anyone decides to help me, i apologize if i ask a lot of follow up questions because i have some difficulty learning through text and might need it explained multiple times before it sinks in.

Thank you in advance

-Skwareblox

*Edit: I noticed my previous post was giving some weird kinda bug thing because the comments were to long so i fixed and simplified that
Last edited on
I have no idea y u keep calling the restart function in each function ?? U ultimately start your program and than start it again and again and so on...
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. Let's take it step by step:

skwareblox wrote:
-Fix errors where i don't get "PLEASE PICK ONE!" infinitely by entering a letter or double symbol such as "--" or "==" as just an example. Or simply allowing the user to only enter 1 to 3 in the menu.


You have the right idea about using a switch statement, but it seems you don't fully understand default. This is a section that will be called when none of the case sections conforms to your variable (assuming you call break; at the end of every case, as you do).
So we could rewrite your switch statement as such:

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
switch(answer){ //Switch runs through its options based on that answer variable
case 1:// if case 1 is picked then it goes to the addition function
  Addition();
break;

case 2:// if 2 is picked then it goes to the subtraction function
    Subtraction();
    break;
case 3: // If the user enters 3 then it clears screen and ends the program.
    system("cls"); // a poor insecure way to clear the screen before exiting the program, 
                  //but currently the only way i know how to get it to work the way i desire it to.
    exit(0);
    //This is not needed, if "answer" is anything other than 3, it can be handled by "default"
case 4:
    if(answer > 3) // if the answer is greater than 3
    {
    cout << "PLEASE PICK ONE!\n"; //Prompting user to pick a valid answer
    MENU(); //Redirecting user back the MENU function
    }
    break;

default :
    //You don't really need this "if", if "answer" is anything but 1, 2 or 3 this default block will handle it.
    if(answer <= 0){ // If the answer is given is less than or equal to 0
    cout << "Please insert 1, 2 or 3.\n";//Prompting user to pick a valid answer
    MENU();//Redirecting user back the MENU function
    }
}





skwareblox wrote:
-Being prompted to restart or quit after receiving your total


Let's look at your Addition() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Addition() //When called upon the program should do a basic 2 number addition.
{
    int RESTART(); //You already declared this at the top of the file, no need to do it here.
    int fn;
    int sn;
    int sum;

    cout << "       ADDITON\n"; //Very simplistic almost DOS like menu for the Addition user interface
    cout << "ENTER YOUR FIRST NUMBER:\n"; //Prompting user for first number
    cin >> fn; // User enters first number
    cout << "\nENTER YOUR SECOND NUMBER:\n"; //Prompting user for first number
    cin >> sn; // User enters second
    sum = fn + sn; //Program adds the two together
    cout << "\nYOUR TOTAL IS: " << sum << endl; //Program prints out sum
    cout << endl;
    RESTART; //This isn't calling a function, you need to add parenthesis: RESTART();
}


Now let's look at your RESTART() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void RESTART() //When called upon the program will restart or end the program, or so it should.
{
    //You start by calling the MENU() function - so the first thing it will do is show the menu again
    void MENU(); //Menu function
    string input;

    cout << "DO YOU WISH TO START OVER? Y/N"; //None of this makes an appearance in the code
    cin >> input;
if (input == 'y')
{
    MENU();
}else if(input == 'n')
{
    system("cls");// a poor insecure way to clear the screen before exiting the program 
    //but currently the only way i know how to get it to work the way i desire it to.
    exit(0);
}



That should help you get on your way.
Let us know if you have any further questions.

All the best,
NwN
Last edited on
Alright so i edited the code in the way you suggested, for ease of access i'll just give a link to paste bin.

I see where i made the mistakes but the RESTART function still isn't working properly and as soon as i'm done with the addition or subtraction part of the program it goes straight to menu. I have tried this with if statements and switch's and nothing seems to function properly and i don't really know why.

Also i hadn't noticed that when i was calling on the RESTART function i forgot the parenthesis, that was a mistake but i fixed it and nothing really changed after i did fix it the issues of the code you had pointed out

well, i guess you'll see for yourself:

http://pastebin.com/8sWV5VRx

Also aside from these immediate issues with trying to add the RESTART function it's still incredibly easy to break and cause an infinite stream line spam of "Please insert 1, 2 or 3!"

I actually worked on this code all day yesterday pretty much (Pretty sad for such a simple program, right?) and i guess it shows i got sloppy towards the end but this has been the best result so far.

I originally designed it to go straight to the menu as part of baby steps towards building the program but it continues to do it after i've added the user prompt.
Topic archived. No new replies allowed.