Thread: Jumping into C++

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Jumping into C++

    For the problem "Make your calculator program perform computations in a separate function for each type ofcomputation." I have done this
    Code:
    #include <iostream>
    
    
    using namespace std;
    int add (int x, int y)
    {
        return x+y;
    }
    int subtract(int x,int y)
    {
        return x-y;
    }
    int divide(int x,int y)
    {
        return x/y;
    }
    int multiply(int x,int y)
    {
        return x*y;
    }
    
    
    int main()
    {
    
    
      while (true)
      {
          int x;
      int y;
      string arg;
      cout<<"please enter a number";
      cin>>x;
      cout<<"enter argument";
      cin>>arg;
      cout<<"enter another number";
      cin>>y;
    
    
      if (arg=="+")
      {
          cout<<add(x,y);
          break;
      }
      else if (arg=="-")
      {
          cout<<subtract(x,y);
         break;
      }
    
    
      else if(arg=="*")
      {
         cout<<multiply(x,y);
         break;
    
    
      }
    
    
       else if(arg=="/")
       {
    
    
          cout<<divide(x,y);
           break;
          }
          else
          {
              cout<<"wrong input, enter again";
    
    
          }
      }
    }
    But its pretty much long. have I done it in the right process?? or there is a simpler way to do it??

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    You could use a switch statement. Also the word argument could be swapped with operator which would make more sense.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Lesshardtofind View Post
    You could use a switch statement. Also the word argument could be swapped with operator which would make more sense.
    owh, thanks.. I have not learnt the switch statement yet. I will give it a try after chapter 8. thanx.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    You could reduce all that code into

    Code:
      switch(arg){
        case "+":  cout<<add(x,y);      break;
        case "-":  cout<<subtract(x,y); break;
        case "*":  cout<<multiply(x,y); break;
        case "/":  cout<<divide(x,y);   break;
        default:   cout<<"wrong input, enter again";
      }
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Lesshardtofind View Post
    You could reduce all that code into

    Code:
      switch(arg){
        case "+":  cout<<add(x,y);      break;
        case "-":  cout<<subtract(x,y); break;
        case "*":  cout<<multiply(x,y); break;
        case "/":  cout<<divide(x,y);   break;
        default:   cout<<"wrong input, enter again";
      }
    I tried this, but compiler gives error that switch quantity not an integer.
    Code:
    #include <iostream>
    
    using namespace std;
    int add (int x, int y)
    {
        return x+y;
    }
    int subtract(int x,int y)
    {
        return x-y;
    }
    int divide(int x,int y)
    {
        return x/y;
    }
    int multiply(int x,int y)
    {
        return x*y;
    }
    
    
    int main()
    {
    
    
      while (true)
      {
          int x;
      int y;
      string arg;
      cout<<"please enter a number";
      cin>>x;
      cout<<"enter argument";
      cin>>arg;
      cout<<"enter another number";
      cin>>y;
    
    
      switch(arg){
        case "+":  cout<<add(x,y);      break;
        case "-":  cout<<subtract(x,y); break;
        case "*":  cout<<multiply(x,y); break;
        case "/":  cout<<divide(x,y);   break;
        default:   cout<<"wrong input, enter again";
      }
      }
    }

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Lesshardtofind View Post
    Code:
        case "+":  cout<<add(x,y);      break;
    Change all the double quotes to single.
    And the arg to a char instead of a string (or switch arg[0]) .

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by manasij7479 View Post
    Change all the double quotes to single.
    And the arg to a char instead of a string (or switch arg[0]) .
    I tried this, but it seems to form an infinite loop if i enter a letter.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Tamim Ad Dari View Post
    I tried this, but it seems to form an infinite loop if i enter a letter.
    Post your current code.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by manasij7479 View Post
    Post your current code.
    Code:
    #include <iostream>
    
    using namespace std;
    int add (int x, int y)
    {
        return x+y;
    }
    int subtract(int x,int y)
    {
        return x-y;
    }
    int divide(int x,int y)
    {
        return x/y;
    }
    int multiply(int x,int y)
    {
        return x*y;
    }
    
    
    int main()
    {
    
    
      while (true)
      {
          int x;
      int y;
      char arg;
      cout<<"please enter a number";
      cin>>x;
      cout<<"enter argument";
      cin>>arg;
      cout<<"enter another number";
      cin>>y;
    
    
      switch(arg){
        case '+':  cout<<add(x,y);      break;
        case '-':  cout<<subtract(x,y); break;
        case '*':  cout<<multiply(x,y); break;
        case '/':  cout<<divide(x,y);   break;
        default:   cout<<"wrong input, enter again";
      }
      }
    }

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Try this after the inputs happen:
    Code:
            if(!cin)
            {
                cin.clear();
                cin.ignore();
                continue;
            }
    You should be able to write a nice wrapper function for this (or one to get an integer, which doesn't return until this check succeeds).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++ problems
    By Tamim Ad Dari in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2013, 09:04 AM
  2. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM
  3. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 05:34 PM
  4. Jumping between functions
    By Nexus-ZERO in forum C Programming
    Replies: 8
    Last Post: 01-14-2008, 03:47 AM
  5. Jumping Script
    By Krak in forum Game Programming
    Replies: 5
    Last Post: 01-12-2004, 03:19 PM