Can you tell me why my calculator program won't compile please..
Mar 24, 2012 at 2:30am UTC
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
//-| -------------------------------------------------------------------------------
//-| COP 3014C Fundamentals of Programming
//-|
//-| Assignment ID: PROG9
//-|
//-| File name: calculator.cpp
//-|
//-| Author: bfields Byron Fields
//-|
//-| Due: Mar 23 by 11pm
//-| ---------------------------------------------------------------------------------
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
//-| ------------------------
//-| Declare Variables
//-| ------------------------
float leftOp;
float rightOp;
float multipliCand;
float multiPlier;
float diviDend;
float diviSor;
float Num;
float Pow;
char menu;
float Sum;
float Sub;
float Multi;
float Divi;
float Rem;
float Expo;
//-| ------------------------------------------
//-| Menu interface
//-| =========================================
//-| || MY CALCULATOR ||
//-| | ------------------------------------- |
//-| | A) Add |
//-| | S) Subtract |
//-| | M) Multiply |
//-| | D) Divide |
//-| | R) Remainder (Mod) |
//-| | P) Power (Exponentiate) |
//-| | Q) QUIT (turn off) |
//-| | ------------------------------------- |
//-| | (c) 2012, cop3014cjoe Dr. Jones |
//-| =========================================
//-| --------------------------------------------
cout << "Enter Menu Choice:"
cin >> menu;
//-| ----------------------
//-| Switch Statement
//-| ----------------------
switch (menu)
{
case 'A' :
cout << "A: Enter left operand: " << endl;
cin >> leftOp;
cout << "A: Enter right operand: " << endl;
cout << "A: Enter left operand: " << endl;
cin >> leftOp;
cout << "A: Enter right operand: " << endl;
cin >> rightOp;
Sum = leftOp + rightOp;
cout << leftOp << " + " << rightOp << " = " << Sum << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'S' :
cout << "S: Enter left operand: " << endl;
cin >> leftOp;
cout << "S: Enter right operand: " << endl;
cin >> rightOp;
Sub = leftOp - rightOp;
cout << Sub << " = " << leftOp << " - " << rightOp << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'M' :
cout << "M: Enter Multiplicand: " << endl;
cin >> multipliCand;
cout << "M: Enter Multiplier: " << endl;
cin >> multiPlier;
Multi = multipliCand * multiPlier;
cout << multipliCand << " x " << multiPlier << " = " << Multi << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'D' :
cout << "D: Enter Dividend: " << endl;
cin >> diviDend;
cout << "D: Enter Divisor: " << endl;
cin >> diviSor;
Divi = diviDend / diviSor;
cout << diviDend << " / " << diviSor << " = " << Divi << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'R' :
cout << "R: Enter left operand: " << endl;
cin >> leftOp;
cout << "R: Enter right operand: " << endl;
cin >> rightOp;
Rem = leftOp % rightOp;
cout << leftOp << " % " << rightOp << " = " << Rem << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'P' :
cout << "P: Enter the Number: " << endl;
cin >> Num;
cout << "P: Enter the Power: " << endl;
cin >> Pow;
Expo = pow( Num, Pow);
cout << "pow(" << Num << "," << Pow << ")" << " = " << Expo << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
case 'Q' :
cout << "Q: POWERING DOWN ... " << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
break ;
}
return 0;
}
Mar 24, 2012 at 2:46am UTC
this is the error i keep getting:
calculator.cpp:105: invalid operands of types `float' and `float' to binary `
operator%'
Mar 24, 2012 at 2:54am UTC
Line 56: you don't end with a ;
Line 108: modulus requires both values be an integer.
Also, you need to put an ignore after every single time some one types a number in.
Mar 24, 2012 at 3:02am UTC
A couple of things:
56 57
cout << "Enter Menu Choice:" // Forgot semicolon here
cin >> menu;
Secondly, you can't use the modulus operator with
float
s.
Use fmod:
http://www.cplusplus.com/reference/clibrary/cmath/fmod/
@LowestOne: the ignore thing isn't really necessary if you don't use getline or anything like that.
(though it
does save you some time if you decide you want to put it in later)
Mar 24, 2012 at 3:02am UTC
okay thanks. i got it to compile. do you have any idea why my switch does not work though?
Mar 24, 2012 at 3:06am UTC
You have to enter a capital letter...
Or, another way to do this would be
56 57 58
cout << "Enter Menu Choice:" ;
cin >> menu;
menu = toupper(menu); // #include <cctype> for this
to convert the input to uppercase (so you can enter either a lowercase or uppercase letter, and it'll work).
EDIT: By the way, what's the deal with this?
64 65 66 67 68 69 70 71
case 'A' :
cout << "A: Enter left operand: " << endl;
cin >> leftOp;
cout << "A: Enter right operand: " << endl;
cout << "A: Enter left operand: " << endl;
cin >> leftOp;
cout << "A: Enter right operand: " << endl;
cin >> rightOp;
Last edited on Mar 24, 2012 at 3:08am UTC
Mar 24, 2012 at 3:13am UTC
thanks man! you saved my grade. just barely got it submitted.
Topic archived. No new replies allowed.