Thread: Beginner's Calculator: Problem with expressions

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *Moved to C++ programming forum*

    Why did you use arrays for this program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thanks for the reply.

    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    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++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular Expressions (regex.h) small problem
    By _Marcel_ in forum C Programming
    Replies: 0
    Last Post: 03-31-2009, 05:13 AM
  2. Calculator problem.
    By Mrzach3590 in forum C++ Programming
    Replies: 22
    Last Post: 08-03-2006, 12:03 AM
  3. help with simple calculator (beginner)
    By philthemn in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:05 AM
  4. Replies: 2
    Last Post: 11-08-2005, 01:16 PM
  5. C++ Beginner Simple Calculator
    By mom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:51 AM