How to make this simple?

How to make my code simple? i wrote it for little practice but end up being a long enough code.
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
///Bot Program :3
#include <iostream>
#include <exception>
#include <fstream>

using namespace std;

int Calculator(float,float);
int CalcExtra1OP(float,float,float);
int CalcExtra2OP(float,float,float);
void BDataEngine();

float Num1,Num2,Num3;

int main()
{
bool extcho;
extcho = true;
while(extcho){
    cout << "-----------------------------WELCOME TO BOT PROGRAM-----------------------------" << endl;
    cout << "\n Here I will give you some choices to choose,\nplease choose one :) \n";
    cout << "1.Calculator Program.\n2.Biodata Engine\n0.Exit BOT Program\n\n";
    int MChoice;
    int CalcNUMs;
    int CalcOPRs;
    cin >> MChoice;
    cout << "The program runs:\n";
    switch (MChoice){
    case 1:
    cout << "-----------------------------------CALCULATOR-----------------------------------\n";

    cout << "Now How many numbers you want to calculate?\nchoose one\n2\n3\nYour Answer: ";
    cin >> CalcNUMs;
     if(CalcNUMs == 2)
    {
        Calculator(Num1,Num2);
        break;
    }
    if(CalcNUMs == 3){
    cout << "Now How many Operator you want to insert?\nChoose one\n1\n2\nYour Answer: ";
    cin >> CalcOPRs;
    if(CalcOPRs == 1){CalcExtra1OP(Num1,Num2,Num3); break;}
    if(CalcOPRs == 2){CalcExtra2OP(Num1,Num2,Num3); break;}
    }
    if(CalcNUMs ==1)
    {
    cout << "You can't calculate one number -_-"; break;
    }
    else
    {
    cout << "that's a too much Numbers, sorry I can't Calculate that much :("; break;
    }
    case 2:
    cout << "---------------------------------Biodata Engine---------------------------------\n";
    cout << "Biodata engine is a program that will create a file that contains your biodata,\
    \n you can chose what is the file extension later in the program ;)" << endl;
    BDataEngine();

    break;
    case 0:
    cout << "Good Bye!";
    extcho = false;

    break;

}
}
while(extcho = false){terminate;}
}

int Calculator(float,float)
{
    int CalcCho;
    cout << "\nOkay Now Input Two Numbers\nNumber1: ";
    cin >> Num1;
    cout << "\nNumber2: ";
    cin >> Num2;
    cout << "Now choose the operator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n\nYour Answer: ";
    cin >> CalcCho;
    switch(CalcCho){
        case 1:
        cout << "The Result is " <<(Num1+Num2);
        break;
        case 2:
        cout << "The Result is " <<(Num1-Num2);
        break;
        case 3:
        cout << "The Result is " <<(Num1*Num2);
        break;
        case 4:
        cout << "The Result is " <<(Num1/Num2);
        break;
        default:
        cout << "Can u see my fucking instruction said number beetween 1-4?";
        terminate;
    }
    }
int CalcExtra1OP(float,float,float)
{
    int CalcCho;
    cout << "Now, you insert 3 numbers to operate with\nNumber1: ";
    cin >> Num1;
    cout << "\nNumber2: ";
    cin >> Num2;
    cout << "\nNumber3: ";
    cin >> Num3;
  cout << "Now choose the operator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n\nYour Answer: ";
    cin >> CalcCho;
    switch(CalcCho){
        case 1:
        cout << "The Result is " <<(Num1+Num2+Num3);
        break;
        case 2:
        cout << "The Result is " <<(Num1-Num2-Num3);
        break;
        case 3:
        cout << "The Result is " <<(Num1*Num2*Num3);
        break;
        case 4:
        cout << "The Result is " <<(Num1/Num2/Num3);
        break;
        default:
        cout << "Can u see my fucking instruction said number beetween 1-4?";
        terminate;
    }

}
int CalcExtra2OP(float,float,float)
{
    int CalcCho;
    int CalcCho2;
     cout << "Now, you insert 3 numbers to operate with\nNumber1: ";
    cin >> Num1;
    cout << "\nNumber2: ";
    cin >> Num2;
    cout << "\nNumber3: ";
    cin >> Num3;
    cout << "Now choose the first operator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n\nYour Answer: ";
    cin >> CalcCho;
    cout << "Now choose the second operator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n\nYour Answer: ";
    cin >> CalcCho2;
    cout << "\n" << endl;
    if(CalcCho == 1){
    if(CalcCho2 == 1){cout << Num1 << " + " << Num2 << " + " << Num3 << " = ";
                      cout << Num1 + Num2 + Num3;
    }
    if(CalcCho2 == 2){cout << Num1 << " + " << Num2 << " - " << Num3 << " = ";
                      cout << Num1 + Num2 - Num3;
    }
    if(CalcCho2 == 3){cout << Num1 << " + " << Num2 << " * " << Num3 << " = ";
                      cout << Num1 + (Num2 * Num3);
    }
    if(CalcCho2 == 4){cout << Num1 << " + " << Num2 << " / " << Num3 << " = ";
                      cout << Num1 + (Num2 / Num3);
    }
    }
    if(CalcCho == 2){
    if(CalcCho2 == 1){cout << Num1 << " - " << Num2 << " + " << Num3 << " = ";
                      cout << Num1 - Num2 + Num3;
    }
    if(CalcCho2 == 2){cout << Num1 << " - " << Num2 << " - " << Num3 << " = ";
                      cout << Num1 - Num2 - Num3;
    }
    if(CalcCho2 == 3){cout << Num1 << " - " << Num2 << " * " << Num3 << " = ";
                      cout << Num1 - (Num2 * Num3);
    }
    if(CalcCho2 == 4){cout << Num1 << " - " << Num2 << " / " << Num3 << " = ";
                      cout << Num1 - (Num2 / Num3);
    }
    }
    if(CalcCho == 3){
    if(CalcCho2 == 1){cout << Num1 << " * " << Num2 << " + " << Num3 << " = ";
                      cout << (Num1 * Num2) + Num3;
    }
    if(CalcCho2 == 2){cout << Num1 << " * " << Num2 << " - " << Num3 << " = ";
                      cout << (Num1 * Num2) - Num3;
    }
    if(CalcCho2 == 3){cout << Num1 << " * " << Num2 << " * " << Num3 << " = ";
                      cout << Num1 * (Num2 * Num3);
    }
    if(CalcCho2 == 4){cout << Num1 << " * " << Num2 << " / " << Num3 << " = ";
                      cout << Num1 * Num2 / Num3;
    }
    }
    if(CalcCho == 4){
     if(CalcCho2 == 1){cout << Num1 << " / " << Num2 << " + " << Num3 << " = ";
                      cout << (Num1 / Num2) + Num3;
    }
    if(CalcCho2 == 2){cout << Num1 << " / " << Num2 << " - " << Num3 << " = ";
                      cout << (Num1 / Num2) - Num3;
    }
    if(CalcCho2 == 3){cout << Num1 << " / " << Num2 << " * " << Num3 << " = ";
                      cout << Num1 / Num2 * Num3;
    }
    if(CalcCho2 == 4){cout << Num1 << " / " << Num2 << " / " << Num3 << " = ";
                      cout << Num1 / Num2 / Num3;
    }
    }
}

void BDataEngine()
{
    string Name;
    int Gender;
    int Birthdate;
    int BirthMonth;
    int BirthYear;
    int age;
    int fileEx;

    cout << "Now please enter your name\nYour Answer: ";
    cin >> Name;
    cout << "\nYour gender:\n1.Male\n2.Female\nYour answer: ";
    cin >> Gender;
    cout << "How about your birth date?(just the date)\nYour answer: ";
    cin >> Birthdate;
    cout << "Month?\nYour answer: ";
    cin >> BirthMonth;
    cout << "Year?\nYour answer: ";
    cin >> BirthYear;
    cout << "Even though i know your birth date i need to ask 'How old are you?'\nYour answer: ";
    cin >> age;
    cout << "That's Enough now see in the same directory of this program there will be\n a file named 'Biodata.txt' with the file extension you wanted";
    ofstream Data;
    Data.open("Biodata.txt");
    Data << "Name: " << Name << endl;
    Data << "Gender: ";
    if(Gender == 1)
    {Data << "Male" << endl;}
    if(Gender == 2)
    {Data << "Female" << endl;}
    Data << "Birth Date: " << Birthdate << "/" << BirthMonth << "/" << BirthYear << endl;
    Data << "Age: " << age << endl;


}
Just glancing at your code I would say it could benefit from

Functions:
Switches:
Structures/Classes:

I would say start with refactoring this

1
2
3
4
5
6
7
   string Name;
    int Gender;
    int Birthdate;
    int BirthMonth;
    int BirthYear;
    int age;
    int fileEx;


into a class/struct
Why would you want to shorten it?
If it works then your in the clear. Also PC's now in days can run some HEAVY programs and you don't necessarily have to worry about memory space because now in days PC's have quite a bit.
Okay then, i don't really wanted to shorten it, I just thinking Maybe there are some suggestions
Why would you want to shorten it?
If it works then your in the clear. Also PC's now in days can run some HEAVY programs and you don't necessarily have to worry about memory space because now in days PC's have quite a bit.


ehh ok...

@GibsRey. Look into what @BDanielz wrote to you. Learn about classes, functions and switches.
It'll be much better if you shorten it, or divide it into files(classes).
The addition/multiplication/division/subtraction function could be made very simpler. It doesn't even need a function actually. Check this.
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
#include<iostream>
using namespace std;

int main(){

int n;
cout<<"Enter the amount of numbers:\n";
cin>>n;
int nums[n];
cout<<"Enter the numbers:\n";
for (int i=0; i<n; i++){
    cin>>nums[i];
}
cout<<"Now enter the operators (without using spaces)";
string op;
cin>>op;
int final_ans = nums[0];
for (int i=1; i<n; i++){  // < i = 1 because we've initialized the final_ans with the value of nums[0]
    if (op[i-1]=='+'){
        final_ans = final_ans + nums[i];
    }
    else if (op[i-1]=='-'){                 // i-1 so we can start from the start
        final_ans = final_ans - nums[i];
    }
    else if (op[i-1]=='*'){
        final_ans = final_ans * nums[i];
    }
     else if (op[i-1]=='/'){
        final_ans = final_ans / nums[i];
    }
}
cout << final_ans;
}

Last edited on

@GibsRey. Look into what @BDanielz wrote to you. Learn about classes, functions and switches.
It'll be much better if you shorten it, or divide it into files(classes).


Classes Huh? @TarikNeaj
That's a good idea, i will try it!
making header Files and include them makes program looks a bit short.
ehh ok...
Last edited on
Topic archived. No new replies allowed.