Jumping into C++

This is a discussion on Jumping into C++ within the C++ Programming forums, part of the General Programming Boards category; For the problem "Make your calculator program perform computations in a separate function for each type ofcomputation." I have done ...

  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
    407
    You could use a switch statement. Also the word argument could be swapped with operator which would make more sense.

  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
    407
    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";
      }

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    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]) .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    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).
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 06:26 AM
  3. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 06: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

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