Thread: Help needed with first c++ program: a simple calculator

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    11

    Help needed with first c++ program: a simple calculator

    Iv only been learning c++ for two days, so please forgive any noobish errors.
    The problem im having is that i cant get the program to deal with numbers longer than 7 characters in lenth without it printing in standard form (it prints 1,000,000 as 1e+06). At first i thought i just needed to use "double" instead of "float", but this hasn't help. I'v changed it now to "long double" and it hasn't helped either.

    Code:
     #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    long double calc(long double num,long double num1,char function, long double neq, char mult, char div, char add, char sub, int cal);
    int fin(long double neq, int cal);
    int main(int argc, char *argv[]){
    int x;
    while (x==x){
    long double num;
    long double num1;
    long double neq;
    char function;
    char mult;
    char div;
    char add;
    char sub;
    int cal;
    mult='*';
    div='/';
    add='+';
    sub='-';
    cout<<"Please enter your calculation, seperated by spaces, and press enter:" <<"\n";
    cin>>num >>function >>num1;
    cin.ignore();
    calc(num, num1, function, neq, mult, div, add, sub, cal);}}
    
    long double calc(long double num,long double num1,char function, long double neq, char mult, char div, char add, char sub, int cal){
     if(function==mult){
      neq=num * num1;
      cal=1;}
     else if(function==div){
      neq=num / num1;
      cal=1;}
     else if(function==add){
      neq=num + num1;
      cal=1;}
     else if(function==sub){
      neq=num - num1;
      cal=1;}
     else{
      cal=0;}
     fin(neq, cal);}
    
    int fin(long double neq,int cal){
      if(cal==1){
       cout<<"\n" <<neq <<"\n";}
      else if(cal==0){
       cout<<"Operation error, please re-enter your calculation" <<"\n\n";
      return 0;}}
    Im sorry there are no comments, i realy need to get into the habbit of using them. Many thanks in advance.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ouput formatting is controlled using I/O format flags (you'll need setprecision).

    Whether the data type is float or double only determines how many valid and meaningful digits you can expect (floating point values are approximations).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    ok thank you, that info will come in handy alot in the future.

    Is there a way to get the program to recognise decimal answers seperatly so i can handle them differantly?
    for example, i'd like for intergers to go up to a certain number of digits (lets say 10), but i only want to display decimals in the form of two digits after the decimal point. Is there an easy way to do this (such as preset commands i dont know about yet) or is it a complicated process?
    Last edited by yblad; 12-30-2007 at 11:10 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Spaghetti code warning!
    That code is so poorly indented it's very hard to read.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    anon had no problems.and as i said, i have two days experience! the way its indented means that i can personally site read it as its indented in a logical way. If you like ill re-indent all my code and repost. "spagetti code warning" realy is not that helpfull, a nice freindly "i cant read you code because of the indentation" would have been much more civilised.
    Last edited by yblad; 12-30-2007 at 01:13 PM.

  6. #6
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    long double calc(long double num,long double num1,char function, long double neq, char mult, char div, char add, char sub, int cal);
    int fin(long double neq, int cal);
    
    int main(int argc, char *argv[])
    {
        int x;
        while (x==x)
        {
            long double num;
            long double num1;
            long double neq;
            char function;
            char mult;
            char div;
            char add;
            char sub;
            int cal;
            mult='*';
            div='/';
            add='+';
            sub='-';
            cout<<"Please enter your calculation, seperated by spaces, and press enter:" <<"\n";
            cin>>num >>function >>num1;
            cin.ignore();
            calc(num, num1, function, neq, mult, div, add, sub, cal);
        }
    }
    
    long double calc(long double num,long double num1,char function, long double neq, char mult, char div, char add, char sub, int cal)
    {
        if (function==mult)
        {
            neq=num * num1;
            cal=1;
        }
        else if (function==div)
        {
            neq=num / num1;
            cal=1;
        }
        else if (function==add)
        {
            neq=num + num1;
            cal=1;
        }
        else if (function==sub)
        {
            neq=num - num1;
            cal=1;
        }
        else
        {
            cal=0;
        }
        fin(neq, cal);
    }
    
    int fin(long double neq,int cal)
    {
        if (cal==1)
        {
            cout<<"\n" <<neq <<"\n";
        }
        else if (cal==0)
        {
            cout<<"Operation error, please re-enter your calculation" <<"\n\n";
            return 0;
        }
    }
    After running it through a source code formatter, it looks like this. Indenting using this method can make code flow apparent more easily.
    Programming Your Mom. http://www.dandongs.com/

  7. #7
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Of course, flame wars can start over where brackets are placed, etc., but I find the above style acceptable.
    Programming Your Mom. http://www.dandongs.com/

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    thank you, that was a usefull input for me. I have no problem atall with critisism help on style, but i realy dont find just saying "your stlye is rubbish" and not offering any advice helps anyone.

    That is quite a good way of doing it, ill have to try and force myself to do it more like that natuarally.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to be disrespectful, but when I say it's hard to read, I was actually implying that you should try to look over the code and think about how to actually make it more readable.
    And I really don't get how you can actually create the code like that, with everything on a single level. I'd be confused the moment I started writing the code and would be prone to errors.

    Also, there are hundreds of other who has done the same and has been teached to indent, and I'm just tired of repeating it, so forgive me if I don't mention something.

    Now, as far as indentation goes, each "block" should be indented once. A block is what is between a { and }, so basically each function, each if, each loop, etc. That should get you started writing readable code.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    well im new so i didnt know you'd have repeated it a hundred times. Iv been reading some of your posts in other sections, and i have to say i have a massive respect for the amount of knowledge you have. the way my mind works that doesnt look like one level, i guess it could to others. with the way my mind sees it it is 3 differant sections each with many other levels in it. thanks for taking the time to look over it, sorry if i came off brash im realy having a shocker of a day.
    Last edited by yblad; 12-30-2007 at 03:01 PM.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    57
    BTW, you don't need those arguments in the main function parenthesis and it should be:

    #include <cstdio>

    not

    #include <stdio.h>

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    thanks, my compiler throws them in there when i open a new project. same with the include.
    Last edited by yblad; 12-30-2007 at 03:19 PM.

  13. #13
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by yblad View Post
    thanks, my compiler throws them in there when i open a new project. same with the include.
    Be careful, a compiler and an IDE isn't the same thing. I very much doubt that your compiler is making additions to your code before compiling, in that case, i suggest you get another one

    Basically, an IDE is just a code editor, a compiler package and a debugger wrapped into one program, an example would be Dev-C++ or Microsoft Visual C++..
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  14. #14
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    For the 'if' statements, I would consider rewriting it as switch-case statements. It seems more proper to use.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A problem with that code is that it is very strangely constructed:
    1) calc is supposed to return a double (the result?) but it doesn't return anything but calls a printing function instead. Similarly fin fails to return an int if cal!=0.
    2) calc takes a large number of arguments, however nothing meaningful is passed for neq and cal and these arguments are not used. You can declare variables locally in a function, they must not all be declared in the argument list.
    3) I somehow don't believe your system of making a separate char variable for each kind of operator and passing them around is very flexible. So if you wanted to add more operations, you'd add new char's in main, new arguments to calc function, new code into calc function? Instead of, for example, just adding new code to calc...

    (If you turned on compiler warnings you should get a lot.)
    May-be you could try a design something like this:
    Code:
    //returns false if invalid operator (or division by zero)
    bool calculate(double& result, double val1, double val2, char operator);
    //prints a double in a way you like
    void display(double d);
    
    usage:
         get input
         if (calculate(result, a, b, op)) {
            display(result);
        }
        else {
            display error message
        }
    As to your original question, may-be you could post a few samples how you want different results to be displayed? It is rather tricky, though, so you might rather concentrate on learning to write good code (first), rather than fancy user interfaces.

    anon had no problems.
    I only answered to the question without reading the code because it really was a bit hard on the eyes
    Last edited by anon; 12-30-2007 at 07:12 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  3. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  4. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM