basic Calculator
Jul 6, 2010 at 11:50am UTC
Made a basic calculator because iwas bored on workexperience and i thought i'd get some feedback.
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
////////////////////////////
///// Calculator 300x //////
/////// Aaron Walwyn ///////
/// © Aaron Walwyn 2010 ////
////////////////////////////
/* Bugs to fix: Multiplication Array */
#include <cstdlib>
#include <iostream>
using namespace std;
int displayMenu()
{
int choice;
cout << " \n Welcome to the Calculator 300x\n Please select a Calculation Option\n\n"
<< " (1) Addition\n"
<< " (2) Subtraction\n"
<< " (3) Division\n"
<< " (4) Mulpilication\n"
<< " (5) Exit\n\n"
<< " Your Choice: " ;
cin >> choice;
return choice;
}
int main(int argc, char *argv[])
{
int menu;
float total, num1, num2;
menu = displayMenu();
system("CLS" );
if (menu == 5)
{
cout << " Thank you for using the Calculator 300x.\n Created by Actionman1995\n\n " ;
cin.get();
}
else
{
switch (menu)
{
case 1:
int values;
cout << " How many numbers would you like to enter? : " ;
cin >> values;
float numbers[values];
for (int counter=0; counter<values; counter++)
{
cout << counter+1 << ") Enter number: " ;
cin >> numbers[counter];
}
for (int counter=0; counter<values; counter++)
total+=numbers[counter];
cout << "\n The Answer is : " << total << "\n\n " ;
system("PAUSE" );
break ;
case 2: // Subtraction Completed
cout << " Enter first number: " ;
cin >> num1;
cout << " Enter second number: " ;
cin >> num2;
total=(num1-num2);
cout << "\n The Answer is : " << total << "\n\n " ;
break ;
case 3: // Divison Completed
cout << " Enter first number: " ;
cin >> num1;
cout << " Enter second number: " ;
cin >> num2;
total=(num1/num2);
cout << "\n The Answer is : " << total << "\n\n " ;
break ;
case 4: // Multiplication
cout << " Enter first number: " ;
cin >> num1;
cout << " Enter second number: " ;
cin >> num2;
total=(num1*num2);
cout << "\n The Answer is : " << total << "\n\n " ;
}
system("PAUSE" );
return EXIT_SUCCESS;
}
/*case 4: // Multiplication
for(int counter=1; counter<values; counter++)
total=(numbers[0]*numbers[counter]);
cout << "\n The Answer is : " << total << "\n\n "; */
system("PAUSE" );
return EXIT_SUCCESS;
}
Jul 6, 2010 at 12:08pm UTC
it shouldn't compile on a compliant compiler
line 52 for example, you have a declaration in the switch scope and a variable as array size
Jul 6, 2010 at 12:24pm UTC
Compiled it in bloodshed dev-c++ and it loaded and worked fine
Jul 6, 2010 at 12:28pm UTC
Dev C++ comes with a 5 years old compiler
Jul 6, 2010 at 1:21pm UTC
Sorry, just stumbled upon this thread. Bazzy, would you mind clarifying your previous statement and explain the relation (or lack thereof) to a "compliant compiler"? Thanks in advance.
Jul 6, 2010 at 4:46pm UTC
According to C++ standards, you can't have a jump passed an initialization ( § C.1.4 )
eg:
1 2 3 4 5 6 7
switch ( x )
{
case 1:
int y = 0;
case 2;
}
on line 6 above the control flow would jump y initialization of line 4, the standards doesn't allow this
The other thing is that in C++ the size of an automatic array must be a constant value ( § 8.3.4 )
eg:
1 2 3 4 5 6 7
int OK [5];
const int constant = 6;
int again_OK [constant];
int variable = 7;
int wrong[variable];
Topic archived. No new replies allowed.