Thread: Add, Subtract Calculator...

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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,218
    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,708
    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:
    #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;
    }

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, 05:30 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10: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