CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2010
    Posts
    1

    [RESOLVED] Why won't this program I made work ?

    I am trying to make a simple calculator program, it's not finished yet, but when i try to "compile and run" it in Dev C++, it says there is 39 errors! How can there be thirty nine??

    So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...





    #include <iostream.h>
    #include <cmath>
    void normalfun(double);
    void specialfun(float);
    void add(float);
    void subtract(float);
    void multiply(float);
    void divide(float);

    /*
    Name: Simple Calculator
    Copyright: 2010
    Author: @@@@@ @@@@@@@@@
    Date: 27/09/10 15:18
    Description: simple application to perform basic calculations
    */

    int main() // main function, calls other functions on request
    {
    using namespace std;

    cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
    cout << "\tType 0 for normal functions";
    cout << "\n\yType 1 for special functions\n";
    double decision;
    cin >> decision; // creates the variable for users input

    if (decision == 0)
    {
    normalfun(double decision2)
    else specialfun(float)
    }

    system("pause");
    return 0;
    }

    void normalfun(double decision2)
    {
    using namespace std;
    cout << "\n\tType 1 for addition,";
    cout << "\n\tType 2 for subtraction,";
    cout << "\n\tType 3 for multiplication,";
    cout << "\n\tType 4 for division\n\n";

    cin >> decision2;
    {
    if (decision2 == 1);
    add();

    if (decision2 == 2);
    subtract();

    if (decision2 == 3);
    multiply();

    if (decision2 == 4);
    divide();
    }
    }

    void specialfun(float) // special function
    {
    using namespace std;
    cout << "This is special function ";
    }

    void add(float add1, add2, addtotal) // addition function
    {
    using namespace std;
    cout << "Enter two numbers ";
    cout << "\nFirst one: \t";
    cin >> add1;
    cout << "\nSecond one: \t";
    cin >> add2;
    addtotal = add1 + add2;
    cout << "\n ---------------------------------------------- " << "\n \t" << add1 << " add " << add2 << " equals " << addtotal << " .";
    }

    void subtract(float sub1, sub2, subtotal) // subtraction function
    {
    using namespace std;
    cout << "Enter two numbers ";
    cout << "\nFirst one: \t";
    cin >> sub1;
    cout << "\nSecond one: \t";
    cin >> sub2;
    subtotal = sub1 - sub2;
    cout << "\n ---------------------------------------------- " << "\n \t" << sub1 << " subtract " << sub2 << " equals " << subtotal << " .";
    }

    void multiply(float mult1, mult2, multtotal) // multiplication function
    {
    using namespace std;
    cout << "Enter two numbers ";
    cout << "\nFirst one: \t";
    cin >> mult1;
    cout << "\nSecond one: \t";
    cin >> mult2;
    multtotal = mult1 * mult2;
    cout << "\n --------------------------------------------- " << "\n \t" << mult1 << " multiplied by " << mult2 << " equals " << multtotal << " .";
    }

    void divide(float div1, div2, divtotal) // division function
    {
    using namespace std;
    cout << "Enter two numbers ";
    cout << "\nFirst one: \t";
    cin >> div1;
    cout << "\nSecond one: \t";
    cin >> div2;
    divtotal = div1 / div2;
    cout << "\n ---------------------------------------------- " << "\n \t" << div1 << " divided by " << div2 << " equals " << divtotal << " .";
    }






    Thanks
    Last edited by Brad Jones; August 13th, 2012 at 10:24 AM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Why won't this program I made work ?

    Quote Originally Posted by dylanm_36 View Post
    Code:
    #include <iostream.h>
    In before Paul : iostream.h is not a standard header. You have to include <iostream>.

    Also, please post your code between [CODE]code[/CODE] tags.

    What errors are you getting? Concentrate only on the very first one. 99% percent of the time, the first errors screws with the compiler, who doesn't understand the rest of the code, and generates more errors.

    If you can fix the errors 1 by 1, starting with the first, you should be fine. I bet there isn't more than a handful.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why won't this program I made work ?

    You should try reading the errors. The compiler will tell you what's wrong.

    You have two big problems that I see. You're not declaring and calling your functions properly, and you're terminating your if statements with a ;

  4. #4
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Why won't this program I made work ?

    Code:
    if (decision == 0)
    {
    normalfun(double decision2)
    else specialfun(float)
    }
    You are also missing your ; here..

    Are you able to put "using namespace std;" in a function?.. I have never tried..

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why won't this program I made work ?

    Quote Originally Posted by jnmacd View Post
    Code:
    if (decision == 0)
    {
    normalfun(double decision2)
    else specialfun(float)
    }
    You are also missing your ; here..
    That's only one of several problems there..

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Why won't this program I made work ?

    Quote Originally Posted by dylanm_36 View Post
    So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...
    That's not how to program; Produce a heap of non-working code and then let someone else sort it out.

    Instead use stepwise refinement. Start with a small working program. Then add stuff in small increments and make sure each addition works before you continue. You'll have a working program from start to finish.

    So start over and proceed as I described.

  7. #7
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Why won't this program I made work ?

    Quote Originally Posted by GCDEF View Post
    That's only one of several problems there..
    Oops! Didn't even look at what he was doing.
    So yeah there probably are over 30 errors here....

    And nuzzle is right. Start smaller. It seems like you coded all of this THEN tried to compile and was surprised it didn't work!

  8. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Why won't this program I made work ?

    To add to above comments:

    Your first action should be to make a correct intendation. Then start with main function to make it compile

    Code:
    int main() // main function, calls other functions on request
    {
        using namespace std;
    
        cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
        cout << "\tType 0 for normal functions";
        cout << "\n\yType 1 for special functions\n";
        double decision;
        cin >> decision; // creates the variable for users input
    
        if (decision == 0)
        {
            normalfun(double decision2)
        else specialfun(float)
        }
    
        system("pause");
        return 0;
    }

    The trouble starts with 'if (decision == 0)':

    'decision' is of type double. Therefore it should be compared with 0. (though the compiler would correct that for you) and in case of 0 it should work. Generally, a double variable should not be compared with a double literal cause a double might have different internal representations for the same decimal, e. g. 3.0 internally may be 2.99999999999999999999_ or 3.0000000000000000000000_. If you compare the first with 3.0 the comparision will not return equality.

    Your solution is easier. You simply turn the 'double decision' to 'int decision' and are done.

    After the if statement you open a curly bracket which opens a new if block. But that block isn't closed before the else. Generally, it is

    Code:
       if (<condition>)
       {
           <statements>
       }
       else
       {
            <statements>
       }
    So, you need either two blocks with curly brackets or you omit the curly brackets at all what is possible if it is only one statement. I would recommend to always use brackets especially if you were not able to make a correct intendation (as in the code you posted).

    Next is that the calls of normalfun and specialfun have wrong syntax. In a call the argument types are not repeated but you pass only the arguments.

    Code:
           normalfun(decision);
    And of course you need to pass variables defined in the calling function and not those which were only prototypes in the called function. So, you have to pass 'decision' and not 'decision2'.

    In your case it is unclear why you were passing an argument at all. If you look at normalfun implemention you see that t he argument passed wasn't used but overwritten by a new user input. Same applies for specialfun whcih for any reason has a float argument and actually should have no argument.
    Last edited by Brad Jones; August 13th, 2012 at 10:23 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured