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