Nov 5, 2015 at 10:22pm Nov 5, 2015 at 10:22pm UTC
Hello everyone, I am very new to this forum and so in C++, I started just few weeks ago by reading a book I bought and programming small useless things just to practice the concepts but recently I completed my first useful program which works perfectly and does everything I want it to do. This is a program to convert units for example kilograms to pounds or Celsius to Fahrenheit, it has a main menu, and many sub-menus that allow the user to pick up any unit to convert. So my problem is the code, its super long, over 3500 lines, the reason is because I wrote every single possible combination the user might want to try, kilometer to mile, mile to kilometer, mile to mile and so on for all of them, hundreds of else if statements and double variables for each possibility. Below you can see part of my code and tell me if there is any other way to face this kind of issue.
Thanks in advance
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void printIntro()
{
cout << "\n What would you like to calculate?\n" << endl;
}
void printList()
{
//Main Menu
cout << " 1) Mass Units\n" ;
cout << " 2) Distance Units\n" ;
cout << " 3) Temperature Units\n" ;
cout << " 4) Area Units\n" ;
cout << " 5) Data Units\n" ;
cout << " 6) Time Units\n" ;
cout << " 7) Life Statistics\n" ;
cout << " 8) Monthly Expenses\n" ;
cout << " 9) Currency Conversion\n" ;
cout << " 10) Exit\n\n" ;
}
/*Functions for each item in Main Menu*/
double startUM()// Start Unit Mass
{
string unit1;
string unit2;
double amount;
//All the posibilities the user might try (25 in total)
const double tn_kg = 1000;
const double kg_tn = 0.001;
const double kg_g = 1000;
const double g_kg = 0.001;
const double tn_g = 1e+6;
const double g_tn = 1e-6; //...
char answerExit = 'y' ;
system("cls" );
cout << " MASS UNITS " ;
cout << " [tn] Ton\n"
<< " [kg] Kilogram\n"
<< " [g] Gram\n"
<< " [lb] Pound\n"
<< " [oz] Ounce\n" ;
LOOP_massUnits:
do {
cout << "\n Insert unit to convert: " ;
cin >> unit1;
cout << " Insert unit to obtain: " ;
cin >> unit2;
if (unit1 == "tn" && unit2 == "kg" )
{
cout << "\n How many " << unit1.c_str() << " do you want to convert? " ;
cin >> amount;
cout << " " << amount << " " << unit1.c_str() << " are " << amount * tn_kg << " " << unit2.c_str() << ".\n" ;
}
else if (unit1 == "kg" && unit2 == "tn" )
{
cout << "\n How many " << unit1.c_str() << " do you want to convert? " ;
cin >> amount;
cout << " " << amount << " " << unit1.c_str() << " are " << amount * kg_tn << " " << unit2.c_str() << ".\n" ;
}
//...26 else if statements in total...
else if (unit1 != "tn" , "kg" , "g" , "lb" , "oz" && unit2 != "tn" , "kg" , "g" , "lb" , "oz" )
{
cout << "\nERROR: Invalid Unit\n" ;
goto LOOP_massUnits;
}
} while (answerExit != 'y' );
return 0;
}
double startUL()//Start Distance Units
{
//66 double variables representing all the posibilities... this program is getting long...
//67 else if stataments (including for ERROR) for all the posibilities the user may want to try...
}
double startUT()//start Temperature Units
{
//Code with a bunch of variables and else if statements
}
double startUA()//Start Area Units
{
//Code
}
double startUD()//Start Data Units
{
//Code
}
double startUTm()//Start Time Units
{
//Code
}
int startLF()//Start Life statistic
{
//Code
}
double startES()//Start Monthly expenses
{
//Code
}
double startCC()//Start Currency convertion
{
//Code
}
int main()
{
printPresentation();
printTitle();
printIntro();
printList(); //Main Menu
int subj; //Option to execute one function of the main menu items
char posAnswer = 127;
LOOP:
do {
cout << " Choose what you want to calculate inserting the number: " ;
cin >> subj;
if (subj == 1)
{
LOOP_UM:
startUM();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UM;
}
}
else
{
if (subj == 2)
{
LOOP_UL:
startUL();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UL;
}
}
else
{
if (subj == 3)
{
LOOP_UT:
startUT();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UT;
}
}
else
{
if (subj == 4)
{
LOOP_UA:
startUA();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UA;
}
}
else
{
if (subj == 5)
{
LOOP_UD:
startUD();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UD;
}
}
else
{
if (subj == 6)
{
LOOP_UTm:
startUTm();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_UTm;
}
}
else
{
if (subj == 7)
{
LOOP_LF:
startLF();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_LF;
}
}
else
{
if (subj == 8)
{
LOOP_ES:
startES();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_ES;
}
}
else
{
if (subj == 9)
{
LOOP_CC:
startCC();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
else
{
goto LOOP_CC;
}
}
else
{
if (subj == 666)
{
secret666();
cout << "\n Go to Main Menu? (y/n) " ;
cin >> posAnswer;
if (posAnswer == 'y' )
{
system("cls" );
printMainMenu();
printList();
goto LOOP;
}
}
else
{
if (subj == 10)
{
char exiting;
system("cls" );
cout << " Thank you for using this program!\n"
<< " Press any key followed by ENTER to exit. " ;
cin >> exiting;
exit(exiting);
}
}
}
}
}
}
}
}
}
}
}
if (subj != 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 666)
{
cout << "\n ERROR: Invalid number\n" ;
goto LOOP;
}
} while (subj);
return 0;
}
Last edited on Nov 5, 2015 at 10:26pm Nov 5, 2015 at 10:26pm UTC
Nov 5, 2015 at 10:29pm Nov 5, 2015 at 10:29pm UTC
The solution would be, for each physical property, to store just a few values in vectors which would describe the necessary conversion factors.
For example:
vector<string> unitNames = { "centimeters", "meters", "kilometers", "feet", "yard"};
vector<double> factors = { 0.01, 1, 1000, 0.3048, 0.9144};
vector<double> offsets = { 0,0,0,0,0};
The offsets would play a role, for example, in conversions between Kelvins, Celsius Degrees and Fahrenheit degrees.
Last edited on Nov 5, 2015 at 10:30pm Nov 5, 2015 at 10:30pm UTC