Basic calculator program in C++

This is a discussion on Basic calculator program in C++ within the C++ Programming forums, part of the General Programming Boards category; This program is just for two variables to be added up and display the results, but when I run it ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    91

    Unhappy Basic calculator program in C++

    This program is just for two variables to be added up and display the results, but when I run it and input 2 and 2, it gives me 3.21412e-039.

    Code:
    #include <iostream>
    
    using namespace std;
    
    float calc(float a, float b, float c) {
          
          c = a + b;
          cin.get();
          return c;
    }
    
    int main() {
           float a;
           float b;
           float c;
           
           cout << "Input a number: ";
           cin >> a;
           cout << "Input a second number: ";
           cin >> b;
           calc(a, b, c);
           cout << a << " plus " << b << " equals " << c << " .\n";
           cin.get();
    }

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Why are you passing c as an argument? You should define calc in terms of two arguments and then use c = calc(a, b);

    The variable c in the calc function is local to that function, which means that the line c = a + b; does not affect the value of c in main.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well, I changed it around, but 2+2 seems to equal 1.#QNAN. Sorry, I'm kinda new at this...

    Code:
    #include <iostream>
    
    using namespace std;
    
    float calc(float a, float b) {
        a + b;
        cin.get();
    }
    
    int main() {
           float a;
           float b;
           float c;
           
           cout << "Input a number: ";
           cin >> a;
           cout << "Input a second number: ";
           cin >> b;
           c = calc(a, b);
           cout << a << " plus " << b << " equals " << c << " .\n";
           cin.get();
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You still need to declare and return 'c' in your function, not outside, and it need not be passed as a parameter. The code you have really should not compile, as you state that your function retruns a float, but you do not return anything.

    Alternatively, you could simply change your function to return the result of addition without the temporary, with 'return a + b;' or, equivalently, 'return (a + b);'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well.. that worked. Thanks. And what exactly would be the proper place to put a cin.get(); in a program? I place it at the very end of main(), but it doesn't work.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Most likely because there is a leftover newline character from the previous call to cin:perator>>.

    Try adding:
    cin.ignore(numeric_limits<streamsize>::max());
    Right before your cin.get.

    Also, be sure to include <limits> for numeric_limits.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Uhhh... I'm like only a day into C++ programming... that code line looks way above my level..

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Basically, there could be extra garbage that you don't want in the input buffer. Calling cin.ignore gets rid of that garbage (up to the maximum amount of characters in a stream), so that your cin.get() won't pick up any garbage.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Why do you need cin.get();? Just open a command line and run your program from the command line. That's how command line programs like being run.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No reason not to run the program how you want to. Running from the command line does simplify matters.

    Perhaps there is a setting in Windows that prevents terminated "terminals" from closing (I know there is on Mac OS X), but I do not know of it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Good point. :P

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    OK, I'm within the Command Prompt thingy, in the folder where my program is, and when I type calc.cpp and press enter, it acts as if it was run through Dev-C++ without a cin.get(). Hmmmm.

    EDIT: Scratch that, when I type calc.cpp, it opens it up in Dev-C++ for editing. Poo.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Compile. Type: calc.exe
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Yet again, I forget everything I knew while learning basic Java. :P Thanks again...

    Also, I don't think i really understand what pointers could be used for. Why not just use the original variables themselves instead of the pointer?

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Its because using pointers you can modify the original variable from directly within a function instead of copying the variables over, then manipulating the data and finally returning the data back to main. You have to bear in mind that in your code above, when you pass the variables in, the a and b in your calc function are actually copies of the a and b in your main so in effect, you actually have four variables in memory rather than the two that you expect to have. To modify the original variables from within a function, you either have to use global variables (which should be avoided unless absolutely necessary) or you can use pointers.

    I just quickly threw together some code to clarify the above so you can see what I mean:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void calc(int a, int b) {
        a = 10; 
        b = 20;
        cout << "Within the calc function a now equals " << a << " and b now equals " << b << "\n";
    }
    
    int main()
    {
        int a = 2;
        int b = 4;
            
        cout << "Before the calc function a equals " << a << " and b equals " << b << "\n";
        calc(a, b);
        cout << "\nAfter the calc function (back in main) a still equals " << a << "\n";
        cout << "and b still equals " << b << "\n";   
        cin.get();
        return 0;
    }
    Last edited by ./fsck; 08-25-2005 at 02:44 AM.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  4. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21