Adding variable error for calculator
I get an error message at the 'comReal1 + comReal2 = comSumR;' part and it is completely beyond me as to why. Help would be appreciated!
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
|
#include <iostream> // saves basic input output functions
using namespace std;
int main()
{
char option;
char optionSim;
int sim1, sim2, simSum;
char optionCom;
int comReal1, comImag1, comReal2, comImag2, comSumR, comSumI;
int imag;
do //do while loop, shows menu until user exits
{
// shows Options for the menu
cout << "1) Simple Number Calculator " << endl;
cout << "2) Complex Number Calculator " << endl;
cout << "3) Calculate Capacitance " << endl;
cout << "4) Calculate Reactance " << endl << endl;
cout << "Q) Quit Program " << endl;
//user input's a number
cout << "Please select an option : ";
cin >> option; // taking option value as input and saving in variable "option"
option = toupper(option); // check for upper case 'Q'
if (option == '1') // Simple Calc
{
cout << "Do you want to do... " << endl;
cout << "1) Addition " << endl;
cout << "2) Subtraction " << endl;
cout << "3) Multiplication " << endl;
cout << "4) Division " << endl;
cout << "5) Go back to main " << endl << endl;
cout << "Please select an option : " << endl;
cin >> optionSim;
if (optionSim == '1')
{
cout << "Please input your numbers: " << endl << endl;
cin >> sim1;
cin >> sim2;
simSum = sim1 + sim2;
cout << "The sum of those numbers are " << simSum << endl << endl;
}
else if (optionSim == '2')
{
cout << "Please input your numbers: " << endl << endl;
cin >> sim1;
cin >> sim2;
simSum = sim1 - sim2;
cout << "The sum of those numbers are " << simSum << endl << endl;
}
else if (optionSim == '3')
{
cout << "Please input your numbers: " << endl << endl;
cin >> sim1;
cin >> sim2;
simSum = sim1 * sim2;
cout << "The sum of those numbers are " << simSum << endl << endl;
}
else if (optionSim == '4')
{
cout << "Please input your numbers: " << endl << endl;
cin >> sim1;
cin >> sim2;
simSum = sim1 / sim2;
cout << "The sum of those numbers are " << simSum << endl << endl;
}
else if (optionSim == '5')
{
cout << "go back " << endl;
}
}
else if (option == '2') // Complex Calc
{
cout << "Do you want to do... " << endl;
cout << "1) Addition " << endl;
cout << "2) Subtraction " << endl;
cout << "3) Multiplication " << endl;
cout << "4) Division " << endl;
cout << "5) Go back to main " << endl << endl;
cout << "Please select an option : " << endl;
cin >> optionCom;
if (optionSim == '1') //addition
{
cout << "Given your question in the form '(a +- bi) + (c +- di) the answer will be given in rectangular form" << endl << endl;
cout << "What is the real part of the first number" << endl;
cin >> comReal1;
cout << "What is the imaginary multiplier of the first number" << endl;
cin >> comImag1;
cout << "What is the real part of the second number" << endl;
cin >> comReal2;
cout << "What is the imaginary multiplier of the second number" << endl;
cin >> comImag2;
comReal1 + comReal2 = comSumR; //add the real variable, then add the imaginary, put into line below
comImag1 + comImag2 = comSumI;
cout <<"Your answer is "<<[comSumR]<<" + "<<[comSumI]<<"i" << endl;
}
else if (optionSim == '2')
{
}
else if (optionSim == '3')
{
}
else if (optionSim == '4')
{
}
else if (optionSim == '5')
{
cout << "go back " << endl;
}
}
else if (option == '3') // Capacitance
{
cout << "Do some Capacitance calcs here." << endl << endl;
}
else if (option == '4') // Reactance
{
cout << "Do a little Reactance calc here." << endl << endl;
}
else if (option == 'Q') // Exit
{
cout << "Terminating Program" << endl << endl << endl;
}
else
{
//Displaying error message
cout << "Invalid Option entered" << endl << endl;
}
} while (option != 'Q'); //condition of do-while loop
cout << "Program is finished!!" << endl << endl << endl;
return 0;
}
|
The = operator assigns what's on the right side to what's on the left side.
comReal1 + comReal2
is not a variable so giving it the value of comSumR
doesn't make sense.
Last edited on
an assignment in C[++] is always right to left. Hence lines 103/104 must be:
1 2
|
comSumR = comReal1 + comReal2; //add the real variable, then add the imaginary, put into line below
comSumI = comImag1 + comImag2;
|
Further more: What is your intention regarding the [] on line 106? In C[++] they have special meaning (lambda, array). You cannot use them like so.
EDIT: Line 89:
optionCom
is set but later
optionSim
is checked
Last edited on
Got in sorted now thank you! Knew it would have been a silly mistake.
Topic archived. No new replies allowed.