Add, Subtract Calculator...

This is a discussion on Add, Subtract Calculator... within the C++ Programming forums, part of the General Programming Boards category; Can someone please tell me what is wrong with this source code? Code: #include <iostream.h> #include <conio.h> int add(int a, ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    Add, Subtract Calculator...

    Can someone please tell me what is wrong with this source code?
    Code:
    #include <iostream.h>	
    #include <conio.h>	
    int add(int a, int b);
    int subt(int c, int d);
    int main()
    {  	 
      int input, a, b, c, d;
      cout<<"1. Add 2 numbers";
      cout<<"2. Subtract 2 numbers";
      cout<<"3. Exit Calculator";
      cin>>input;	 
      switch (input)	
      {	   
       case 1:
           {
             cout<<"Enter two numbers to be added: ";
             cin>>a>>b;
             cout<<"The sum of your two numbers is: "<<add(a, b);
             cin.get();
             return 0;
           }
           int add(int a, int b)
           {
             cin.get();
             return a+b;
           }
    	   break;	 
       case 2:
            {
              cout<<"Enter two numbers to be subtracted: ";
              cin>>c>>d;
              cout<<"The difference of your two numbers is: "<<subt(c, d);
              cin.get();
              return 0;
            }
            int subt(int c, int d);
            {
              cin.get();
              return c-d;
            }
            break;
       case 3:
            return 0;   
      default: 	  
            cout<<"Error, bad input, quitting";	
      }
      return 0;
    }
    I'm getting the following errors...

    Line 23: Parse error before {
    Line 27: Break Statement not within loop or switch
    Line 28: case label 2 not within loop or switch statement

    Thank you in advance for any help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Code:
           {
             cout<<"Enter two numbers to be added: ";
             cin>>a>>b;
             cout<<"The sum of your two numbers is: "<<add(a, b);
             cin.get();
             return 0;  //!! Seems pretty pointless to quit the program after one run
           }
           int add(int a, int b) //!! Embedded function definitions are not supported - move them outside of main()
           {
             cin.get();
             return a+b;
           }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    I understand the return 0; part but what do you mean by move the functions outside of main? Also, how can I fix the case 2 and break problem.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    You're giving up way too easily

    Code:
    int main ( ) {
        // stuff for main
        return 0;
    }
    
    // See, now it's outside main
    int add(int a, int b)
    {
      cin.get();
      return a+b;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    The only reason I gave up is because I've tryed at least 50 different things and couldn't get it to work and I get all confused by big words lol.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    I'm sorry. I must be dumb because I cannot get this. Here is the source I have now.

    Code:
    #include <iostream.h>
    #include <conio.h>
    int add(int a, int b);
    int subt(int c, int d);
    int main()
    {
      int input, a, b, c, d;
      cout<<"1. Add 2 numbers.";
      cout<<"2. Subtract 2 numbers.";
      cout<<"3. Exit Calculator.";
      cin>>input;
      switch (input)
      {
          case 1:
          cout<<"Enter two numbers to be added: ";
          cin>>a>>b;
          cout<<"The sum of your two numbers is: "<<add(a, b);
          int add(int a, int b)
          return a+b;
          break;
          case 2:
        cout<<"Enter two numbers to be subtracted: ";
        cin>>c>>d;
        cout<<"The difference of your two numbers is: "<<subt(c, d);
        int subt(int c, int d)
        return c-d;
        break;
        case 3:
        return 0;
      }
      cin.get();
      return 0;
    }
    Now I am getting parse error before return on line 19 and and line 26.
    Sorry if this isn't organized. I kept moving stuff around.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Dont expect that too many does this for you
    Code:
    #include <iostream>  // Use standard header; loose the h
    #include <conio.h>  // I dont know if there is a conio header or if this is the standard one.
    using namespace std;  // Use this together with iostream
    int add(int a, int b);
    int subt(int c, int d);
    int main()
    {
      int input, a, b, c, d;
      cout<<"1. Add 2 numbers.";
      cout<<"2. Subtract 2 numbers.";
      cout<<"3. Exit Calculator.";
      cin>>input;
      switch (input)
      {
          case 1:
          cout<<"Enter two numbers to be added: ";
          cin>>a>>b;
          cout<<"The sum of your two numbers is: "<<add(a, b);
          break;
          case 2:
        cout<<"Enter two numbers to be subtracted: ";
        cin>>c>>d;
        cout<<"The difference of your two numbers is: "<<subt(c, d);
        break;
        case 3:
        return 0;
      }
      cin.get();
      return 0;
    }
    int add(int a, int b)
    {
        
          return (a+b);
    }
    
    int subt(int c, int d)
    {
        return (c-d);
    }

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    So you have to tell the compiler to go to the section that has the solution or whatever. I think I understand.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Most programming languages will not let you define the body of a function within the body of another function. Also, the body should be preceded by a function declaration at the top of the program. That enables you to call any function from anywhere in the program. When you call a function you don't use the same syntax for defining it -

    int add(int a, int b);
    int a = 512;
    int b = add(a, a);

    Always count your braces, too. Many a compiler error is caused by mismatched braces...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infix Calculator
    By Bnchs in forum C++ Programming
    Replies: 12
    Last Post: 05-16-2007, 10:49 AM
  2. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 04:30 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. how to add a factorial ()
    By correlcj in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2002, 02:28 PM
  5. Can somebody test this code please
    By andy bee in forum C Programming
    Replies: 6
    Last Post: 10-09-2001, 03:08 PM

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