Thread: Calc practice problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Calc practice problem

    Hey so I came back to c++ earlier, and while I'm not an amazing coder I figured I could do the practices easily for some warm ups to get used to the language again. so I got the calc one and did it, and from what I can see the code I have is the same as the solution, just lacking the "using namespace std" because I remember seeing people say that was a bad habit to have, though I did use "std::???" appropriately.

    But when I run the code and try to use something, it starts spamming the console with the normal message, and I can't figure out why, I'm sure its something really simple and basic that I'm missing, but could you guys lend me a hand?

    Code:
    #include <iostream>
    
    int multiply(int x, int y)
    {
      return x*y;
    }
    
    
    
    
    int divide(int x, int y)
    {
      return x/y;
    }
    
    
    int add(int x, int y)
    {
      return x+y;
    }
    
    
    int subtract(int x, int y)
    {
      return x-y;
    }
    
    
    int main()
    {
      char op='c';
      int x, y;
      while(op!='e')
      {
      std::cout<< "What operation would you like to perform: add(+), subtract(-), divide(/), multiply(*), [e]xit?";
      std::cin>>op;
      switch(op)
      {
        case '+':
        std::cin>>x;
        std::cin>>y;
        std::cin>>y;
        std::cout<< x << "+" << y << "=" << add(x, y) <<std::endl;
        break;
        
        case '-':
        std::cin>>x;
        std::cin>>y;
        std::cout<< x << "-" << y << "=" << subtract(x, y) <<std::endl;
        break;
        
        case '/':
        std::cin>>x;
        std::cin>>y;
        std::cout<< x << "/" << y << "=" << divide(x, y) <<std::endl;
        break;
        
        case '*':
        std::cin>>x;
        std::cin>>y;
        std::cout<< x << "*" << y << "=" << multiply(x, y) <<std::endl;
        break;
        
        case 'e':
        return 0;
        default:
        std::cout<< "Sorry, try again" <<std::endl;
        }
      }
      return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What is the problem exactly?
    here
    Code:
         case '+':
        std::cin>>x;
        std::cin>>y;
        std::cin>>y;
    you need only once the input for y

    An after message :
    I think that you will agree that this is more like a C program,where printf and scanf are replaced by std::cout and std::cin...
    Why don't try to create a class and have the operations as member-functions ,x and y as data members and also setting some geters and seters and a function print?
    Well this not a perfect example of OOP,because in real world you will need on calculator to perform these operatons and since OOP is about representing real world into code,you are going to create only one instance of the class
    Last edited by std10093; 11-01-2012 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Practice Problem Help!
    By PCBeginner in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2012, 10:24 AM
  2. Even or Odd (practice problem)
    By Amphibian in forum C Programming
    Replies: 16
    Last Post: 09-22-2010, 01:32 PM
  3. Pixle Calc. Problem
    By loopshot in forum Game Programming
    Replies: 18
    Last Post: 03-25-2005, 01:28 AM
  4. polish notation calc problem
    By deedlit in forum C Programming
    Replies: 6
    Last Post: 06-14-2004, 10:17 PM
  5. Calc Problem.
    By KrAzY CrAb in forum C Programming
    Replies: 4
    Last Post: 06-02-2002, 09:34 PM

Tags for this Thread