# Beginner's Calculator: Problem with expressions

• 10-09-2011
SpudCake
Beginner's Calculator: Problem with expressions
Hello.

I'm new to C Programming and am struggling with a practice exercise for a course I'm doing. My aim is to code a simple calculator, which has two constants, two variables that are manually input, and an answer. Then there is a statement at the end showing my two input values and the answer to the calculation.

I just don't know how to get this to work.

Code:

```#include <iostream> using namespace std; int main () {     //variable declarations and initialisation     const double mol = 1.0;     const double gas_const = 8.315;         char temp[50], vol[50], pres[50];     //prompt and read     cout << "Please enter the temperature / K:" << endl;     cin >> temp;     cout << "Please enter the volume / m^3:" << endl;     cin >> vol;     //calculate pressure     pres = (mol * gas_const * temp) / vol;     //print result     cout << "One mole of gas at " << temp << "K, in a volume of " << vol << "m^3, has a pressure of " << pres << "Pa." << endl; }            //end of main()```
The calculator uses the expression P = nRT/V, where n and R are constants that I define in the program, and T and V are variables that I input manually.

This coding gives a single error on the line with the equation:

Code:

`error C2297: '*' : illegal, right operand has type 'char [50]'`
Both "pres" and "temp" have red lines underneath. When I move the cursor over "pres" I get "Error: expression must be a modifyable lvalue", and doing the same with "temp" gives "Error: expression must have arithmetic or enum type".

I've been working on this for almost four hours at the time of posting. I've Googled and searched through books, I've gone through my notes, but I don't understand what I'm supposed to do.

If I remove the expression and give "pres" a fixed value, the statement at the end works. Its the expression part that I don't understand.

Could anyone help me with this? Any help would be very much appreciated.

Thanks.
• 10-09-2011
laserlight
*Moved to C++ programming forum*

Why did you use arrays for this program?
• 10-09-2011
SpudCake
Sorry for initially posting this in the wrong forum. I don't understand much about what I'm doing.

Apart from the expression, what I've typed is the only way I've been shown how to code. I'm following an example from a previous exercise.
• 10-09-2011
laserlight
Instead of blindly following the code that you have been shown, think about what the code is supposed to do. For example, since the temperature is supposed to be a number, why not make temp a double instead of an array?
• 10-09-2011
SpudCake
Well, I took your advice, and got my calculator working at the first attempt. Thank you very much.

Code:

```#include <iostream> using namespace std; int main () {     //variable declarations and initialisation     const double mol = 1.0;     const double gas_const = 8.315;     double temp;     double vol;     double pres;     //prompt and read     cout << "Please enter the temperature / K:" << endl;     cin >> temp;     cout << "Please enter the volume / m^3:" << endl;     cin >> vol;     //calculate pressure     pres = (mol * gas_const * temp) / vol;     //print result     cout << "One mole of gas at " << temp << "K, in a volume of " << vol << "m^3, has a pressure of " << pres << "Pa." << endl; }            //end of main()```
The thing is, I may lose marks when I submit this into the marking software. It may mark me down because I've used a method that hasn't been shown in the notes/lectures, so far atleast. All of the examples I have for the course use the coding that I originally used.

Sooo, is it actually possible to get this calculator working with arrays? Are arrays like matrices?

Thanks again.
• 10-09-2011
laserlight
Quote:

Originally Posted by SpudCake
The thing is, I may lose marks when I submit this into the marking software. It may mark me down because I've used a method that hasn't been shown in the notes/lectures, so far atleast.

Did it actually mark you down? If so, perhaps you should approach your instructor to ask why.

Quote:

Originally Posted by SpudCake
Sooo, is it actually possible to get this calculator working with arrays?

Yes... but it means that you will read input as a string and then convert the input to a double. This is not wrong, and in fact it has its benefits, but the typical way of converting the input from string to double in C++ is going to be akin to reading directly into the double variable.

Quote:

Originally Posted by SpudCake
Are arrays like matrices?

Yes, and in fact I recall a maths textbook of mine defining a matrix as a rectangular array.
• 10-10-2011
SpudCake

I submitted this program with a few more tweaks, and a second one similar to it, and they both got 100% of the marks. Myself and others are a bit concerned about using code that hasn't been on the course, but seeing as this was accepted, the instructor must welcome us developing other ways.

I prefer the way you advised. It just seems better to me. And I can get it to work.

Thanks again. You've been a great help.
• 10-11-2011
Elysia
I don't see "an other way" that isn't more complicated, probably involving more code that you haven't actually studied yet.
Btw, this is C++, not C, so you know. They're different languages, and you are studying C++. Don't confuse it with C.
• 10-12-2011
SpudCake
The course I'm doing is called "Introduction to Programming in C", but we use a C++ compiler, and as you said, we're using C++ code.

Its all very confusing to me, and even to people on the course with extensive experience of C++.