Thread: Simple Calculator: Not Positive About Correct Usage of C++

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Question Simple Calculator: Not Positive About Correct Usage of C++

    Dear Fellow C++ Programmers,

    I am currently just producing a simple scientific calculator to improve and refresh my memory on some skills (I'm a novice programmer). - I am not sure if what I wrote is legal, such as declaring a function from a class outside of the function (you will see). I tried to space out my coding nicely and added comments where my problems are. If anyone could provide aid I would very much appreciate it - thank you!

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main() {
    
    //global variables
    char choice;
    int ammount;
    float a;
    float b;
    float c;
    float answer;
    
    //class containing functions to be created...
    class op_function; {
    op_add();
    op_sub();
    op_mul();
    op_div();
    op_sqr();
    op_sqr_root();
    op_pi();
    op_sin();
    op_cosin();
    op_tan();
    op_sci_notate();
    op_retansw();
    }
    
              //main menu
              cout << "Scientific Calculator Version 0.1 /n";
              cout << "[01] Addition /n";
              cout << "[02] Subtraction /n";
              cout << "[03] Multiplcation /n";
              cout << "[04] Division /n";
              cout << "[05] Square /n";
              cout << "[06] Square Root /n";
              cout << "[07] Circle /n";
              cout << "[08] Sin - Cosin - Tangent /n";
              cout << "[09] Scientific Notation /n";
              cout << "[10] Formulae /n";
              cout << "[10] About Information /n";
              cout << "Selection # ["
    
              cin >> choice
    
              cout << "] /n";
    
              switch(choice) {
    
                             //case '01' is the selection for addition
                             //case '01' contains 1 overloaded function
                             case '01':
    
                                  cout << "Addition Version 0.1 /n";
                                  cout << "Supported: Three Floating Pnts. /n"
                                  cout << "Specify Number Ammount: ";
    
                                  cin >> ammount;
    
                                      //does this first if statement go with the last else statement?
                                      if (ammount == 1) {
                                         cout << "Cannot Compute Value w/ One Number"; }
    
                                      if (ammount == 2) {
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         //do I specify the decleration here? or in the class structure?
                                         op_function.op_add(float a, float b);
    
                                         op_function.op_add() {
                                                  return a + b; }
    
                                         answer = op_function.op_add(a, b);
    
                                         cout << "Answer: = " answer;
    
                                      else {
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         cout << "/n";
                                         cout << "Enter Num(3): ";
    
                                         cin >> c;
    
                                         //do I specify this here or in the class structure?
                                         op_function.op_add(float a, float b, float c);
    
                                         op_function.op_add() {
                                                  return a + b + c; }
    
                                         answer = op_function.op_add(a, b, c);
    
                                         cout << "Answer: = " answer; }
    
                                      else {
                                         cout << "Cannot Compute Value w/ One Number"; }
    
                                      //Rest Is Unfinished
    I am trying to maintain clean and professional looking code for my own reference and skill and in situations such as these. I hope it is clear enough to understand - if not, I guess i'll still be tinkering with it.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Your class and global variable definitions should be outside of main( ), and its not className; { ... }, its className {...};
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Oof! Yes that was a grave mistake - nothing that couldn't be quickly fixed. - I made some changes while I was waiting for a reply and many things are comming back to me. - I am running into only "parse" errors now (for those of you who have Bloodshed DevC++) - Everything seems to be in order but I have a feeling it has to do with the op_function.op_add(a, b) bit... heres the modified coding.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    char choice;
    int ammount;
    float a;
    float b;
    float c;
    float answer;
    
    class op_function {
    public:
    float op_add(float a, float b, float c);
    float op_sub();
    float op_mul();
    float op_div();
    float op_sqr();
    float op_sqr_root();
    float op_pi();
    float op_sin();
    float op_cosin();
    float op_tan();
    float op_sci_notate();
    float op_retansw();
    };
    
    int main() {
    
              cout << "Scientific Calculator Version 0.1 /n";
              cout << "[01] Addition /n";
              cout << "[02] Subtraction /n";
              cout << "[03] Multiplcation /n";
              cout << "[04] Division /n";
              cout << "[05] Square /n";
              cout << "[06] Square Root /n";
              cout << "[07] Circle /n";
              cout << "[08] Sin - Cosin - Tangent /n";
              cout << "[09] Scientific Notation /n";
              cout << "[10] Formulae /n";
              cout << "[10] About Information /n";
              cout << "Selection #: ";
    
              cin >> choice;
    
              cout << "/n";
    
              switch(choice) {
    
                             case '1':
    
                                  cout << "Addition Version 0.1 /n";
                                  cout << "Supported: Three Floating Pnts. /n";
                                  cout << "Specify Number Ammount: ";
    
                                  cin >> ammount;
    
                                      if (ammount == 1) {
                                         cout << "Cannot Compute Value w/ One Number"; }
    
                                      if (ammount == 2) {
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         op_function.op_add(a, b) {
                                                  return a + b; };
    
                                         answer = op_function.op_add(a, b);
    
                                         cout << "Answer: = " answer;
    
                                      else {
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         cout << "/n";
                                         cout << "Enter Num(3): ";
    
                                         cin >> c;
    
                                         op_function.op_add(a, b) {
                                                  return a + b + c; }
    
                                         answer = op_function.op_add(a, b, c);
    
                                         cout << "Answer: = " answer; }
    
                                      else {
                                         cout << "Cannot Compute Value w/ One Number"; }
    I think the problem lies in the op_function accessing the op_add. Do I need to access op_add() through op_function since I placed it in its class?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You need to create an intstance of op_function.
    Code:
    op_function opfun;
    float x = opfun.op_add( 1, 2, 3 );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Parse errors! There attacking! Grr... no more errors with the calling of the function and return types... now just inconvienant parse errors that i'm about to blow a fuse at - everything seems in order - I checked syntax but I am sure I missed something =( take a look...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    char choice;
    int ammount;
    float a;
    float b;
    float c;
    float d;
    float answer;
    
    class op_function {
    public:
    float op_add(float, float, float);
    float op_add(float, float);
    float op_sub();
    float op_mul();
    float op_div();
    float op_sqr();
    float op_sqr_root();
    float op_pi();
    float op_sin();
    float op_cosin();
    float op_tan();
    float op_sci_notate();
    float op_retansw();
    };
    
    int main() {
    
    op_function op_funct;
    
              cout << "Scientific Calculator Version 0.1 /n";
              cout << "[01] Addition /n";
              cout << "[02] Subtraction /n";
              cout << "[03] Multiplcation /n";
              cout << "[04] Division /n";
              cout << "[05] Square /n";
              cout << "[06] Square Root /n";
              cout << "[07] Circle /n";
              cout << "[08] Sin - Cosin - Tangent /n";
              cout << "[09] Scientific Notation /n";
              cout << "[10] Formulae /n";
              cout << "[10] About Information /n";
              cout << "Selection #: ";
    
              cin >> choice;
    
              cout << "/n";
    
              switch(choice) {
    
                             case '1':
    
                                  cout << "Addition Version 0.1 /n";
                                  cout << "Supported: Three Floating Pnts. /n";
                                  cout << "Specify Number Ammount: ";
    
                                  cin >> ammount;
    
                                      if (ammount == 1) {
                                         cout << "Cannot Compute Value w/ One Number"; }
    
                                      if (ammount == 2) {
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         op_funct.op_add(a, b) { //Parse Error Before '{'
                                                  return a + b; };
    
                                         answer = op_funct.op_add(a, b, c);
    
                                         cout << "Answer: = " << answer;
    
                                      else { //Parse Error Before 'else'
    
                                         cout << "Enter Num(1): ";
    
                                         cin >> a;
    
                                         cout << "/n";
                                         cout << "Enter Num(2): ";
    
                                         cin >> b;
    
                                         cout << "/n";
                                         cout << "Enter Num(3): ";
    
                                         cin >> c;
    
                                         op_funct.op_add(a, b, c) { //Parse Error Before '{'
                                                  return a + b + c;
    
    
                                         answer = op_funct.op_add(a, b, c);
    
                                         cout << "Answer: = " << answer; }
    
                                      else { //Parse Errior Before 'else'
                                         cout << "Cannot Compute Value w/ One Number"; }
                                         break;
                                         return(0); }
    ahhh parse errors! ><

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    'choice' is a single char but you have two chars to make '10'. Use an int instead like this:

    int choice = 0;
    cin >> choice;
    switch(choice) {
    case 1:
    //do something
    break;
    case 2:
    //do something
    break;
    default: // nothing for now
    }


    You need a '}' before the else statement to close the if statement.

    And what's this? a function definition right smack down the middle of another (main() in this case)?

    op_funct.op_add(a, b) { //Parse Error Before '{'
    return a + b; };

    Functions must be defined separately.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Thanks so much! - right now doing some homework that is in need to be finished, - i'll hop right on after, and follow the same proccess for each function, define first, than execute. Is this correct?

    //define first
    op_add(a, b) {
    return a + b;

    /*
    Get values for a and b here
    */

    //enter and execute function
    op_funct.op_add()

    //print to screen answer
    cout << op_add();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Correct usage of malloc()
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2005, 06:28 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 08:58 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM